【发布时间】:2022-11-19 12:55:48
【问题描述】:
我以前没有使用 Tensorflow 或 Keras 的经验。 我正在尝试按照教程https://tensorflow.rstudio.com/tutorials/beginners/
library(keras)
mnist <- dataset_mnist()
mnist$train$x <- mnist$train$x/255
mnist$test$x <- mnist$test$x/255
model <- keras_model_sequential() %>%
layer_flatten(input_shape = c(28, 28)) %>%
layer_dense(units = 128, activation = "relu") %>%
layer_dropout(0.2) %>%
layer_dense(10, activation = "softmax")
summary(model)
model %>%
compile(
loss = "sparse_categorical_crossentropy",
optimizer = "adam",
metrics = "accuracy"
)
#Note that compile and fit (which we are going to see next) modify the model object in place, unlike most R functions.
model %>%
fit(
x = mnist$train$x, y = mnist$train$y,
epochs = 5,
validation_split = 0.3,
verbose = 2
)
predictions <- predict(model, mnist$test$x)
head(predictions, 2)
class_predictions <- predict(model, mnist$test$x) %>% k_argmax()
class_predictions
predict_classes 已弃用。 k_armax() 被宣传为错误中的替代方法。 但是我不知道如何将预测的类(在本例中为数字 0-9)作为向量在 confusionMatrix 中使用它,就像其他 R 模型一样。 任何帮助,将不胜感激。
【问题讨论】:
-
请选择一个真正描述您的问题的标题。
标签: r tensorflow keras