如何打印出多元线性回归模型在 Tensorflow 中使用的方程式?
How to print out equation that multiple linear regression model is using in Tensorflow?
对于 python 中 Tensorflow 中的多元线性回归模型,如何打印出模型用于预测标签的方程式。我目前使用的模型需要两个特征来预测一个标签,所以我认为一般方程是 this 但是我如何使用 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)
我将解释如何编写 NN 的方程。
为此,我修改了您的代码并为您的 Y 特征和 Y 标签添加了固定值。我这样做是为了逐步展示整个计算,以便下次您可以自己做。
根据您提供的所有信息,您似乎有
- NN 有 2 层。
- 第一层是规范化层
- 第二层是密集层
- 您的输入张量中有 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,不是方差)
您可以通过对数据应用规范化来检查这一点。
在代码中,如果你 运行 这 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
我们很接近,我们只需要总结这 2 并添加偏差
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]] ))
对于 python 中 Tensorflow 中的多元线性回归模型,如何打印出模型用于预测标签的方程式。我目前使用的模型需要两个特征来预测一个标签,所以我认为一般方程是 this 但是我如何使用 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)
我将解释如何编写 NN 的方程。
为此,我修改了您的代码并为您的 Y 特征和 Y 标签添加了固定值。我这样做是为了逐步展示整个计算,以便下次您可以自己做。
根据您提供的所有信息,您似乎有
- NN 有 2 层。
- 第一层是规范化层
- 第二层是密集层
- 您的输入张量中有 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,不是方差)
您可以通过对数据应用规范化来检查这一点。 在代码中,如果你 运行 这 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
我们很接近,我们只需要总结这 2 并添加偏差
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]] ))