ValueError:检查目标时出错:预期dropout_82有3个维度,但得到的数组有形状(7,7500)

时间:2018-01-08 08:14:04

标签: python tensorflow keras

嗨,我必须实现一个cnn,我是Keras和Tensorflow的新手,所以如果我犯了错误,我会道歉。

这就是我的所作所为:

数据集是一个numpy数组(23,4800000),#number of audio tracks x #number of samples。

所以我将数据集拆分为火车(10,480000),验证(7,480000)和测试(6,480000)

沿着列的卷积过程,所以我必须重新整理输入:

X = np.expand_dims(train, axis=2)
Y = np.expand_dims(valid, axis=2)

第一部分cnn的代码是:

cnn = Sequential()
cnn.add(Conv1D(40, 80, input_shape=(4800000, 10)))
cnn.add(MaxPooling1D(pool_size=2))
cnn.add(Conv1D(40, 8000))
cnn.add(MaxPooling1D(pool_size=20))
cnn.add(Flatten())

cnn.summary()

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_1 (Conv1D)            (None, 4799921, 40)       32040     
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 2399960, 40)       0         
_________________________________________________________________
conv1d_2 (Conv1D)            (None, 2391961, 40)       12800040  
_________________________________________________________________
max_pooling1d_2 (MaxPooling1 (None, 119598, 40)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 4783920)           0         
=================================================================
Total params: 12,832,080
Trainable params: 12,832,080
Non-trainable params: 0
_______________________________

cnn.compile(loss='mean_squared_error', optimizer='adam')
cnn.fit(X,Y)

错误是:

ValueError: Error when checking input: expected conv1d_3_input to have shape (None, 4800000, 10) but got array with shape (4800000, 10, 1)

我真的不明白这意味着什么,请有人帮助我吗?

所以在这些日子里,我试图简化我的工作。

X_train,X_valid =(7,7500,1),7个曲目,7500个样本和1个频道

y_train,y_valid =(7,7500),对于7个轨道中的每一个,对应于任何样本中的概率值。

model = Sequential()

model.add(Conv1D(40, 80, activation='relu', input_shape=(7500,1)))
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(0.5))
model.add(Conv1D(40, 800 ,activation='relu'))
model.add(MaxPooling1D(pool_size=20))
model.add(Dropout(0.5))
model.compile(loss='mean_squared_error', 
              optimizer='sgd', 
              metrics=['accuracy'])
model.summary() 

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_112 (Conv1D)          (None, 7421, 40)          3240      
_________________________________________________________________
max_pooling1d_93 (MaxPooling (None, 3710, 40)          0         
_________________________________________________________________
dense_6 (Dense)              (None, 3710, 40)          1640       
_________________________________________________________________
dropout_81 (Dropout)         (None, 3710, 40)          0         
_________________________________________________________________
conv1d_113 (Conv1D)          (None, 2911, 40)          1280040   
_________________________________________________________________
max_pooling1d_94 (MaxPooling (None, 145, 40)           0         
_________________________________________________________________
dropout_82 (Dropout)         (None, 145, 40)           0         
=================================================================
Total params: 1,284,920
Trainable params: 1,284,920
Non-trainable params: 0

model.fit(X_train, y_train, batch_size=50, epochs=1, validation_data=(X_valid, y_valid)) 

ValueError: Error when checking target: expected dropout_82 to have 3 dimensions, but got array with shape (7, 7500)

我认为它关注y_train和y_valid,但是如果我扩展维度,则错误会随着这个而改变

ValueError: Error when checking target: expected dropout_86 to have shape (None, 145, 40) but got array with shape (7, 7500, 1)

1 个答案:

答案 0 :(得分:0)

正如错误消息所示,输入数据的形状与预期的形状不匹配,因为您扩展了错误的维度。

在这种情况下,原始训练数据的形状为(23,4800000),并且在使用

扩展尺寸后变为(23,480000,1)
X = np.expand_dims(train, axis=2)

但输入数据的预期形状为(None,4800000,10),None表示可变长度。

你应该做的只是用

重塑数据
X = np.expand_dims(train, axis=0)