【问题标题】:How to make x*y with simple deep learning(linear regression)如何通过简单的深度学习(线性回归)制作 x*y
【发布时间】:2021-07-08 15:20:14
【问题描述】:

为了我将来的使用,我想测试多变量多层感知器。

为了测试,我做了一个简单的python程序。

这是代码。

import tensorflow as tf
import pandas as pd
import numpy as np
import random

input = []
result = []

for i in range(0,10000):
    x = random.random()*100
    y = random.random()*100
    input.append([x,y])
    result.append(x*y)


input = np.array(input,dtype=float)
result = np.array(result,dtype = float)

activation_func = "relu"
unit_count = 256

model = tf.keras.models.Sequential([
tf.keras.layers.Dense(1,input_dim=2),
tf.keras.layers.Dense(unit_count,activation=activation_func),
tf.keras.layers.Dense(unit_count,activation=activation_func),
tf.keras.layers.Dense(unit_count,activation=activation_func),
tf.keras.layers.Dense(unit_count,activation=activation_func),
tf.keras.layers.Dense(1)])

model.compile(optimizer="adam",loss="mse")


model.fit(input,result,epochs=10)

predict_input = np.array([[7,3],[5,4],[8,8]]);

print(model.predict(predict_input))

我用这段代码试过了,结果不好。损失值似乎并没有在某个时候降低。

我还尝试了较小的 x 和 y。数字越大,模型就越不准确。

我改变了激活函数,增加了更密集的层并增加了单元的数量,但并没有变得更好。

【问题讨论】:

    标签: python keras deep-learning linear-regression tensorflow2.0


    【解决方案1】:

    神经网络无法自行调整(无需额外训练)以适应不同的域,这意味着您应该在一个域上训练并在同一个域上运行推理。

    在图像中,我们通常只是将输入图像从 [0,255] 缩放到 [-1,1] 并让网络从这个范围内的值中学习(在推理过程中,我们总是将输入值重新缩放到 [ -1,1] 范围)。

    为了解决您的任务,您应该将问题提交到受限域。

    在实践中,如果您有兴趣训练模型仅用于乘以正数,您可以将它们压缩在 [0,1] 范围内,并且由于该范围内的值相乘总是会给出相同的输出值范围。

    我稍微修改了你的代码,并在源代码中添加了一些 cmets。

    import random
    
    import numpy as np
    import pandas as pd
    import tensorflow as tf
    
    input = []
    result = []
    
    # We want to train our network to work in a fixed domain
    # the [0,1] range.
    
    # Let's also increase the training set -> more data is always better
    for i in range(0, 100000):
        x = random.random()
        y = random.random()
        input.append([x, y])
        result.append(x * y)
        print(input, result)
        sys.exit()
    
    
    input = np.array(input, dtype=float)
    result = np.array(result, dtype=float)
    
    activation_func = "relu"
    unit_count = 256
    
    # no need for a tons of layers
    model = tf.keras.models.Sequential(
        [
            tf.keras.layers.Dense(unit_count, input_dim=2, activation=activation_func),
            tf.keras.layers.Dense(unit_count, activation=activation_func),
            tf.keras.layers.Dense(1, use_bias=False),
        ]
    )
    
    model.compile(optimizer="adam", loss="mse")
    model.fit(input, result, epochs=10)
    
    # Bring our input values in the [0,1] range
    max_value = 10
    predict_input = np.array([[7, 3], [5, 4], [8, 8]]) / max_value
    print(predict_input)
    
    # Back to the original domain
    # Multiply by max_value**2 is required since the multiplication
    # for a number in [0,1] it's the same of a division
    print(model.predict(predict_input) * max_value ** 2)
    

    示例输出:

    [[0.7 0.3]
     [0.5 0.4]
     [0.8 0.8]]
    [[21.04468 ]
     [20.028284]
     [64.05521 ]]
    

    【讨论】:

    • 最后一个 Dense 层中的 use_bias=False 是否必不可少?
    • 不,不是,你可以在最后一层有偏差,尽管它不会有太大变化。我的插管建议执行乘法时的加法项应该为零(这就是我消除偏差的原因)
    • 层数有什么作用?它与拥有大量单位数量有什么不同?
    猜你喜欢
    • 2019-08-12
    • 1970-01-01
    • 2018-01-26
    • 2019-04-19
    • 2020-06-25
    • 2019-08-01
    • 2017-03-30
    • 2017-01-08
    • 2016-03-31
    相关资源
    最近更新 更多