【问题标题】:Layer was called with an input that isn't a symbolic tensor. Received type `Sequential`使用不是符号张量的输入调用图层。接收到的类型`Sequential`
【发布时间】:2020-11-30 00:18:51
【问题描述】:

我必须输入我想合并到一个网络中的输入,所以它应该是这样的:

input1    input2  
   |        | 
   |        |
 hidden    hidden
     Merged

我试过这段代码:

b1_part = Sequential()
b1_part.add(Dense(units=b1_X_train.shape[1],activation='tanh', activity_regularizer=regularizers.l2(1e-2)))
b1_part.add(Dropout(0.5))
b1_part.add(Dense(units=b1_X_train.shape[1]/4,activation='tanh', activity_regularizer=regularizers.l2(1e-2)))


b2_part = Sequential()
b2_part.add(Dense(units=b2_X_train.shape[1],activation='tanh', activity_regularizer=regularizers.l2(1e-2)))
b2_part.add(Dropout(0.5))
b2_part.add(Dense(units=b2_X_train.shape[1]/4,activation='tanh', activity_regularizer=regularizers.l2(1e-2)))

result = Concatenate(axis=1)([b1_part, b2_part])

optimizer = Adagrad()
result.compile(optimizer=optimzier, loss=BinaryFocalLoss(gamma=2),
               metrics=['accuracy'])

但是得到了:

ValueError: Layer concatenate_10 was called with an input that isn't a symbolic tensor. Received type: <class 'tensorflow.python.keras.engine.sequential.Sequential'>. Full input: [<tensorflow.python.keras.engine.sequential.Sequential object at 0x7fb1cbf97048>, <tensorflow.python.keras.engine.sequential.Sequential object at 0x7fb1cbeb5358>]. All inputs to the layer should be tensors.

知道为什么吗?

【问题讨论】:

    标签: python tensorflow keras deep-learning concatenation


    【解决方案1】:

    首先,请记住指定序列模型的输入形状。

    然后你可以组合你的顺序模型并以这种方式定义一个完整的模型:

    b1_part = Sequential()
    b1_part.add(Dense(units=100,activation='tanh', input_shape=(100,)))
    b1_part.add(Dropout(0.5))
    b1_part.add(Dense(units=25,activation='tanh'))
    
    b2_part = Sequential()
    b2_part.add(Dense(units=200,activation='tanh', input_shape=(200,)))
    b2_part.add(Dropout(0.5))
    b2_part.add(Dense(units=50,activation='tanh'))
    
    result = Concatenate(axis=1)([b1_part.output, b2_part.output])
    result = Dense(1, activation='sigmoid')(result)
    
    model = Model([b1_part.input, b2_part.input], result)
    model.compile('adam', 'binary_crossentropy', metrics='accuracy')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-29
      • 2019-06-04
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多