张量流中神经网络的多个输入?

时间:2020-12-31 08:08:36

标签: python tensorflow input neural-network

我正在尝试使用 tensorflow 实现强化学习算法来训练代理。

我希望我的神经网络有 2 个不同的输入,第一个是具有形状 (4,160,120,1) 的 4 个图像的图像堆栈,然后是一个包含 10 个条目的一维数组。

我试着像我只用一个输入那样做,用两个输入定义我的神经网络的调用函数并运行我的程序。当函数 train_on_batch 被执行时,它导致了一个错误,我收到了以下消息,其中 states2 是我的第二个输入:

ValueError: 传递给 train_on_batch 的模型只能将 trainingcall 中的第一个参数作为位置参数,发现:['state2']

那么如何为我的神经网络使用两个输入并且仍然能够使用 train_on_batch?

2 个答案:

答案 0 :(得分:0)

您需要将输入连接到单个 npy 数组中,或者使用数组列表,如运行 tf.keras.Model.train_on_batch() 函数时所述的 in the documentation

答案 1 :(得分:0)

您可以创建一个具有两个输入的模型,例如

input1=tf.keras.Input( shape= .....
# add layers here to process input 1 as you wish 
# last layer should be a Flatten layer or GlobalMaxPooling Layer
out1=tf.keras.layers.Flatten()(previous layer)
input2= tf.keras.Input ( shape=....
add layers  to process input 2
# last layer should be a Flatten layer or GlobalMaxPooling Layer
out2=tf.keras.layers.Flatten()(previous layer)
# now concatenate the outputs out1 and out2
concatted = tf.keras.layers.Concatenate()([out1, out2])
# now you can add more layers here to process the concatted output as you wish
#  last layer should be your output layer
output=Dense (number of classes, activation='softmax;)(previous layer output)
model=keras.Model(inputs=[input1,input2], outputs=output)
#  then compile your model

    
相关问题