简体   繁体   中英

Runnnig a model.fit for cnn : valueError

Once I want to train my model, I get the following error in the model.fit() line:

Code:

DO = Denoiser()
visible = Input(shape=(500, batch_size))
my_denoiser = DO.rkhs(visible, kern, I_mat)
conv1 = Conv1D(6, kernel_size=4, activation='relu')(visible)
pool1 = MaxPooling1D(pool_size=5)(conv1)
conv2 = Conv1D(12, kernel_size=4, activation='relu')(pool1)
pool2 = MaxPooling1D(pool_size=5)(conv2)
flat = Flatten()(pool2)
hidden1 = Dense(10, activation='relu')(flat)
output = Dense(3, activation='softmax')(hidden1)
model = Model(inputs=visible, outputs=output)

model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(new_signal, y, epochs=2, batch_size=200)

output:

   Model: "model_3"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_6 (InputLayer)        [(None, 500, 128)]        0         
                                                                 
 conv1d_7 (Conv1D)           (None, 497, 6)            3078      
                                                                 
 max_pooling1d_7 (MaxPooling  (None, 99, 6)            0         
 1D)                                                             
                                                                 
 conv1d_8 (Conv1D)           (None, 96, 12)            300       
                                                                 
 max_pooling1d_8 (MaxPooling  (None, 19, 12)           0         
 1D)                                                             
                                                                 
 flatten_3 (Flatten)         (None, 228)               0         
                                                                 
 dense_6 (Dense)             (None, 10)                2290      
                                                                 
 dense_7 (Dense)             (None, 3)                 33        
                                                                 
=================================================================
Total params: 5,701
Trainable params: 5,701
Non-trainable params: 0
_________________________________________________________________

Error:

ValueError: in user code:

File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function  *
    return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step  **
    outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
    y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
    raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

ValueError: Input 0 of layer "model_3" is incompatible with the layer: expected shape=(None, 500, 128), found shape=(None, 10)

The batch_size variable is not related to the Input of the network, batch_size refers to the number of samples before doing a backpropagation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM