如何通过简单的深度学习(线性回归)生成x*y

nvbavucw  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(354)

为了将来的使用,我想测试多变量多层感知器。
为了测试它,我编写了一个简单的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。这使得模型不准确,数字更大。
我改变了激活函数,增加了密度层,增加了单位数量,但效果并没有变好。

2guxujil

2guxujil1#

神经网络无法将自身(无需额外训练)适应不同的领域,这意味着您应该在一个领域上进行训练,并在同一领域上运行推理。
在图像中,我们通常只是将输入图像从[0255]缩放到[-1,1],并让网络从该范围内的值中学习(在推断过程中,我们总是将输入值重新缩放到[-1,1]范围内)。
为了解决您的任务,您应该将问题带到受限域。
实际上,如果您只想训练正数相乘的模型,您可以在[0,1]范围内挤压它们,并且由于此范围内的值相乘总是在相同范围内产生输出值。
我稍微修改了您的代码,并在源代码中添加了一些注解。

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

相关问题