简体   繁体   English

model.fit keras 和用户代码中的 ValueError

[英]ValueError in model.fit keras and user code

I have a problem in training a model我在训练 model 时遇到问题

I'm learning deep-learning python using keras and tensorflow.我正在使用 keras 和 tensorflow 学习深度学习 python。 I am using efficientnetb0 from imagenet dataset.我正在使用 imagenet 数据集中的efficientnetb0。 I had divided the training and testing sets and performed one hot encoding.我已经划分了训练集和测试集并执行了一次热编码。 I have 17 folders or classifications of images.我有 17 个文件夹或图像分类。

effnet = EfficientNetB0(weights='imagenet',include_top=False,input_shape=(image_size,image_size,3))

model = effnet.output
model = tf.keras.layers.GlobalAveragePooling2D()(model)
model = tf.keras.layers.Dropout(rate=0.5)(model)
model = tf.keras.layers.Dense(17,activation='softmax')(model)
model = tf.keras.models.Model(inputs=effnet.input, outputs = model)

model.compile(loss='categorical_crossentropy',optimizer = 'Adam', metrics= ['accuracy'])

Everything ran smooth until training the model一切顺利,直到训练 model

history = model.fit(X_train,y_train,validation_split=0.1, epochs =10, verbose=1,batch_size=32, callbacks=[tensorboard,checkpoint,reduce_lr])

ValueError                                Traceback (most recent call last)
Input In [22], in <cell line: 1>()
----> 1 history = model.fit(X_train,y_train,validation_split=0.1, epochs =20, verbose=1,batch_size=32, callbacks=[tensorboard,checkpoint,reduce_lr])

File C:\Ken\Conda\lib\site-packages\keras\utils\traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     65 except Exception as e:  # pylint: disable=broad-except
     66   filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67   raise e.with_traceback(filtered_tb) from None
     68 finally:
     69   del filtered_tb

File ~\AppData\Local\Temp\__autograph_generated_filexkmxbaog.py:15, in outer_factory.<locals>.inner_factory.<locals>.tf__train_function(iterator)
     13 try:
     14     do_return = True
---> 15     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16 except:
     17     do_return = False

ValueError: in user code:

    File "C:\Ken\Conda\lib\site-packages\keras\engine\training.py", line 1051, in train_function  *
        return step_function(self, iterator)
    File "C:\Ken\Conda\lib\site-packages\keras\engine\training.py", line 1040, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "C:\Ken\Conda\lib\site-packages\keras\engine\training.py", line 1030, in run_step  **
        outputs = model.train_step(data)
    File "C:\Ken\Conda\lib\site-packages\keras\engine\training.py", line 890, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "C:\Ken\Conda\lib\site-packages\keras\engine\training.py", line 948, in compute_loss
        return self.compiled_loss(
    File "C:\Ken\Conda\lib\site-packages\keras\engine\compile_utils.py", line 201, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "C:\Ken\Conda\lib\site-packages\keras\losses.py", line 139, in __call__
        losses = call_fn(y_true, y_pred)
    File "C:\Ken\Conda\lib\site-packages\keras\losses.py", line 243, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "C:\Ken\Conda\lib\site-packages\keras\losses.py", line 1787, in categorical_crossentropy
        return backend.categorical_crossentropy(
    File "C:\Ken\Conda\lib\site-packages\keras\backend.py", line 5119, in categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)

    ValueError: Shapes (None, 18) and (None, 17) are incompatible

I fixed the error我修复了错误

I just changed dense layer to 18 and deleted dropout layer.我只是将密集层更改为 18 并删除了 dropout 层。 I don't know why.我不知道为什么。 I will determine if it is because of the dense layer or the dropout layer.我将确定是因为密集层还是辍学层。 Maybe dropout helps the model not to overfit.也许 dropout 有助于 model 不会过拟合。 Maybe it was the dense layer's error.也许这是密集层的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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