使用Tensorflow.keras创建简单模型
1. 使用keras的API配置训练
训练代码:
import tensorflow as tf
from tensorflow import keras
import numpy as np
from matplotlib import pyplot as plt
np.random.seed(42) # 设置numpy随机数种子
tf.random.set_seed(42) # 设置tensorflow随机数种子
# 生成训练数据
x = np.linspace(-1, 1, 100)
y = x * x + 1 + np.random.rand(100)*0.1 # y=x^2+1 + 随机噪声
x_train = np.expand_dims(x, 1) # 将一维数据扩展为二维
y_train = np.expand_dims(y, 1) # 将一维数据扩展为二维
plt.plot(x, y, '.') # 画出训练数据
# 创建模型:输入层1个节点,隐藏层10个节点,输出层1个节点
model = keras.Sequential()
model.add(keras.Input(shape=(1,)))
model.add(keras.layers.Dense(10, keras.activations.relu))
model.add(keras.layers.Dense(1))
# 打印模型架构
print(model.summary())
# 模型配置(配置优化器,损失函数等)
model.compile(optimizer='sgd', # 优化器
loss='mse') # 损失函数
# 模型训练
model.fit(x_train,
y_train,
epochs=1000) # 迭代次数
# 预测
y_pre = model.predict(x_train)
# 画出预测值
plt.plot(x, y_pre)
plt.show()
打印结果:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 10) 20
_________________________________________________________________
dense_1 (Dense) (None, 1) 11
=================================================================
Total params: 31
Trainable params: 31
Non-trainable params: 0
_________________________________________________________________
2. 手动进行前向和反向传播
训练代码:
import tensorflow as tf
from tensorflow import keras
import numpy as np
from matplotlib import pyplot as plt
np.random.seed(42) # 设置numpy随机数种子
tf.random.set_seed(42) # 设置tensorflow随机数种子
# 生成训练数据
x = np.linspace(-1, 1, 100)
x = x.astype('float32')
y = x * x + 1 + np.random.rand(100)*0.1 # y=x^2+1 + 随机噪声
x_train = np.expand_dims(x, 1) # 将一维数据扩展为二维
y_train = np.expand_dims(y, 1) # 将一维数据扩展为二维
plt.plot(x, y, '.') # 画出训练数据
def create_model():
inputs = keras.Input((1,))
x = keras.layers.Dense(10, activation='relu')(inputs)
outputs = keras.layers.Dense(1)(x)
model = keras.Model(inputs=inputs, outputs=outputs)
return model
model = create_model() # 创建一个模型
loss_fn = keras.losses.MeanSquaredError() # 定义损失函数
optimizer = keras.optimizers.SGD() # 定义优化器
epochs = 1000 # 训练次数
for epoch in range(epochs):
with tf.GradientTape() as tape:
y_pred = model(x_train, training=True) # 前向传播,注意不要忘了training=True
loss = loss_fn(y_train, y_pred) # 计算损失
grads = tape.gradient(loss, model.trainable_variables) # 计算梯度
optimizer.apply_gradients(zip(grads, model.trainable_variables)) # 使用优化器进行反向传播
print('epoch:', epoch+1, '\t', 'loss:', loss.numpy()) # 打印训练信息
# 预测
y_pre = model.predict(x_train)
# 画出预测值
plt.plot(x, y_pre.squeeze())
plt.show()