【发布时间】: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