简体   繁体   English

“ValueError:形状 (None, 1) 和 (None, 6) 不兼容”

[英]“ValueError: Shapes (None, 1) and (None, 6) are incompatible”

I want to classify 6 different categories of x-ray scans, What's wrong with the code?我想对 6 种不同类别的 X 射线扫描进行分类,代码有什么问题?

model = Sequential()

model.add(Conv2D(256, (3, 3), input_shape=X.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(256, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())  

model.add(Dense(64))

model.add(Dense(6))
model.add(Activation('softmax'))

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

model.fit(X, y, batch_size=32, epochs=3, validation_split=0.1)

The shapes of input are: (50, 50, 1)输入的形状是:(50, 50, 1)

Should I remove one of the MaxPooling layers?我应该删除 MaxPooling 层之一吗?

I've seen it's good manners here to post the traceback aswell, so here it is:我已经看到在这里发布回溯也很有礼貌,所以这里是:

Epoch 1/3
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
(...)
ValueError: in user code:

    C:\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py:571 train_function  *
        outputs = self.distribute_strategy.run(
    C:\Python38\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:951 run  **
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    C:\Python38\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2290 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    C:\Python38\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2649 _call_for_each_replica
        return fn(*args, **kwargs)
    C:\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py:532 train_step  **
        loss = self.compiled_loss(
    C:\Python38\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:205 __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    C:\Python38\lib\site-packages\tensorflow\python\keras\losses.py:143 __call__
        losses = self.call(y_true, y_pred)
    C:\Python38\lib\site-packages\tensorflow\python\keras\losses.py:246 call
        return self.fn(y_true, y_pred, **self._fn_kwargs)
    C:\Python38\lib\site-packages\tensorflow\python\keras\losses.py:1527 categorical_crossentropy
        return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
    C:\Python38\lib\site-packages\tensorflow\python\keras\backend.py:4561 categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)
    C:\Python38\lib\site-packages\tensorflow\python\framework\tensor_shape.py:1117 assert_is_compatible_with
        raise ValueError("Shapes %s and %s are incompatible" % (self, other))

    ValueError: Shapes (None, 1) and (None, 6) are incompatible

to avoid misunderstandings and possible error I suggest you to reshape your target from (586,1) to (586,).为避免误解和可能的错误,我建议您将目标从 (586,1) 重塑为 (586,)。 you can simply do y = y.ravel()你可以简单地做y = y.ravel()

you have to simply manage the correct loss你必须简单地管理正确的损失

if you have 1D integer encoded target you can use sparse_categorical_crossentropy as loss function如果您有 1D integer 编码目标,您可以使用 sparse_categorical_crossentropy 作为损失 function

X = np.random.randint(0,10, (1000,100))
y = np.random.randint(0,3, 1000)

model = Sequential([
    Dense(128, input_dim = 100),
    Dense(3, activation='softmax'),
])
model.summary()
model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
history = model.fit(X, y, epochs=3)

Otherwise, if you have one-hot encoded your target in order to have 2D shape (n_samples, n_class) you can use categorical_crossentropy否则,如果您对目标进行一次性编码以获得 2D 形状 (n_samples, n_class),则可以使用 categorical_crossentropy

X = np.random.randint(0,10, (1000,100))
y = pd.get_dummies(np.random.randint(0,3, 1000)).values

model = Sequential([
    Dense(128, input_dim = 100),
    Dense(3, activation='softmax'),
])
model.summary()
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
history = model.fit(X, y, epochs=3)

For my case in the one-hot coding scenario, I use the following method.对于我在 one-hot 编码场景中的情况,我使用以下方法。

1.Build the DNN 1.构建DNN

model.add(layers.Dense(1, activation='sigmoid'))

2.Configure the model 2.配置model

model.compile(optimizer=optimizers.RMSprop(lr=1e-4),
              loss='binary_crossentropy',
              metrics=['acc'])

It can successfully solves the above-mentioned issue.它可以成功解决上述问题。

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

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