【问题标题】:Keras error when applying multiple categorys for prediction应用多个类别进行预测时出现 Keras 错误
【发布时间】:2019-12-07 05:09:00
【问题描述】:

我有一些文本数据,我已按照此处的所有步骤操作:https://jjallaire.github.io/deep-learning-with-r-notebooks/notebooks/6.1-using-word-embeddings.nb.html

但是我已经根据自己的问题对其进行了调整。在上面的示例中,作者使用了 0 或 1 的分类。但是在我的模型中,我想对 1、2、3、4、5 进行分类。但是我收到以下错误:

 Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: You are passing a target array of shape (15433, 1) while using as loss `categorical_crossentropy`. `categorical_crossentropy` expects targets to be binary matrices (1s and 0s) of shape (samples, classes). If your targets are integer classes, you can convert them to the expected format via:
```
from keras.utils import to_categorical
y_binary = to_categorical(y_int)
```

Alternatively, you can use the loss function `sparse_categorical_crossentropy` instead, which does expect integer targets. 

我使用的代码如下:

model <- keras_model_sequential() %>% 
  layer_embedding(input_dim = max_words, output_dim = embedding_dim, 
                  input_length = maxlen) %>% 
  layer_flatten() %>% 
  layer_dense(units = 32, activation = "relu") %>% 
  layer_dense(units = 5, activation = "softmax")


model %>% compile(
  optimizer = "rmsprop",
  loss = "categorical_crossentropy",
  metrics = c("accuracy")
)

history <- model %>% fit(
  x_train, y_train,
  epochs = 20,
  batch_size = 32,
  validation_data = list(x_test, y_test)
)

x_train 和 x_test 是文本数据,y_train 和 y_test 是 1,2,3,4,5 分类。

> unique(y_test)
[1] 5 3 4 2 1
> unique(y_train)
[1] 2 3 1 5 4

在正确方向上的任何帮助都会很棒!

【问题讨论】:

    标签: r machine-learning keras neural-network classification


    【解决方案1】:

    您似乎将整数标签(即 0、1、2、3 等)而不是单热编码标签(例如 [1,0,0][0,1,0][0,0,1])传递给您的模型。但是,由于您使用 categorical_crossentropy 作为损失函数,因此您必须传递 one-hot 编码标签。但是,您可以使用该损失函数的等效稀疏版本,即sparse_categorical_crossentropy,而无需修改您的标签:

    model %>% compile(
      loss = "sparse_categorical_crossentropy", ...
    )
    

    此外,请确保标签从 0 而不是 1 开始(即整数标签必须在集合 {0, 1, 2, ..., NUM_CLASSES-1} 中)。

    【讨论】:

    • 谢谢!这似乎已经解决了这个问题!
    【解决方案2】:

    你有两个选择:

    1. 对您的数据进行一次热编码:
    history <- model %>% fit(
      x_train, to_categorical(y_train -1),
      epochs = 20,
      batch_size = 32,
      validation_data = list(x_test, to_categorical(y_test-1))
    )
    
    1. 使用sparse_categorical_crossentropy损失:
    model %>% compile(
      optimizer = "rmsprop",
      loss = "sparse_categorical_crossentropy",
      metrics = c("accuracy")
    )
    
    history <- model %>% fit(
      x_train, y_train -1,
      epochs = 20,
      batch_size = 32,
      validation_data = list(x_test, y_test-1)
    )
    

    请注意,您需要在 y 中添加 -1,因为 Keras 将采用从零开始的整数。

    【讨论】:

    • 谢谢!我不知道,它似乎解决了我的问题!我会在预处理时更加小心。我得到了大量的负数作为我的损失,现在损失是正的,看起来好多了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    相关资源
    最近更新 更多