【问题标题】:Multiple Inputs for neural network in tensorflow?张量流中神经网络的多个输入?
【发布时间】:2020-12-31 08:08:36
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: python tensorflow input neural-network


    【解决方案1】:

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

    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
    
        
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2017-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多