简体   繁体   English

输入形状(以喀拉斯计)(此损失期望目标与输出具有相同的形状)

[英]Input shape in keras (This loss expects targets to have the same shape as the output)

this is my first time using keras, I'm trying to follow a tutorial I've found online and fit my own data to it. 这是我第一次使用keras,我正在尝试遵循我在网上找到的教程,并将自己的数据拟合到其中。 I have a matrix and binary labels. 我有一个矩阵和二进制标签。

> str(d_train)
 num [1:1062, 1:180] -0.04748 0.04607 -0.05429 -0.0126 -0.00219 ...
> str(trainlabels)
 num [1:1062, 1:2] 0 0 0 0 0 0 1 0 0 0 ...

my code: 我的代码:

model = keras_model_sequential()
model %>%
  layer_dense(units = 8, activation = 'relu', input_shape = c(180)) %>%
  layer_dense(units = 3, activation = "softmax")
summary(model)
## Compile
model %>%
  compile(loss = "binary_crossentropy",
          optimizer = "adam",
          metrics = "accuracy")
## Fit model
history = model %>%
  fit(d_train,
      trainlabels,
      epoch=200,
      batch_size=32,
      validation_split=0.2)

I can't seem to fit the model, I'm getting this error message: 我似乎无法拟合模型,收到以下错误消息:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: A target array with shape (1062, 2) was passed for an output of shape (None, 3) while using as loss `binary_crossentropy`. This loss expects targets to have the same shape as the output.

Based on the error message, asking for different shape of my input array, I tried to change the dimensions around with no luck. 根据错误消息,要求输入数组的形状不同,我尝试更改尺寸而没有运气。

I am not an R expert, but here: 我不是R专家,但是在这里:

layer_dense(units = 3, activation = "softmax")

You are telling Keras that the output of your network has three classes. 您正在告诉Keras,您的网络输出具有三个类。 Your labels have shape (1062, 2) which suggest it has two classes, hence there is an inconsistency. 您的标签具有形状(1062, 2) ,表明它具有两个类别,因此存在不一致的地方。

You could just change units = 2 in your last dense and it should work. 您可以在最后一个密集区中更改units = 2 ,它应该可以工作。 Also note that you are using the softmax activation, and in that case you should prefer to use the categorical_crossentropy loss. 还要注意,您正在使用softmax激活,在这种情况下,您应该更喜欢使用categorical_crossentropy损失。

To use binary_crossentropy for binary classification, you should have units = 1 , sigmoid activation, and labels should be (1062, 1) or (1062,) , which means they are 0-1 encoded. 要将binary_crossentropy用于二进制分类,您应该将units = 1sigmoid激活,并且标签应为(1062, 1)(1062,) ,这意味着它们是0-1编码的。

暂无
暂无

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

相关问题 多元LSTM的keras输入形状 - keras input shape for multivariate LSTM 使用keras 1D卷积层在R(Rstudio)中设置NLP任务的输入形状,当它需要3维输入(张量)时 - Setting input shape for an NLP task in R(Rstudio) using keras 1D convolution layer, when it expects 3 dimensional input (a tensor) 张量形状在keras模型的输入形状中重要吗? R编程 - Does Tensor Shape matter in the input shape of keras model? R-Programming 如何检查R中两个图的形状是否相同? - How can I check if two plots have the same shape in R? 如何从数组返回一个具有条件的值,与输入数组的形状相同? - How to return a value with condition from array, with same shape as input array? r keras:ValueError:检查目标时出错:预期density_18具有形状(无,6) - r keras: ValueError: Error when checking target: expected dense_18 to have shape (None, 6) Keras R中图像分类模型中的形状误差 - Shape error in image classification model in Keras R 一维 CNN 的输入形状 - Input shape for 1-Dimensional CNN 构建多输入多输出模型:给出 AttributeError: 'dict' object has no attribute 'shape' - Building a multi input and multi output model: giving AttributeError: 'dict' object has no attribute 'shape' 直接绘制拟合函数和绘制预测值(它们具有相同的形状但范围不同)之间有什么区别? - What are the differences between directly plotting the fit function and plotting the predicted values(they have same shape but different ranges)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM