Keras错误:预期input_1具有3个维度,但数组的形状为(256326,3)

时间:2019-08-29 09:17:32

标签: python tensorflow keras deep-learning keras-layer

我正在尝试对多输出dnn进行建模。也使用kaggle信用卡data。因为我只是想测试,所以我的代码只能从三个方面学习。

我的代码:

4, 5, 6

错误:

df = pd.read_csv('creditcard.csv')

X = df.iloc[:, :-1].values
y = df.iloc[:, -1].values

X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.1, random_state=1)
temp = []
for x in X_train:
    temp.append(x[:3])
X_train = temp
temp = []
for x in X_test:
    temp.append(x[:3])
X_test = temp

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

inputs = keras.layers.Input(shape=(None, 3))
x = layers.Dense(16, activation='relu')(inputs)
x = layers.Dense(20, activation='relu')(x)
x = layers.Dropout(0.25)(x)
x = layers.Dense(16, activation='relu')(x)
a_prediction = layers.Dense(1, name='a')(x)
b_prediction = layers.Dense(16, activation='softmax', name='b')(x)
c_prediction = layers.Dense(1, activation='sigmoid', name='c')(x)
model = Model(inputs, [a_prediction, b_prediction, c_prediction])

model.compile(optimizer='rmsprop', loss={'a': mean_squared_error, 'b': categorical_crossentropy, 'c': binary_crossentropy}, loss_weights={'a': 0.25, 'b': 1., 'c': 10.})

model.fit(X_train, {'a': Y_train, 'b': Y_train, 'c': Y_train}, epochs=10, batch_size=64)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

“输入”层的shape参数不应包含批处理大小(link to doc)。将该行切换为inputs = keras.layers.Input(shape=(3, ))应该可以解决您的问题。

将来,您可以使用model.summary()方法查看图层的内部名称以及每个图层的预期输出形状。对于您当前的代码,将显示以下内容:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, None, 3)      0                                            
__________________________________________________________________________________________________
dense_1 (Dense)                 (None, None, 16)     64          input_1[0][0]                    
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, None, 20)     340         dense_1[0][0]                    
__________________________________________________________________________________________________
dropout_1 (Dropout)             (None, None, 20)     0           dense_2[0][0]                    
__________________________________________________________________________________________________
dense_3 (Dense)                 (None, None, 16)     336         dropout_1[0][0]                  
__________________________________________________________________________________________________
a (Dense)                       (None, None, 1)      17          dense_3[0][0]                    
__________________________________________________________________________________________________
b (Dense)                       (None, None, 16)     272         dense_3[0][0]                    
__________________________________________________________________________________________________
c (Dense)                       (None, None, 1)      17          dense_3[0][0]                    
==================================================================================================
Total params: 1,046
Trainable params: 1,046
Non-trainable params: 0
__________________________________________________________________________________________________

我们可以看到输入层(input_1,与堆栈跟踪中提到的相同)具有三个维度。