logits and labels must have the same first dimension, got logits shape [32,4] and labels shape [128]

这个错误目前发现有两种情况:

第一种就是字面意思 你的shape没有对齐,

(2000, 64, 64, 1)
(2000, 10)
比如上面两个形状的第一维是样本数量,(64,64,1)是图片维度。10是分类。要求2000=2000.就是x,y配对。很好理解。

但是当你检查之后发现没问题还报这个错。

第二种情况:

你的损失函数使用有问题。

损失函数使用要求:


问题出在您的目标形状上,并且与正确选择合适的损失函数有关。你有两种可能性:

1.可能性:如果你有一个一维整数编码的目标,你可以sparse_categorical_crossentropy用作损失函数

n_class = 3
n_features = 100
n_sample = 1000

X = np.random.randint(0,10, (n_sample,n_features))
y = np.random.randint(0,n_class, n_sample)

inp = Input((n_features,))
x = Dense(128, activation='relu')(inp)
out = Dense(n_class, activation='softmax')(x)

model = Model(inp, out)
model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
history = model.fit(X, y, epochs=3)

2.可能性:如果您对目标进行单热编码以获得2D形状(n_samples,n_class),则可以使用categorical_crossentropy

n_class = 3
n_features = 100
n_sample = 1000

X = np.random.randint(0,10, (n_sample,n_features))
y = pd.get_dummies(np.random.randint(0,n_class, n_sample)).values

inp = Input((n_features,))
x = Dense(128, activation='relu')(inp)
out = Dense(n_class, activation='softmax')(x)

model = Model(inp, out)
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
history = model.fit(X, y, epochs=3)

换成categorical_crossentropy试试。

如果解决了你的问题,请点个免费的赞。

  • 64
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值