[英]Tensorflow dimension issue: ValueError: Shapes (3, 1) and (None, 3) are incompatible
I'm pretty new with NN and I am having an issue with some dimensions whilst fitting a model.我对 NN 很陌生,在安装 model 时遇到了一些尺寸问题。 Here's my case:
这是我的情况:
model_sigmoid = tf.keras.Sequential([
embedding_layer,
GlobalAveragePooling1D(),
Dense(3, activation="softmax")])
model_sigmoid.summary()
Model: "sequential_12"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding (Embedding) (None, None, 100) 1195200
_________________________________________________________________
global_average_pooling1d_5 ( (None, 100) 0
_________________________________________________________________
dense_11 (Dense) (None, 3) 303
=================================================================
Total params: 1,195,503
Trainable params: 303
Non-trainable params: 1,195,200
___________________________________________
This is the model I would like to train (it's a model to set a starting baseline).这是我想训练的 model(设置起始基线是 model)。 It's a multiclass classification problem with an embedding layer: GloVe 100d embedding
这是一个带有嵌入层的多类分类问题:GloVe 100d embedding
model_sigmoid.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
history = model_sigmoid.fit(
train, epochs=10, batch_size=128,
validation_data=validation, verbose=1
)
train
and validation
are vectorized version of my train and validation dataset. train
和validation
是我的训练和验证数据集的矢量化版本。
train_ds
<MapDataset shapes: ((None, 80), (3,)), types: (tf.int64, tf.float32)>
tweet, label = next(iter(train))
tweet
<tf.Tensor: shape=(1, 80), dtype=int64, numpy=
array([[ 6, 32, 1321, 3, 157, 383, 4, 18, 137, 1222, 6,
18, 181, 2770, 1024, 6781, 51, 6, 375, 240, 486, 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]])>
label
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 0., 0.], dtype=float32)>
As you can see the my "X" is a sequence with a length of 80 and with integers that correspond to the initial words in my dataset.如您所见,我的“X”是一个长度为 80 的序列,其整数对应于我的数据集中的初始单词。 My "Y" instead is an encoded version of the original sentiment value (negative, neutral, positive).
相反,我的“Y”是原始情绪值(负面、中性、正面)的编码版本。
When I call the fit operation I get当我调用 fit 操作时,我得到
ValueError: Shapes (3, 1) and (None, 3) are incompatible
I'm pretty sure the error is with Y, but I can't really figure out how to fix the shape of my tensor.我很确定错误出在 Y 上,但我真的不知道如何修复张量的形状。
After some digging and more shape inspection, I figured out how to fix the error above.经过一些挖掘和更多的形状检查,我想出了如何解决上面的错误。
I added a reshape call in my function:我在 function 中添加了一个重塑调用:
def vectorize_text_and_reshape(text, label):
text = tf.expand_dims(text, -1)
return vectorizer(text), tf.reshape(label, [1,3])
train_ds = train_tf.map(vectorize_text_and_reshape)
val_ds = val_tf.map(vectorize_text_and_reshape)
test_ds = test_tf.map(vectorize_text_and_reshape)
I had already implemented the vectorize_text_and_reshape
function above for vectorizing my text data.我已经实现了上面的
vectorize_text_and_reshape
function 来对我的文本数据进行矢量化。 I only had to add the reshape call at the label level.我只需要在 label 级别添加重塑调用。 This turned my label from (3,) shape to (1,3).
这将我的 label 从 (3,) 形状变成了 (1,3)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.