Keras ValueError:尺寸必须相等,但对于 'node Equal 输入形状为 2 和 32:[?,2], [?,32,32]

Posted

技术标签:

【中文标题】Keras ValueError:尺寸必须相等,但对于 \'node Equal 输入形状为 2 和 32:[?,2], [?,32,32]【英文标题】:Keras ValueError: Dimensions must be equal, but are 2 and 32 for 'node Equal with input shapes: [?,2], [?,32,32]Keras ValueError:尺寸必须相等,但对于 'node Equal 输入形状为 2 和 32:[?,2], [?,32,32] 【发布时间】:2021-11-07 06:42:41 【问题描述】:

当我遇到以下错误时,我试图训练一个简单的 Keras 网络进行分类。我知道我的输入有问题,但我不知道如何解决它。这是我的代码

我的数据集形状:

    x_train :  float32 0.0 1.0 (2444, 64, 64, 1)
    y_train :  float32 0.0 1.0 (2444, 2)
    x_test :  float32 0.0 1.0 (9123, 64, 64, 1)
    y_test :  float32 0.0 1.0 (9123, 2)

型号:

inputs = keras.Input(shape=(64,64,1), dtype='float32')

x = keras.layers.Conv2D(12,(9,9), padding="same",input_shape=(64,64,1), dtype='float32',activation='relu')(inputs)
x = keras.layers.Conv2D(18,(7,7), padding="same", activation='relu')(x)

x = keras.layers.MaxPool2D(pool_size=(2,2))(x)
x = keras.layers.Dropout(0.25)(x)

x = keras.layers.Dense(50, activation='relu')(x)
x = keras.layers.Dropout(0.4)(x)
outputs = keras.layers.Dense(2, activation='softmax')(x)

model = keras.Model(inputs, outputs)

模型总结:

Model: "model_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         [(None, 64, 64, 1)]       0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 64, 64, 12)        984       
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 64, 64, 18)        10602     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 32, 32, 18)        0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 32, 32, 18)        0         
_________________________________________________________________
dense_2 (Dense)              (None, 32, 32, 50)        950       
_________________________________________________________________
dropout_3 (Dropout)          (None, 32, 32, 50)        0         
_________________________________________________________________
dense_3 (Dense)              (None, 32, 32, 2)         102       
=================================================================
Total params: 12,638
Trainable params: 12,638
Non-trainable params: 0
________________________

编译器和拟合器在我想拟合模型时会发生哪个错误

model.compile(
    loss=keras.losses.SparseCategoricalCrossentropy(),
     optimizer=keras.optimizers.Adam(0.01),
      metrics=["acc"],
      )
model.fit(x_train, y_train, batch_size=32, epochs = 20, validation_split= 0.3,
          callbacks=[tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)])

最后是错误:

ValueError                                Traceback (most recent call last)
<ipython-input-31-e4cade46a08c> in <module>()
      1 model.fit(x_train, y_train, batch_size=32, epochs = 20, validation_split= 0.3,
----> 2           callbacks=[tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)])

9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    992           except Exception as e:  # pylint:disable=broad-except
    993             if hasattr(e, "ag_error_metadata"):
--> 994               raise e.ag_error_metadata.to_exception(e)
    995             else:
    996               raise

ValueError: in user code:

    /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:853 train_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:842 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1286 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2849 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3632 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:835 run_step  **
        outputs = model.train_step(data)
    /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:792 train_step
        self.compiled_metrics.update_state(y, y_pred, sample_weight)
    /usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py:457 update_state
        metric_obj.update_state(y_t, y_p, sample_weight=mask)
    /usr/local/lib/python3.7/dist-packages/keras/utils/metrics_utils.py:73 decorated
        update_op = update_state_fn(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/keras/metrics.py:177 update_state_fn
        return ag_update_state(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/keras/metrics.py:681 update_state  **
        matches = ag_fn(y_true, y_pred, **self._fn_kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
        return target(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/keras/metrics.py:3537 sparse_categorical_accuracy
        return tf.cast(tf.equal(y_true, y_pred), backend.floatx())
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
        return target(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/math_ops.py:1864 equal
        return gen_math_ops.equal(x, y, name=name)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/gen_math_ops.py:3219 equal
        name=name)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/op_def_library.py:750 _apply_op_helper
        attrs=attr_protos, op_def=op_def)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py:601 _create_op_internal
        compute_device)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py:3569 _create_op_internal
        op_def=op_def)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py:2042 __init__
        control_input_ops, op_def)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py:1883 _create_c_op
        raise ValueError(str(e))

    ValueError: Dimensions must be equal, but are 2 and 32 for 'node Equal = Equal[T=DT_FLOAT, incompatible_shape_error=true](IteratorGetNext:1, Cast_1)' with input shapes: [?,2], [?,32,32].

【问题讨论】:

【参考方案1】:

在模型摘要中可以看到,模型的输出形状是(None,32,32,2),而根据目标值应该是(None,2),尝试在Dense层之前添加Flatten层:

x = keras.layers.Dropout(0.25)(x)
x = keras.layers.Flatten()(x)                    # Add this
x = keras.layers.Dense(50, activation='relu')(x)

【讨论】:

是的,你是对的。我忘了在密集层之前添加展平层。不管怎样,谢谢你的帮助,Kaveh :)

以上是关于Keras ValueError:尺寸必须相等,但对于 'node Equal 输入形状为 2 和 32:[?,2], [?,32,32]的主要内容,如果未能解决你的问题,请参考以下文章

Keras ValueError: 维度必须相等,但对于 'node Equal 是 6 和 9

你能帮我解决keras的尺寸问题吗?

Python | Keras:ValueError:检查目标时出错:预期conv2d_3有4个维度,但得到了有形状的数组(1006,5)

ValueError: `sequences` 在 Keras 中必须是可迭代的

ValueError:模型的输出张量必须是TensorFlow`Layer`的输出

ValueError: 目标尺寸 (torch.Size([128])) 必须与输入尺寸 (torch.Size([112])) 相同