R深度学习——基本分类问题
这里使用Keras框架对Fashion MNIST数据集进行分类:
library(keras)
划分训练集测试集:
fashion_mnist <- dataset_fashion_mnist()
c(train_images, train_labels) %<-% fashion_mnist$train
c(test_images, test_labels) %<-% fashion_mnist$test
label中的每一个编号对应一种服饰:
class_names = c('T-shirt/top',
'Trouser',
'Pullover',
'Dress',
'Coat',
'Sandal',
'Shirt',
'Sneaker',
'Bag',
'Ankle boot')
训练集由60000个28*28像素的图像组成:
dim(train_images)
[1] 60000 28 28
dim(train_labels)
[1] 60000
train_labels[1:20]
[1] 9 0 0 3 0 2 7 2 5 5 0 9 5 5 7 9 1 0 6 4
dim(test_images)
[1] 10000 28 28
dim(test_labels)
[1] 10000
数据预处理:
library(tidyr)
library(ggplot2)
image_1 <- as.data.frame(train_images[1, , ])
colnames(image_1) <- seq_len(ncol(image_1))
image_1$y <- seq_len(nrow(image_1))
image_1 <- gather(image_1, "x", "value", -y)
image_1$x <- as.integer(image_1$x)
ggplot(image_1, aes(x = x, y = y, fill = value)) +
geom_ti