【问题标题】:TensorFlow model with multiple inputs and single output具有多输入和单输出的 TensorFlow 模型
【发布时间】:2021-08-20 19:10:36
【问题描述】:

我是 的新手。尝试开发具有多个输入和单个输出的简单模型。如果有人可以帮助我,我将不胜感激。我发现以下代码可能有效,但无效。另外,在这种情况下如何传递预测参数?

trainx1 = np.array([-1, 0, 1, 2, 3, 4], dtype=float)
trainx2 = np.array([-1, 0, 1, 2, 3, 4], dtype=float)
labely1 = np.array([-2, 0, 2, 4, 6, 8], dtype=float)

x1 = Input(shape =(1,))
x2 = Input(shape =(1,))
input_layer = concatenate([x1,x2])
hidden_layer = Dense(units=4, activation='relu')(input_layer)
prediction = Dense(1, activation='linear')(hidden_layer)

model = Model(inputs=[x1, x2], outputs=prediction)
model.compile(loss="mean_squared_error", 
              optimizer="adam", metrics=['accuracy'])

model.fit([trainx1, trainx2], labely1, 
          epochs=100, batch_size=1, verbose=2, shuffle=False)
model.summary()

【问题讨论】:

  • 这段代码遇到了什么错误?
  • 谢谢 Inna!实际上,我在模型中看到零精度。两个输入和输出之间的关系只是两个输入的总和。所以模型应该能够达到100%的准确率。同样当我使用 model.predict([5,5]) 时。它抛出错误“ValueError:层模型需要 2 个输入,但它接收到 1 个输入张量。收到的输入:[]”跨度>

标签: tensorflow python tensorflow machine-learning keras deep-learning


【解决方案1】:

首先,accuracy 指标对回归任务意义不大,更适合分类问题。相反,对于回归,可以使用maer2 分数。仅供参考,从以下链接中,您可以找到 r2 scoretfa.metrics.RSquare 实现。


让我们构建一个模型,该模型将对两个整数输入进行简单求和。为此,我们首先创建一个虚拟数据集。

import numpy as np 
import tensorflow as tf 

inp1 = np.array([i-1 for i in range(3000)], dtype=float)
inp2 = np.array([i-1 for i in range(3000)], dtype=float)
tar = np.array([(input[0] + input [1]) \
                for input in zip(inp1, inp2)], dtype=float)

inp1.shape, tar.shape 
((3000,), (3000,))

inp1[:5], tar[:5]
(array([-1.,  0.,  1.,  2.,  3.]), array([-2.,  0.,  2.,  4.,  6.]))

型号

import tensorflow as tf 
from tensorflow.keras import Input  
from tensorflow.keras import Model 
from tensorflow.keras.layers import *

x1 = Input(shape =(1,))
x2 = Input(shape =(1,))

input_layer = concatenate([x1,x2])
hidden_layer = Dense(units=4, activation='relu')(input_layer)

prediction = Dense(1, activation='linear')(hidden_layer)
model = Model(inputs=[x1, x2], outputs=prediction)

编译并运行

model.compile(loss="mean_squared_error", 
              optimizer='adam', 
              metrics=['mae'])
model.fit([inp1, inp2], tar, epochs=300, 
          batch_size=32, verbose=2)
Epoch 1/300
94/94 - 0s - loss: 10816206.0000 - mae: 2846.8416
Epoch 2/300
94/94 - 0s - loss: 7110172.5000 - mae: 2301.0493
Epoch 3/300
94/94 - 0s - loss: 3619359.5000 - mae: 1633.6898
....
....
Epoch 298/300
94/94 - 0s - loss: 9.3060e-07 - mae: 7.4665e-04
Epoch 299/300
94/94 - 0s - loss: 9.3867e-07 - mae: 7.5240e-04
Epoch 300/300
94/94 - 0s - loss: 7.2407e-07 - mae: 6.6270e-04

推理

模型需要两个输入,形状分别为 (None, 1)(None, 1)。因此,我们为每个输入扩展了一个批处理维度 (expand_dims),如下所示。

model([np.expand_dims(np.array(4), 0), 
       np.expand_dims(np.array(4), 0)]).numpy()
array([[7.998661]], dtype=float32)

model([np.expand_dims(np.array(10), 0), 
       np.expand_dims(np.array(10), 0)]).numpy()
array([[19.998667]], dtype=float32)

model([np.expand_dims(np.array(50), 0), 
       np.expand_dims(np.array(40), 0)]).numpy()
array([[88.77226]], dtype=float32)

【讨论】:

  • 非常感谢您抽出宝贵时间。实际上我在这一行得到错误 (array([-1., 0., 1., 2., 3.]), array([-2., 0., 2., 4., 6.])) .错误是 TypeError:array() 参数 1 必须是 unicode 字符,而不是列表。
  • 实际上我删除了那行代码并执行了你的代码。它仍然可以正常工作,但模型无法正确预测。对于这个 print(model([np.expand_dims(np.array(18), 0), np.expand_dims(np.array(6), 0)]).numpy()),我得到 [[33.084637]]。
  • 在这一点上这是正常的,它只是指过度拟合的东西。为了使建模策略更强大,我们可能需要使用验证数据集或使用各种技术来防止这种情况。这实际上超出了您当前问题的范围。仅供参考,如果您不了解模型过拟合、欠拟合等,请自行搜索。
  • 非常感谢您抽出宝贵的时间。我从你身上学到了很多。在你的帮助下,我能够做我想做的事。再次感谢你!!!
  • 很高兴为您提供帮助。 :)
猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 2016-07-08
相关资源
最近更新 更多