首页 > 解决方案 > 具有多个输入参数和多个输出参数的神经网络

问题描述

我已经开始处理 TensorFlow。在 Udacity 的一次练习中,我学会了创建一个可以将摄氏温度转换为华氏温度的神经网络。为了训练神经网络,给出了 3 个示例:

celsius_q = np.array([-40, -10, 0, 8, 15, 22, 38], dtype=float)
fahrenheit_a = np.array([-40, 14, 32, 46, 59, 72, 100], dtype=float)

现在我想处理一个更复杂的问题。我有一个以 3 个测量系列为例的测量系列。对于每个测量系列,我有 4 个输入参数(输入)和 3 个相应的测量值(输出)。

数据

我现在想创建一个神经网络并为其提供 3 个测量系列进行训练。然后我想输入一组输入参数,神经网络应该给我输出。

我从 Udacity 的练习中获取了现有代码,并尝试将其转换为我的用例。

不幸的是,我还没有取得成功。代码在这里:

进口

import tensorflow as tf
import numpy as np

设置训练数据

inputMatrix = np.array([(100,230,0.95,100),
                        (200,245,0.99,121),
                        ( 40,250,0.91,123)],dtype=float)
outputMatrix = np.array([(120, 5,120),
                         (123,24,100),
                         (154, 3,121)],dtype=float)
for i,c in enumerate(inputMatrix):
print("{}Input Matrix={}Output Matrix".format(c,outputMatrix[i]))

创建模型

l0 = tf.keras.layers.Dense(units = 4, input_shape = [4])
l1 = tf.keras.layers.Dense(units = 64)
l2 = tf.keras.layers.Dense(units = 128)
l3 = tf.keras.layers.Dense(units = 3)

model = tf.keras.Sequential([l0,l1,l2,l3])

编译模型

model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(0.1))

训练模型

history = model.fit(inputMatrix,outputMatrix,epochs=500,verbose=False)
print("Finished training the model!")

显示训练统计

import matplotlib.pyplot as plt
plt.xlabel('Epoch Number')
plt.ylabel('Loss Magnitude')
plt.plot(history.history['loss'])

使用模型预测值

print(model.predict([120,260,0.98,110]))`

我认为一个问题是在“设置训练数据”下,3 个测量系列没有正确实施。

如果我执行“训练模型”下的代码,训练很快就完成了,这种复杂的任务不应该是这样。

我希望有人可以帮助我逐步学习并解决这个问题。

标签: pythontensorflowneural-network

解决方案


我在代码中看到的唯一问题是预期输入的形状是 1,4

print(model.predict(np.array([120,260,0.98,110]).reshape(1,4)))

推荐阅读