可以实现输入三个数预测输出三个数的神经网络模型实例
时间: 2023-05-25 11:03:07 浏览: 84
这里提供一个简单的神经网络模型实例,实现输入三个数预测输出三个数的功能。
``` python
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# 创建训练数据集
X_train = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15]])
y_train = np.array([[4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
# 创建神经网络模型
model = Sequential([
Dense(64, activation='relu', input_shape=(3,)),
Dense(64, activation='relu'),
Dense(3, activation='linear')
])
# 编译模型
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
# 训练模型
model.fit(X_train, y_train, epochs=500, verbose=0)
# 测试模型
X_test = np.array([[16, 17, 18], [19, 20, 21], [22, 23, 24]])
y_test = np.array([[19, 20, 21], [22, 23, 24], [25, 26, 27]])
test_loss, test_mae = model.evaluate(X_test, y_test, verbose=0)
print("Test MAE: {:.3f}".format(test_mae))
# 结果预测
X_new = np.array([[25, 26, 27], [28, 29, 30], [31, 32, 33]])
y_pred = model.predict(X_new)
print("Predicted result: \n", y_pred)
```
在这个模型实例中,我们创建了一个包含三个 Dense 层的神经网络,输入层包含三个神经元,输出层也包含三个神经元。为了训练模型,我们使用了输入和输出的训练数据集,并使用了 Adam 优化器和均方误差(MSE)损失函数来编译模型。然后,我们使用测试数据集评估了模型的性能,并使用预先提供的三个新输入来进行预测输出。