【问题标题】:Neural network with no hidden layers and a linear activation function should approximate a linear regression?没有隐藏层和线性激活函数的神经网络应该近似线性回归?
【发布时间】:2020-05-18 07:16:21
【问题描述】:

据我了解,假设您不使用隐藏层和线性激活函数,神经网络将产生与线性回归相同的方程形式。即 y = SUM(w_i * x_i + b_i) 其中 i 是您拥有的特征数量的 0。

我试图通过使用线性回归的权重和偏差向自己证明这一点,将其输入神经网络并查看结果是否相同。他们不是。

我想知道我的理解是否不正确,或者我的代码是否正确,或者两者兼而有之。


from sklearn.linear_model import LinearRegression
import tensorflow as tf
from tensorflow import keras
import numpy as np

linearModel = LinearRegression()
linearModel.fit(np.array(normTrainFeaturesDf), np.array(trainLabelsDf))

# Gets the weights of the linear model and the intercept in a form that can be passed into the neural network
linearWeights = np.array(linearModel.coef_)
intercept = np.array([linearModel.intercept_])

trialWeights = np.reshape(linearWeights, (len(linearWeights), 1))
trialWeights = trialWeights.astype('float32')
intercept = intercept.astype('float32')
newTrialWeights = [trialWeights, intercept]

# Create a neural network and set the weights of the model to the linear model
nnModel = keras.Sequential([keras.layers.Dense(1, activation='linear', input_shape=[len(normTrainFeaturesDf.keys())]),])
nnModel.set_weights(newTrialWeights)

# Print predictions of both models (the results are vastly different)
print(linearModel.predict(np.array(normTestFeaturesDf))
print(nnModel.predict(normTestFeaturesDf).flatten())

【问题讨论】:

  • 在打印结果之前您是否训练过您的神经网络?如果您没有看到的是随机初始化权重的预测。
  • @Gabriel_D nnModel.set_weights(newTrialWeights) 不是将 NN 的权重设置为线性模型的权重吗?
  • 你能把它变成一个可重现的例子吗? normTrainFeaturesDf 未定义。

标签: python tensorflow machine-learning neural-network linear-regression


【解决方案1】:

是的,单层无激活函数的神经网络相当于线性回归。

定义一些你没有包含的变量:

normTrainFeaturesDf = np.random.rand(100, 10)
normTestFeaturesDf = np.random.rand(10, 10)
trainLabelsDf = np.random.rand(100)

那么输出如预期:

>>> linear_model_preds = linearModel.predict(np.array(normTestFeaturesDf))
>>> nn_model_preds = nnModel.predict(normTestFeaturesDf).flatten()

>>> print(linear_model_preds)
>>> print(nn_model_preds)
[0.46030349 0.69676376 0.43064266 0.4583325  0.50750268 0.51753189
 0.47254946 0.50654825 0.52998559 0.35908762]
[0.46030346 0.69676375 0.43064266 0.45833248 0.5075026  0.5175319
 0.47254944 0.50654817 0.52998555 0.3590876 ]

这些数字是相同的,除了由于浮点精度而导致的微小变化。

>>> np.allclose(linear_model_preds, nn_model_preds)
True

【讨论】:

    猜你喜欢
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 2018-03-22
    • 2019-12-11
    • 2017-03-30
    • 1970-01-01
    • 2018-12-29
    相关资源
    最近更新 更多