【问题标题】:How to print out equation that multiple linear regression model is using in Tensorflow?如何打印出多元线性回归模型在 Tensorflow 中使用的方程?
【发布时间】:2020-12-30 15:01:11
【问题描述】:

对于 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)

【问题讨论】:

    标签: python tensorflow math linear-regression


    【解决方案1】:

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

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

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

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

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

    我将两个输入特征称为 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 层,因此它是完全连接的。这意味着两个值都将注入单个神经元中。

    这些行显示了 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

    这是代码。我还添加了 tensorboard,以便您可以验证我在这里所说的内容。

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

    【讨论】:

    • 非常感谢!但是当我打印出权重和偏差时,我得到了以下输出:
    • 权重:[8.89 11.5] 偏差:[321.69 357.53]
    • 如何将其转换为多元线性回归方程
    • 嗯,这似乎是正确的。这些是模型的 2 个权重和偏差。没有代码,我不知道您的模型的架构,因此我无法编写方程式。
    • 刚刚添加了代码,感谢您对我的包容:)
    猜你喜欢
    • 2016-09-06
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 2019-02-23
    • 1970-01-01
    • 2021-10-13
    相关资源
    最近更新 更多