简体   繁体   English

tensorflow 值错误:形状不兼容

[英]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.

相关问题 TensorFlow:ValueError:形状不兼容 - TensorFlow: ValueError: Shapes are incompatible TensorFlow - ValueError:形状 (3, 1) 和 (4, 3) 不兼容 - TensorFlow - ValueError: Shapes (3, 1) and (4, 3) are incompatible Tensorflow ValueError:形状(?,1)和(?,)不兼容 - Tensorflow ValueError: Shapes (?, 1) and (?,) are incompatible Tensorflow ValueError 形状不兼容 - Tensorflow ValueError Shapes are incompatible Tensorflow:ValueError:形状(None,1)和(None,2)不兼容 - Tensorflow: ValueError: Shapes (None, 1) and (None, 2) are incompatible TensorFlow - ValueError:形状(无,1)和(无,10)不兼容 - TensorFlow - ValueError: Shapes (None, 1) and (None, 10) are incompatible ValueError:使用 RandomizedSearchCV 的 Tensorflow LSTM 中的形状不兼容 - ValueError: Shapes are incompatible in Tensorflow LSTM using RandomizedSearchCV 在 tensorflow 中出现 ValueError 说我的形状不兼容 - Getting a ValueError in tensorflow saying that my shapes are incompatible Tensorflow 尺寸问题:ValueError:形状 (3, 1) 和 (None, 3) 不兼容 - Tensorflow dimension issue: ValueError: Shapes (3, 1) and (None, 3) are incompatible Tensorflow - 我没有得到正确的形状 - `ValueError:形状 (9, 1) 和 (8, 9) 不兼容` - Tensorflow - I don't get the right shapes - `ValueError: Shapes (9, 1) and (8, 9) are incompatible`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM