ValueError:形状 (104, 1) 和 (None, 104) 不兼容

时间:2021-04-22 07:22:47

标签: python tensorflow keras google-colaboratory text-classification

我正在使用 Tensorflow 和 Keras 开发文本分类器。我使用自己的数据集按照 this TF tutorial 中的说明进行操作,但是当需要训练模型时,它失败了,返回此错误:

ValueError: in user code:
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 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:2730 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:3417 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:788 run_step  **
        outputs = model.train_step(data)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:756 train_step
        y, y_pred, sample_weight, regularization_losses=self.losses)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/compile_utils.py:203 __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:152 __call__
        losses = call_fn(y_true, y_pred)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:256 call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
        return target(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:1537 categorical_crossentropy
        return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
        return target(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py:4833 categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py:1134 assert_is_compatible_with
        raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (104, 1) and (None, 104) are incompatible

这是我创建 train_ds 和 val_ds 的时间。首先,我为每个集合(X 和 y)创建一个数据集结构。集合 y 由单热矩阵(104 个值作为类数)组成。然后我对集合进行矢量化,在单词和整数之间建立一对一的关联(每个单词都有一个唯一的 int 值):

X_train_ds = tf.data.TextLineDataset(file_list[:train_len])
X_val_ds = tf.data.TextLineDataset(file_list[train_len:train_len+val_len])
y_train_ds = tf.data.Dataset.from_tensor_slices(train_one_hot)
y_val_ds = tf.data.Dataset.from_tensor_slices(val_one_hot)

trainset = tf.data.Dataset.zip((X_train_ds, y_train_ds))
valset = tf.data.Dataset.zip((X_train_ds, y_val_ds))

AUTOTUNE = tf.data.experimental.AUTOTUNE  # to level up performances

train_ds = trainset.cache().prefetch(buffer_size=AUTOTUNE)
val_ds = X_val_ds.cache().prefetch(buffer_size=AUTOTUNE)

train_ds = trainset.map(vectorize_text)
val_ds = valset.map(vectorize_text)

以下是 train_ds 对象中每个元素是如何组成的快照:

[(b'Fwd: ---------- Forwarded message --------- Da: Alicja Mantovani <> Date: Mer 13 Gen 2021, 10:01 Subject: To: <> Good Morning Serena yesterday I asked why I would like to purchase a skirt. Here is the code you sent me ',
  array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int32)),

这是模型:

def create_model():
    model = tf.keras.Sequential([
      tf.keras.layers.Embedding(max_features + 1, embedding_dim),
      tf.keras.layers.Dropout(0.2),
      tf.keras.layers.GlobalAveragePooling1D(),
      tf.keras.layers.Dropout(0.2),
      tf.keras.layers.Dense(104, activation="softmax")])
    model.summary()
    
    return model

model = create_model()

其中 max_feature = 5000embedding_dim = 32

这是火车呼叫(因为它是单热矩阵版本,我必须选择 CategoricalCrossentropy):

model.compile(optimizer="adam",
              loss=tf.keras.losses.CategoricalCrossentropy(),
              metrics=['accuracy'])

history = model.fit(train_ds,
                      batch_size=32,
                      validation_data=val_ds,
                      epochs=10,
                      verbose=1,
                      callbacks=[early_stopping_cb])

train_ds 是一个 <ZipDataset shapes: ((), (104,)), types: (tf.string, tf.int32)>train_ds 也一样。

0 个答案:

没有答案