Skip to main content
 首页 » 编程设计

python之一维卷积神经网络输入形状 `ValueError`

2024年11月24日8duanxz

我正在生成一维卷积,但我的数据输入形状有一些问题。我看了一些帖子,似乎错误是数据必须是 3D,但我的数据已经是 3D。

# shape 
# x_train shape: (1228, 1452, 20) 
# y_train shape: (1228, 1452, 8) 
# x_val shape: (223, 680, 20) 
# x_val shape: (223, 680, 8) 
 
### 
n_outputs = 8 
n_timesteps = 1452 
n_features = 20 
 
model = Sequential() 
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(x_train.shape[1:]))) # ie 1452, 20 
model.add(Conv1D(filters=64, kernel_size=3, activation='relu')) 
model.add(Dropout(0.5)) 
model.add(MaxPooling1D(pool_size=2)) 
model.add(Flatten()) 
model.add(Dense(100, activation='relu')) 
model.add(Dense(n_outputs, activation='softmax')) 
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) 
 
model.fit(x_train, y_train, epochs=9,  
                batch_size=64,  
                shuffle=True) 

但我不断收到此错误消息:

ValueError: A target array with shape (1228, 1452, 8) was passed for an output of shape (None, 8) while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output. 

我从中收集到的是 3 维的目标形状与 2 维输出不同,因此无法计算出损失,但我只需要找到一种方法来 reshape 它们平等的。

编辑 model.summary()如下图

_________________________________________________________________ 
Layer (type)                 Output Shape              Param #    
================================================================= 
conv1d (Conv1D)              (None, 1450, 64)          3904       
_________________________________________________________________ 
conv1d_1 (Conv1D)            (None, 1448, 64)          12352      
_________________________________________________________________ 
dropout (Dropout)            (None, 1448, 64)          0          
_________________________________________________________________ 
max_pooling1d (MaxPooling1D) (None, 724, 64)           0          
_________________________________________________________________ 
flatten (Flatten)            (None, 46336)             0          
_________________________________________________________________ 
dense (Dense)                (None, 100)               4633700    
_________________________________________________________________ 
dense_1 (Dense)              (None, 8)                 808        
================================================================= 
Total params: 4,650,764 
Traceback (most recent call last): 
Trainable params: 4,650,764 
Non-trainable params: 0 

请您参考如下方法:

在我的案例中,问题是目标向量是 3D 而输出向量是 2D,因此存在明显的不匹配。要解决此问题,请将 y_train 的形状更改为 (batch, 8) 或使用 return_sequences=True 返回与之前相同的形状LSTM 层。