Keras训练模型找到两个数字的和

时间:2019-09-07 13:07:09

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

尝试实现一个非常简单的训练模型,以使用张量流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]]

有人可以帮助我改善我的训练模型和准确性吗?预先感谢。

1 个答案:

答案 0 :(得分:0)

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

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.]]
相关问题