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

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

我有两个数组 "train_vol""train_time"形状 (164,6790) 和一个数组 "train_cap" with shape(164,1)。我想像这样训练模型... 输入--> train_vol 和train_time 输出--> train_cap ..... 验证输入 --> val_vol,val_time 和验证输出 --> val_cap.. val_vol,[ 的形状=46=] 是 (42,6790) 而 val_cap 是 (42,1) 1]1

我正在尝试使用 model.fit() 来训练 model.I 尝试将 2 数组作为输入 给变量 x** 和 1个数组输出变量y。但是我收到如图所示的错误。2]2

文档说我可以给出数组列表作为输入。所以我已经尝试过,但出现以下错误,如图所示。谁能告诉我哪里做错了?3]3

尝试将参数传递为

model.fit(x=(train_vol, train_time), ..)

使用 () 而不是 []。背后的原因是模型无法决定您是要尝试提供 2 个样本的数据集还是两个并发数据集。第一种情况表示有两个数据集,后者表示有两个样本的数据集。

您可以使用函数 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

您可以使用上述语法指定图层之间的连接。您的模型可以有 2 个以上的输入。使用以上代码构建的模型如下所示。

请注意,输入层中的 136 代表您各自数据中的特征。

要训练模型,您可以使用以下语法:

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)]
)

这里train_data和new_train_data是两个独立的数据实体。
注意:您也可以传递列表而不是字典,但字典在编码方面更具可读性。

有关函数​​ API 的更多信息。你可以检查这个link:Functional API