AssertionError: Could not compute output Tensor

前端 未结 1 797
半阙折子戏
半阙折子戏 2021-01-18 12:07

I am trying to build a model that takes multiple inputs and multiple outputs using a functional API. I followed this to create the code.

def create_model_mul         


        
相关标签:
1条回答
  • 2021-01-18 12:45

    you have to provide validation_data in the correct format (like your train). you have to pass 2 input data and 2 targets... you are passing only one

    this is a dummy example

    def create_model_multiple():
    
        input1 = tf.keras.Input(shape=(13,), name = 'I1')
        input2 = tf.keras.Input(shape=(6,), name = 'I2')
        hidden1 = tf.keras.layers.Dense(units = 4, activation='relu')(input1)
        hidden2 = tf.keras.layers.Dense(units = 4, activation='relu')(input2)
        merge = tf.keras.layers.concatenate([hidden1, hidden2])
        hidden3 = tf.keras.layers.Dense(units = 3, activation='relu')(merge)
        output1 = tf.keras.layers.Dense(units = 2, activation='softmax', name ='O1')(hidden3)
        output2 = tf.keras.layers.Dense(units = 2, activation='softmax', name = 'O2')(hidden3)
        model = tf.keras.models.Model(inputs = [input1,input2], outputs = [output1,output2])
        model.compile(optimizer='adam',
                      loss='sparse_categorical_crossentropy',
                      metrics=['accuracy'])
        return model
    
    
    x1 = np.random.uniform(0,1, (190,13))
    x2 = np.random.uniform(0,1, (190,6))
    val_x1 = np.random.uniform(0,1, (50,13))
    val_x2 = np.random.uniform(0,1, (50,6))
    
    y1 = np.random.randint(0,2, 190)
    y2 = np.random.randint(0,2, 190)
    val_y1 = np.random.randint(0,2, 50)
    val_y2 = np.random.randint(0,2, 50)
    
    
    model = create_model_multiple()
    
    history = model.fit({'I1':x1, 'I2':x2},
                        {'O1':y1, 'O2': y2},
                        validation_data=([val_x1,val_x2], [val_y1,val_y2]), # <=========
                        epochs=100,
                        verbose = 1)
    
    0 讨论(0)
提交回复
热议问题