标签:put rand width otl 预测 keras imp sha 技术
训练代码:
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
_________________________________________________________________
标签:put rand width otl 预测 keras imp sha 技术
原文地址:https://www.cnblogs.com/amazingter/p/14226171.html