繁体   English   中英

在 tensorflow 2.x 中获取 ValueError: ValueError: Shapes (50, 6) and (50, 100) is incompatible

[英]Getting a ValueError in tensorflow 2.x: ValueError: Shapes (50, 6) and (50, 100) are incompatible

我对 tensorflow 还很陌生,我有这个简单的模型:

model = keras.Sequential()
model.add(layers.Embedding(input_dim=64, output_dim=32))
model.add(layers.GRU(128, return_sequences=True))
model.add(layers.SimpleRNN(32))
model.add(layers.Dense(100, activation='softmax'))

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

当我尝试使用以下模型拟合模型时:

model.fit(dataset, epochs=50)

我收到以下错误:ValueError: Shapes (50, 6) and (50, 100) is incompatible

我有这个数据集:<MapDataset shape: ((50, 6), (50, 6)), types: (tf.int64, tf.int64)>

我使用此代码制作数据集:

results = tf.data.Dataset.from_tensor_slices(dataset)
sequences = results.batch(51, drop_remainder=True)
def split(batch):       
    input_ = batch[:-1]
    output_ = batch[1:]
    return input_, output_
dataset = sequences.map(split)

我正在尝试创建一个模型,在给定数组序列的情况下,它将预测序列中的下一个元素。 我正在使用 google colab 来运行我的代码。 任何帮助将不胜感激。

查看您的数据集,您有6 classes50 samples 因此你的标签形状是(50,6)

但是您模型的输出层有 100 个单元,这实质上意味着您有 100 个类,但事实并非如此。 因此你会得到形状错误。

最后一层中的单元数应等于具有categorical_crossentropy损失的softmax激活的categorical_crossentropy

将模型架构更改为:

model = keras.Sequential()
model.add(layers.Embedding(input_dim=64, output_dim=32))
model.add(layers.GRU(128, return_sequences=True))
model.add(layers.SimpleRNN(32))
model.add(layers.Dense(6, activation='softmax'))

暂无
暂无

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

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