首页 > 解决方案 > 如何打印出多元线性回归模型在 Tensorflow 中使用的方程?

问题描述

对于 Python 中 Tensorflow 中的多元线性回归模型,如何打印出模型用于预测标签的方程。我目前使用的模型需要两个特征来预测一个标签,所以我认为一般方程是这样的,但是我怎样才能使用 Tensorflow 获得所有常量的未知参数和值呢?

代码:

fundingFeatures = fundingTrainSet.copy()
fundingLabels = fundingFeatures.pop('% of total funding spent')
fundingFeatures = np.array(fundingFeatures)
normalizer = preprocessing.Normalization()
normalizer.adapt(fundingFeatures)
model = tf.keras.Sequential([
    normalizer,
    layers.Dense(units=1)
])
model.compile(loss = tf.losses.MeanSquaredError(),
                      optimizer = tf.keras.optimizers.SGD(
    learning_rate=0.06, momentum=0.0, nesterov=True, name="SGD",
))
model.fit(fundingFeatures, fundingLabels, epochs=1000)

标签: pythontensorflowmathlinear-regression

解决方案


我将解释如何编写 NN 的方程。

为此,我修改了您的代码并为您的 Y 特征和 Y 标签添加了固定值。我这样做是为了逐步显示整个计算,以便下次您可以自己做。

根据您提供的所有信息,您似乎有

  1. NN 有 2 层。
  2. 第一层是标准化层
  3. 第二层是密集层
  4. 您的输入张量中有 2 个特征和 1 个单输出

让我们从规范化层开始。对于标准化层,我认为使用“权重”一词有点“奇怪”。权重基本上是将应用于每个输入的均值和方差,以便对数据进行归一化。

我将调用 2 个输入特征 x0 和 x1

如果你运行我的代码(这是你的代码和我的固定数据),你会看到规范化层的权重是

[5。4.6] [ 5.4 11.24]

这意味着您的 [x0 x1] 列的平均值是 [5. 4.6],方差​​为 [5.4 11.24]

我们可以验证吗?我们可以。让我们检查一下 x0。

[1,4,8,7,3,6,6,5,2,8,5]
mean = 5
stddev = 2.323790008
variance = 5.4 ( variance = stddev^2)

如您所见,它与标准化层的“权重”相匹配。

当数据通过归一化层时,每个值将基于 x' = (x-mean)/stddev ( stddev, not variance ) 进行归一化

您可以通过对数据应用规范化来检查这一点。在代码中,如果你运行这 2 行

normalized_data = normalizer(fundingFeatures)

print(normalized_data)

你会得到

[[-1.7213259   1.31241   ]
 [-0.43033147  1.014135  ]
 [ 1.2909944   0.41758505]
 [ 0.86066294 -0.47723997]
 [-0.86066294 -1.07379   ]
 [ 0.43033147  1.31241   ]
 [ 0.43033147 -1.07379   ]
 [ 0.         -1.07379   ]
 [-1.2909944   0.71586   ]
 [ 1.2909944  -1.07379   ]]
 

让我们验证第一个数字。

x0[0] = 1
x0'[0] = (1-5)/2.323790008 = -1.7213 ( it does match)

此时,我们应该能够写出归一化层的方程

y[0]' = (x0-5)/2.323790008   # (x-mean)/stddev
y[1]' = (x1-4.6)/3.352610923

现在,这两个输出将被注入下一层。请记住,您有一个 Dense 层,因此它是完全连接的。这意味着两个值都将注入单个神经元。

这些线显示了密集层的权重和偏差值。

weights = model.layers[1].get_weights()[0]
biases = model.layers[1].get_weights()[1]

print(weights)
print(biases)


[[-0.12915221]
 [-0.41322172]]
[0.32663438]

神经元将每个输入乘以给定的权重,将所有结果与偏差相加。让我们修改 y[0]' 和 y[1]' 以包含权重。

y[0]' = (x0-5)/2.323790008)* -0.12915221
y[1]' = (x1-4.6)/3.352610923 * -0.41322172

我们很接近,我们只需要总结这两个并添加偏差

y' = ((x0-5)/2.323790008)* -0.12915221 + (x1-4.6)/3.352610923 * -0.41322172 + 0.32663438

由于您没有激活功能,我们可以在这里停止。

我们如何验证公式是否正确?让我们使用模型来预测随机输入的标签,看看它是否与我们在方程中放入相同值时得到的结果相匹配。

首先,让我们对 [4,5] 进行模型预测

print(model.predict( [[4,5]] )) 
[[0.3329112]] 

现在,让我们将相同的输入插入我们的方程

y' = (((4-5)/2.323790008)* -0.12915221) + ((5-4.6)/3.352610923 * -0.41322172) + 0.32663438

y' = 0.332911

看来我们很好。我削减了一些精度只是让我的生活更轻松。

这是您的模型的功能。只需将我的号码替换为您的号码即可。

y' = ((x0-5)/2.323790008)* -0.12915221 + (x1-4.6)/3.352610923 * -0.41322172 + 0.32663438

这是代码。我还添加了张量板,因此您可以验证自己在这里所说的内容。

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
from matplotlib import pyplot as plt
import numpy as np
import datetime


fundingFeatures = tf.constant([[1, 9], [4, 8], [8, 6], [7 ,3], [3 ,1], [6, 9], [6, 1], [5, 1], [2, 7], [8, 1]], dtype=tf.int32)
fundingLabels = tf.constant([ 0.8160469,  -0.05249139,  1.1515405,   1.0792135,   0.80369186, -1.7353221,  1.0092108,   0.19228514, -0.10366996,  0.10583907])
normalizer = preprocessing.Normalization()
normalizer.adapt(fundingFeatures)

normalized_data = normalizer(fundingFeatures)

print(normalized_data)

print("Features mean raw: %.2f" % (fundingFeatures[:,0].numpy().mean()))
print("Features std raw: %.2f" % (fundingFeatures[:,0].numpy().std()))
print("Features mean raw: %.2f" % (fundingFeatures[:,1].numpy().mean()))
print("Features std raw: %.2f" % (fundingFeatures[:,1].numpy().std()))

print("Features mean: %.2f" % (normalized_data.numpy().mean()))
print("Features std: %.2f" % (normalized_data.numpy().std()))

model = tf.keras.Sequential([
    normalizer,
    layers.Dense(units=1)
])
model.compile(loss = tf.losses.MeanSquaredError(),
                      optimizer = tf.keras.optimizers.SGD(
    learning_rate=0.06, momentum=0.0, nesterov=True, name="SGD",
))


log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

model.summary()
print('--------------')
weights = model.layers[0].get_weights()[0]
biases = model.layers[0].get_weights()[1]
print('--------------')


model.fit(fundingFeatures, fundingLabels, epochs=1000, callbacks=[tensorboard_callback])

weights = model.layers[0].get_weights()[0]
biases = model.layers[0].get_weights()[1]

print(weights)
print(biases)

print ("\n")

weights = model.layers[1].get_weights()[0]
biases = model.layers[1].get_weights()[1]

print(weights)
print(biases)

print('\n--------- Prediction ------')

print(model.predict( [[4,5]] )) 


推荐阅读