【问题标题】:Keras training model to find the sum of two numbersKeras训练模型求两个数之和
【发布时间】:2020-01-10 00:53:19
【问题描述】:

尝试使用 tensorflow keras 实现一个非常简单的训练模型来预测两个数字的总和。 我尝试使用单密集层。 我也尝试改变时代。 我也尝试将优化器更改为线性。 但是没有任何结果可以让我获得最好的准确性。

下面是我到目前为止尝试的代码和我得到的输出。

导入

import numpy as np
import tensorflow as tf
from random import randrange

生成训练数据

trainingInput = [[i, i + randrange(5000)] for i in range(1, 5000)]
trainingOutput = [(input [0] + input [1]) for input  in trainingInput ]

testInput = [[5, 5], [1, 9], [2, 5], [6, 3], [1, 4]]
testOutput = [10, 10, 7, 9, 5]

构建模型

model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(2,)))
model.add(tf.keras.layers.Dense(64, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(64, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(1))

编译模型

model.compile(optimizer='adam', loss=tf.keras.losses.mae, metrics=['mae'])

培训

model.fit(trainData, trainOutput, batch_size=5, epochs=50)

最终评估和预测

test_loss, test_acc = model.evaluate(testData, testOutput)
print("Test Accuracy : ", test_acc)
a = np.array([[3, 3000], [4, 5], [1,10], [2,10],[5,9], [4,10], [1,15]])
print(model.predict(a))

输出

[[2994.769   ]
[   8.959281]
[  10.961123]
[  11.956481]
[  13.944955]
[  13.947194]
[  15.949196]]

谁能帮我改进我的训练模型和准确性?提前致谢。

【问题讨论】:

  • 您可以尝试增加层数,每层中的单元数并训练更长的时间。
  • 好的 @Aditya Mishra 让我再添加两层,再添加一些没有单位的层,让我们看看。
  • 我不同意@Aditya Mishra - 这是一项微不足道的任务,具有 0 个隐藏层更合适。在这种情况下,模型将是:y = w1 * x1 + w2 * x2 + b,它应该学习 w1=w2=1 和 b=0。除此之外,当您打印“测试准确性”时,您实际上显示的是平均绝对误差(这就是 mae 的含义),所以越低越好。
  • model.add(tf.keras.layers.Dense(1, activation='linear', input_shape=(2,))), model.compile(optimizer=tf.keras.optimizers.SGD (学习率=0.2),损失=tf.keras.losses.mean_squared_error,指标=['mse'])。我也尝试过这种方式,但没有得到更好的结果
  • “没有得到更好的结果”是什么意思?您如何评价表现?

标签: python tensorflow keras linear-regression keras-layer


【解决方案1】:

您只需要较小的学习率 - 这样梯度就不会“跳跃”太多 - 意味着对权重进行更精细的调整。我还添加了一个简单的线性激活和一些反例,以使模型更通用:

trainingInput = np.array([[randrange(5000)-2500,  randrange(5000)-2500] for i in range(1, 50000)])
trainingOutput =  np.array([(input [0] + input [1]) for input  in trainingInput ])

inp = Input(shape=(2,), name='inp')
out = Dense(1, activation='linear',name='out')(inp)
model_sum=Model(inp,out)
opt = tf.keras.optimizers.Adam(learning_rate=0.0001)
model_sum.compile(loss='mean_absolute_error', optimizer=opt, metrics=['mae'])

model_sum.fit(trainingInput, trainingOutput, batch_size=100, epochs=50,verbose=2)

a = np.array([[3, 3000], [4, 5], [1,10], [2,10],[5,9], [4,10], [1,15]])
print(model_sum.predict(a))

输出:

Epoch 45/50
 - 0s - loss: 0.0166 - mae: 0.0166
Epoch 46/50
 - 0s - loss: 0.0184 - mae: 0.0184
Epoch 47/50
 - 0s - loss: 0.0164 - mae: 0.0164
Epoch 48/50
 - 0s - loss: 0.0151 - mae: 0.0151
Epoch 49/50
 - 0s - loss: 0.0161 - mae: 0.0161
Epoch 50/50
 - 0s - loss: 0.0161 - mae: 0.0161

 [[3002.9626  ]
 [   8.99987 ]
 [  10.999748]
 [  11.999768]
 [  13.999842]
 [  13.999809]
 [  15.999686]]

您还可以使用较低的学习率(10 倍)再次编译模型,并使其越来越准确。不要再次构建模型 - 只是优化器定义,编译和拟合:

opt = tf.keras.optimizers.Adam(learning_rate=0.000001)
model_sum.compile(loss='mean_absolute_error', optimizer=opt, metrics=['mae'])

model_sum.fit(trainingInput, trainingOutput, batch_size=100, epochs=50,verbose=2)

a = np.array([[3, 3000], [4, 5], [1,10], [2,10],[5,9], [4,10], [1,15]])
print(model_sum.predict(a))

Epoch 47/50
 - 0s - loss: 9.9272e-11 - mae: 9.9272e-11
Epoch 48/50
 - 0s - loss: 1.5182e-10 - mae: 1.5182e-10
Epoch 49/50
 - 0s - loss: 1.5532e-10 - mae: 1.5532e-10
Epoch 50/50
 - 0s - loss: 4.5265e-10 - mae: 4.5265e-10

 [[3003.]
 [   9.]
 [  11.]
 [  12.]
 [  14.]
 [  14.]
 [  16.]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-31
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 2020-07-16
    • 2019-08-27
    • 2017-09-28
    相关资源
    最近更新 更多