【问题标题】:R-Image Plot/Visualization-MNIST datasetR-Image Plot/Visualization-MNIST 数据集
【发布时间】:2019-07-31 14:46:02
【问题描述】:

我在使用 MNIST 数据集在 R 中绘制图像 50 时遇到问题。

  library(keras)
  mnist <- dataset_mnist()
  train_images <- mnist$train$x
  train_labels <- mnist$train$y
  test_images <- mnist$test$x
  test_labels <- mnist$test$y
  
  train_images <- array_reshape(train_images, c(60000, 28 * 28))
  train_images <- train_images / 255
  
  test_images <- array_reshape(test_images, c(10000, 28 * 28))
  test_images <- test_images / 255
  
  train_labels <- to_categorical(train_labels)
  test_labels <- to_categorical(test_labels)
  
  network <- keras_model_sequential() % % layer_dense(units = 512, 
             activation = "relu", input_shape = c(28 * 28)) % %
                                  layer_dense(units = 10, activation = "softmax")
  
  network % % compile(optimizer = "rmsprop",loss = "categorical_crossentropy", 
                                                    metrics = c("accuracy"))
  
  network % % fit(train_images, train_labels, epochs = 5, batch_size = 128)
  digit<-train_images[50,,]

train_images[50, , ] 中的错误:维数不正确

我不知道如何处理这个错误信息。感谢您的帮助!

【问题讨论】:

  • 请提供reproducible example in r。我提供的链接会告诉你如何做。此外,请拨打tour 并访问how to ask。干杯。
  • 也许可以这样做:digit&lt;-train_images[50 , ]
  • 我试过 digit

标签: r keras mnist


【解决方案1】:

mnist$train$x 的维度为 (60000, 28, 28)。

library(keras)
mnist <- dataset_mnist()
train_images <- mnist$train$x
dim(train_images)
# 60000    28    28

要绘制第 50 张图像,您需要对其进行子集化。

digit <- train_images[50, 1:28, 1:28]

这将是一个尺寸为 28x28 的矩阵。你需要先转置它。然后您可以使用image 函数进行绘图。

par(pty="s") # for keeping the aspect ratio 1:1
image(t(digit), col = gray.colors(256), axes = FALSE)

【讨论】:

    猜你喜欢
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 2019-07-18
    • 2019-05-14
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多