tensorflow ValueError:形状不兼容



我的模型的x是一个浮点数组的数组(每个样本是一个包含40个元素的数组)。我的模型的y也是一个浮点数组的数组(每个样本是包含80个元素的数组)。下面是再现我的问题的代码:

import tensorflow as tf
from tensorflow.keras import models, layers
import numpy as np
x = []
for i in range(100):
array_of_random_floats = np.random.random_sample((40))
x.append(array_of_random_floats)
x = np.asarray(x)
y = []
for i in range(100):
array_of_random_floats = np.random.random_sample((80))
y.append(array_of_random_floats)
y = np.asarray(y)
print(f"x has {len(x)} elements. Each element has {len(x[0])} elements")
# x has 100 elements. Each element has 40 elements
print(f"y has {len(y)} elements. Each element has {len(y[0])} elements")
# y has 100 elements. Each element has 80 elements

model = models.Sequential([
layers.Input(shape=(40,)),
layers.Dense(units=40),
])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
history = model.fit(x=x,
y=y,
epochs=100)

这是产生的错误。

ValueError: Shapes (None, 80) and (None, 40) are incompatible

怎么了?

为了测量损失,尺寸需要匹配。您正在尝试将(100, 40)的输出与(100, 80)的目标数组进行比较。

相关内容

  • 没有找到相关文章

最新更新