[英]tensorflow ValueError: Shapes are incompatible
My model's x
is an array of float arrays (each sample is an array containing 40 elements).我的模型的
x
是一个浮点数组 arrays (每个样本都是一个包含 40 个元素的数组)。 My model's y
is also an array of float arrays (each sample is an array containing 80 elements).我的模型的
y
也是一个浮点数组 arrays (每个样本都是一个包含 80 个元素的数组)。 Here's the code reproducing my issue:这是重现我的问题的代码:
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)
And this is the error produced.这就是产生的错误。
ValueError: Shapes (None, 80) and (None, 40) are incompatible
What is going wrong?出了什么问题?
In order to measure the loss, the dimensions need to match.为了衡量损失,维度需要匹配。 You're trying to compare an output of
(100, 40)
with a target array of (100, 80)
.您正在尝试将
(100, 40)
的 output 与 ( (100, 80)
的目标数组进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.