【问题标题】:Neural network with several input parameters and several output parameters具有多个输入参数和多个输出参数的神经网络
【发布时间】:2019-12-01 13:40:51
【问题描述】:

我已经开始处理 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 个测量系列没有正确实施。

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

希望有人能帮我一步步学习,解决这个问题。

【问题讨论】:

  • 输出值应该是多少?
  • 我收到134.3495 4.957284 141.96487 。这是正确的吗?
  • 这些值不是真实值。这只是一个例子。您是如何让软件工作的?

标签: python tensorflow neural-network


【解决方案1】:

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

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

【讨论】:

  • 谢谢。有用!现在我将检查是否通过合理的例子得到合理的值。
  • 还有一个问题:我解决问题的方法好吗?还是我的方法不太适合这个问题,有更好的方法吗?
猜你喜欢
  • 1970-01-01
  • 2017-11-16
  • 2016-03-13
  • 2017-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
相关资源
最近更新 更多