Search code examples
pythonarraystensorflowtraining-datamodel-fitting

Attribute Error: 'List' object has no attribute 'shape' . Error while trying to train the model with multiple features( multiple arrays)


I have two arrays "train_vol" and "train_time" with shape (164,6790) and one array "train_cap" with shape(164,1). I want to train the model like this... inputs--> train_vol and train_time output--> train_cap .....validation inputs --> val_vol,val_time and validation output --> val_cap....shape of val_vol,val_time is (42,6790) and val_cap is (42,1) 1]1

I am trying to use model.fit() to train the model.I tried giving 2 arrays as input to variable x** and 1 array to output variable y. But I am getting an error as shown.2]2

The document says I can give list of arrays as input. So i have tried but i am getting the following error as shown in the picture. Can Anyone let me know where I have done a mistake?3]3


Solution

  • You can create a model which takes multiple inputs as well as multiple output using functional API.

    def create_model3():
        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
    

    You can specify the connection between your layers using the syntax above. Your model can have more than 2 inputs. The model constructed using above code looks like this.

    Note that 13 and 6 in the Input layers represent features in your respective data.

    Model Visualization

    For training the model you can use the following syntax:

    history = model.fit(
        x = {'I1':train_data, 'I2':new_train_data}, 
        y = {'O1':train_labels, 'O2': new_train_labels},
        batch_size = 32,
        epochs = 10,
        verbose = 1,
        callbacks = None,
        validation_data = [(val_data,new_val_data),(val_labels, new_val_labels)]
    )
    

    Here train_data and new_train_data are two separate data entities.
    Note: You can also pass List instead of dictionary but the dictionary is more readable in terms of coding.

    For more information on Functional API. You can check this link: Functional API