构建模型后出现以下错误(完全按照教程进行,但将其应用于我自己的数据集。
这里有模型和我遇到的错误。 (重新应用代码时遇到第二个错误)。
我有5个类别变量,我想尝试训练一些文本数据以将自身分类为5个类别中的1个。
代码和错误:
> maxlen
[1] 1000
> embedding_dim
[1] 300
> max_words
[1] 15000
> embedding_dim <- 300
>
> model <- keras_model_sequential() %>%
+ layer_embedding(input_dim = max_words, output_dim = embedding_dim,
+ input_length = maxlen) %>%
+ layer_flatten() %>%
+ layer_dense(units = 8, activation = "relu") %>%
+ layer_dropout(0.4) %>%
+ layer_dense(units = 5, activation = "softmax")
>
> model %>% compile(
+ optimizer = "rmsprop",
+ loss = "categorical_crossentropy",
+ metrics = c("accuracy")
+ )
>
> history <- model %>% fit(
+ x_train, to_categorical(y_train),
+ epochs = 20,
+ batch_size = 32,
+ validation_data = list(x_val, to_categorical(y_val))
+ )
/data/users/USER/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
/data/users/USER/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/data/users/USER/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
/data/users/USER/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/data/users/USER/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)])
/data/users/USER/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])
/data/users/USER/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
/data/users/USER/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/data/users/USER/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
/data/users/USER/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/data/users/USER/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)])
/data/users/USER/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])
WARNING: Logging before flag parsing goes to stderr.
W0730 19:41:13.310763 140518437951872 lazy_loader.py:50]
The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:
* https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
* https://github.com/tensorflow/addons
* https://github.com/tensorflow/io (for I/O related ops)
If you depend on functionality not listed there, please file an issue.
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: cannot convert float NaN to integer
Detailed traceback:
File "/data/users/USER/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorflow/python/keras/utils/np_utils.py", line 40, in to_categorical
y = np.array(y, dtype='int')
当我重新运行模型时
> model <- keras_model_sequential() %>%
+ layer_embedding(input_dim = max_words, output_dim = embedding_dim,
+ input_length = maxlen) %>%
+ layer_flatten() %>%
+ layer_dense(units = 8, activation = "relu") %>%
+ layer_dropout(0.4) %>%
+ layer_dense(units = 5, activation = "softmax")
> model %>% compile(
+ optimizer = "rmsprop",
+ loss = "categorical_crossentropy",
+ metrics = c("accuracy")
+ )
> history <- model %>% fit(
+ x_train, to_categorical(y_train),
+ epochs = 20,
+ batch_size = 32,
+ validation_data = list(x_val, to_categorical(y_val))
+ )
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: cannot convert float NaN to integer
Detailed traceback:
File "/data/users/msmith/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorflow/python/keras/utils/np_utils.py", line 40, in to_categorical
y = np.array(y, dtype='int')
答案 0 :(得分:1)
当y包含任何NA值时,我也会遇到相同的错误。您需要确保y
和y_train
不包含任何NA值。
keras::to_categorical(c(1, NA))
#> Error in py_call_impl(callable, dots$args, dots$keywords): ValueError: cannot convert float NaN to integer
#>
#> Detailed traceback:
#> File "/Users/dfalbel/.virtualenvs/r-reticulate/lib/python2.7/site-packages/tensorflow/python/keras/utils/np_utils.py", line 40, in to_categorical
#> y = np.array(y, dtype='int')
由reprex package(v0.3.0)于2019-07-31创建