如何在Tensorflow中打印出多元线性回归模型正在使用的方程式? [英] How to print out equation that multiple linear regression model is using in Tensorflow?

查看:74
本文介绍了如何在Tensorflow中打印出多元线性回归模型正在使用的方程式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Tensorflow中的python中的多元线性回归模型,如何打印出该模型用于预测标签的方程式.我目前使用的模型具有两个特征来预测一个标签,因此我认为一般方程为 但是如何使用 Tensorflow 获取所有常量的未知参数和值?

For a multiple linear regression model in Tensorflow in python, how can you print out the equation that the model is using to predict the label. The model I am currently using takes two features to predict one label, so I think the general equation is this but how could I get the unknown parameters and values of all the constants using 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方程.

I will explain how you can write the equation of your NN.

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

In order to do that, I have modified your code and added fixed values for your Y features and Y labels. I'm doing that in order to show the whole calculation step by step so that next time you can do it yourself.

根据您提供的所有信息,看来您已经拥有

Based on all the information you have provided, it seems that you have

  1. 具有2层的NN.
  2. 第一层是标准化层
  3. 第二层是致密层
  4. 输入张量中有2个特征,单个输出中有1个

让我们从规范化层开始.对于归一化层,它是奇怪的".我认为使用术语重量".重量基本上是均值和方差,这些均值和方差将应用于每个输入以标准化数据.

Let's start with the normalization layer. For normalization layers, it is kind "strange" in my opinion to use the term "weight". The weights are basically the mean and variance which will be applied to each input in order to normalize the data.

我将2个输入特征称为x0和x1

I wil call the 2 input features x0 and x1

如果运行我的代码(这是包含固定数据的代码),则会看到归一化层的权重为

if you run my code (which is your code with my fixed data), you will see that the weights for the normalization layer are

[5.4.6][5.4 11.24]

[5. 4.6] [ 5.4 11.24]

这意味着[x0 x1]列的均值为[5.4.6],方差​​为[5.4 11.24]

It means that the means for your [x0 x1] columns are [5. 4.6] and the variances are [5.4 11.24]

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

Can we verify that? Yes, we can. Let's check for x0.

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

如您所见,它与权重"匹配.归一化层.

As you can see, it matches the "weights" of the normalization layer.

当数据通过归一化层推送时,每个值将基于x'=(x-均值)/stddev(stddev,不是方差)

As data is pushed thru the normalization layer, each value will be normalized based on x' = (x-mean)/stddev ( stddev, not variance )

您可以通过对数据进行归一化来检查.在代码中,如果您运行这两行

You can check that by applying the normalization to the data. In the code, if you run this 2 lines

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   ]]
 

让我们验证第一个数字.

Let's verify the first number.

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

在这一点上,我们应该能够为归一化层编写方程式

At this point, we should be able to write the equations for the normalization layer

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

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

Now, these 2 outputs will be inject in the next layer. Remember, you have a Dense layer and therefore it is fully connected. It means that both values will be inject in the single neuron.

这些行显示了密集"层的权重和偏差值.

These lines show the value of both weights and bias for the Dense layer.

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]'以包括权重.

A neuron multiplies each input by a given weight, adds all results with the bias. Let's modify y[0]' and y[1]' to include the weights.

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

我们很接近,我们只需要对这2点求和并加上偏差

We are close, we just need to sum up these 2 and add the bias

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

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

Since you don't have an activation function, we can stop here.

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

How can we verify if the formula is right? Let's use the model to predict the label for a random input and see if it matches the result we get when we put the same values in our equation.

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

First, let's run a model prediction for [4,5]

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

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

Now, let's plug the same inputs to our equation

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

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

y'= 0.332911

y' = 0.332911

看来我们很好.我降低了一些精确度只是为了让我的生活更轻松.

It seems that we are good. I cut some precisions just be make my life easier.

这是您的模型的功能.只需用您的电话号码代替我的电话号码即可.

Here is the function for your model. Just replace my numbers with your numbers.

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

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

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

And here is the code. I have also added tensorboard so you can verify yourself what I have said here.

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]] )) 

这篇关于如何在Tensorflow中打印出多元线性回归模型正在使用的方程式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆