使用 Keras 进行线性回归参数估计

问题描述 投票:0回答:1

我的数据集已经变得非常大,所以我无法使用典型的 OLS 方法来计算我的线性回归估计器,所以我想使用典型的优化器(Adam 似乎很合适)

我知道我可以使用 Keras 相当简单地完成此操作,请参阅下面的示例

    import numpy as np
    import tensorflow as tf
    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense
    from tensorflow.keras.optimizers import Adam

    # Define the model
    def build_model(input_dim):
        model = Sequential()
        # Using a smaller standard deviation for the normal initializer
        model.add(Dense(1, input_dim=input_dim, kernel_initializer=tf.keras.initializers.RandomNormal(mean=0.0, stddev=0.05), activation='linear'))
        # Increased learning rate
        optimizer = Adam(learning_rate=0.1)
        model.compile(loss='mse', optimizer=optimizer, metrics=['mse'])
        return model

    # Example usage:
    X = np.array([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]], dtype=float)
    y = np.array([3, 5, 7, 9, 11], dtype=float)

    # Build and train the model
    model = build_model(input_dim=2)
    model.fit(X, y, epochs=1000, verbose=0, batch_size=5)  # Reduced number of epochs and batch size

    # Make predictions
    predictions = model.predict(X)
    print("Predictions:", predictions.flatten())

    # Output the model summary to check the structure
    model.summary()
    model.get_weights()

但是,我的问题是,即使经过 1000 个 epoch,它仍然没有收敛到明显的 1,1 权重,大约为 1.15 / 0.85

对于这个例子来说,Adam 不是一个好的优化器,还是我做错了什么——我记得不久前玩过 SGD,我记得它在 linreg 问题上的讨论速度非常快。这对我来说有点令人担忧,因为我需要在超过 1,000,000 x 100 的矩阵上运行它,并且在那里运行 1000 个纪元将永远花费时间。

python tensorflow keras
1个回答
0
投票

问题是你对训练数据的选择。您的数据的形式为

(x1, x2)
,但在所有训练示例中都是
x2 == x1 + 1
。所以你实际上只有一个输入,但有两个权重加上一个偏差,从而产生无限多个解决方案。你要学习的功能基本上就是
2 * x1 + 1
。但由于您有两个权重,因此有不同的方法可以将其分开,例如

  • w=(0.9, 0.1), b=0.1
  • w=(0.7, 0.3), b=0.3
  • 等等,也许你已经可以看到图案了。

由于一种解决方案并不比另一种解决方案“更好”,因此它没有理由收敛到“明显”的解决方案。可能的修复:

  • 使用更好的选择不存在此问题的训练数据。
  • 在 Dense 层中设置
    use_bias=False
    ,强制偏差为 0,在这种情况下,
    w
    只有一种解决方案。

如果您想了解更多信息——您的数据表现出多重共线性

© www.soinside.com 2019 - 2024. All rights reserved.