Keras 的写法
# -*- coding: utf-8 -*-
import numpy as np
import tensorflow as tf
import tensorflow.keras.layers as layers
# 训练数据为-100到100内加法
X = np.random.randint(-100, 100, (10000, 2))
Y = X.sum(axis=1)
# 验证数据为1000到10000内的加法,可以说验证数据和训练数据分布完全不同了,可以看出模型是否学到了加法
val_X = np.random.randint(1000, 10000, (2000, 2))
val_Y = val_X.sum(axis=1)
# 构建并训练模型,损失函数选择MAE,平均绝对误差
model = tf.keras.Sequential()
# 第一层: 2 x 10 , 激励函数:relu
model.add(layers.Dense(10, activation='relu'))
# 第二层: 10 x 10 , 激励函数:relu
model.add(layers.Dense(10, activation='relu'))
# 输出层: 10 x 1
model.add(layers.Dense(1, activation=None))
model.build((None, 2))
# 优化器: adam, 步长 0.01, 损失函数:mae
model.compile(optimizer=tf.keras.optimizers.Adam(0.01), loss='mae')
# 开始训练
model.fit(X, Y, epochs=20, verbose=1, validation_data=(val_X, val_Y))
# 验证 a + b
a = 11
b = 12
result = model.predict(np.array([[a,b]]))
print("{}+{}={}".format(a,b,result))
经过 20 次训练迭代得出的 11 + 12 的结果
Train on 10000 samples, validate on 2000 samples
Epoch 1/20
10000/10000 [==============================] - 1s 78us/sample - loss: 5.6489 - val_loss: 12.3674
Epoch 2/20
10000/10000 [==============================] - 0s 28us/sample - loss: 0.8179 - val_loss: 71.6364
Epoch 3/20
10000/10000 [==============================] - 0s 29us/sample - loss: 0.6928 - val_loss: 53.9322
...
10000/10000 [==============================] - 0s 28us/sample - loss: 0.4014 - val_loss: 37.9406
11+12=[[23.045128]]
tensorflow 的写法
# -*- coding: utf-8 -*-
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import numpy as np
import matplotlib.pyplot as plt
def add_layer(inputs, in_size, out_size, activation_function=None): #添加一个层
w = tf.Variable(tf.random_normal([in_size, out_size])) #从正态分布中生成随机值
b = tf.Variable(tf.zeros([1, out_size]) + 0.1)
Wx_plus_b = tf.matmul(inputs, w) + b
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
## 训练数据为-100到100内加法, 10000 x 2 的矩阵
x_data = np.random.randint(-100, 100, (10000,2))
print(x_data.shape)
y_data = x_data.sum(axis=1)[:, np.newaxis]
print(y_data.shape)
xs = tf.placeholder(tf.float32, [None, 2])
ys = tf.placeholder(tf.float32, [None, 1])
# 第一层: 2 x 10 , 激励函数:relu
layer1 = add_layer(xs, 2, 10, activation_function=tf.nn.relu)
# 第二层: 10 x 10 , 激励函数:relu
layer2 = add_layer(layer1, 10, 10, activation_function=tf.nn.relu)
# 输出层: 10 x 1
prediction = add_layer(layer2, 10, 1, activation_function=None)
# 损失函数:mae
loss = tf.reduce_mean(tf.reduce_sum(tf.abs(ys-prediction), reduction_indices=[1]))
# 优化器: adam, 步长 0.01
train_step = tf.train.AdamOptimizer(0.01).minimize(loss)
# 训练次数
epoch = 20
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
# 开始训练
for i in range(epoch):
print("Epoch {}/{}".format(i+1,epoch))
feed_dict = {xs:x_data,ys:y_data}
sess.run(train_step,feed_dict=feed_dict)
print("loss = {}".format(sess.run(loss,feed_dict=feed_dict)))
# 验证 a + b
a = 11
b = 12
result = sess.run(prediction,feed_dict={xs:[[a,b]]})
print("{}+{}={}".format(a,b,result))
经过 20 次训练迭代得出的 11 + 12 的结果
Epoch 1/20
loss = 75.47555541992188
Epoch 2/20
loss = 64.27383422851562
Epoch 3/20
loss = 53.39580154418945
...
Epoch 20/20
loss = 11.570183753967285
11+12=[[23.427382]]