使用R语言实现利用卷积神经网络对MNIST数据集进行分类
时间: 2023-12-29 18:03:18 浏览: 122
使用卷积神经网络对mnist数据集进行分析
首先,需要安装并加载以下 R 包:keras、tidyverse、reshape2
然后,导入 MNIST 数据集,将其转换为所需格式:
```r
library(keras)
library(tidyverse)
library(reshape2)
# 导入 MNIST 数据集
mnist <- dataset_mnist()
# 将训练集和测试集转换为 data.frame
x_train <- mnist$train$x %>% array_reshape(dim = c(nrow(.), 28 * 28)) %>% as.data.frame()
y_train <- mnist$train$y %>% as.data.frame()
x_test <- mnist$test$x %>% array_reshape(dim = c(nrow(.), 28 * 28)) %>% as.data.frame()
y_test <- mnist$test$y %>% as.data.frame()
# 将类别变量转换为因子
y_train <- factor(y_train)
y_test <- factor(y_test)
# 将像素值缩放到 0-1 范围内
x_train <- x_train / 255
x_test <- x_test / 255
# 将输入数据转换为矩阵
x_train <- as.matrix(x_train)
x_test <- as.matrix(x_test)
# 将输出数据转换为 one-hot 编码
y_train <- to_categorical(y_train)
y_test <- to_categorical(y_test)
```
接下来,构建卷积神经网络模型,包括两个卷积层和两个全连接层:
```r
# 构建卷积神经网络模型
model <- keras_model_sequential() %>%
layer_reshape(target_shape = c(28, 28, 1), input_shape = c(28 * 28)) %>%
layer_conv_2d(filters = 32, kernel_size = c(3, 3), activation = "relu") %>%
layer_max_pooling_2d(pool_size = c(2, 2)) %>%
layer_conv_2d(filters = 64, kernel_size = c(3, 3), activation = "relu") %>%
layer_max_pooling_2d(pool_size = c(2, 2)) %>%
layer_flatten() %>%
layer_dense(units = 128, activation = "relu") %>%
layer_dropout(rate = 0.5) %>%
layer_dense(units = 10, activation = "softmax")
# 编译模型
model %>% compile(
loss = "categorical_crossentropy",
optimizer = optimizer_rmsprop(),
metrics = c("accuracy")
)
```
然后,训练模型:
```r
# 训练模型
history <- model %>% fit(
x_train, y_train,
epochs = 10, batch_size = 128,
validation_split = 0.2
)
```
最后,评估模型的性能:
```r
# 评估模型性能
model %>% evaluate(x_test, y_test)
```
完整的代码如下:
```r
library(keras)
library(tidyverse)
library(reshape2)
# 导入 MNIST 数据集
mnist <- dataset_mnist()
# 将训练集和测试集转换为 data.frame
x_train <- mnist$train$x %>% array_reshape(dim = c(nrow(.), 28 * 28)) %>% as.data.frame()
y_train <- mnist$train$y %>% as.data.frame()
x_test <- mnist$test$x %>% array_reshape(dim = c(nrow(.), 28 * 28)) %>% as.data.frame()
y_test <- mnist$test$y %>% as.data.frame()
# 将类别变量转换为因子
y_train <- factor(y_train)
y_test <- factor(y_test)
# 将像素值缩放到 0-1 范围内
x_train <- x_train / 255
x_test <- x_test / 255
# 将输入数据转换为矩阵
x_train <- as.matrix(x_train)
x_test <- as.matrix(x_test)
# 将输出数据转换为 one-hot 编码
y_train <- to_categorical(y_train)
y_test <- to_categorical(y_test)
# 构建卷积神经网络模型
model <- keras_model_sequential() %>%
layer_reshape(target_shape = c(28, 28, 1), input_shape = c(28 * 28)) %>%
layer_conv_2d(filters = 32, kernel_size = c(3, 3), activation = "relu") %>%
layer_max_pooling_2d(pool_size = c(2, 2)) %>%
layer_conv_2d(filters = 64, kernel_size = c(3, 3), activation = "relu") %>%
layer_max_pooling_2d(pool_size = c(2, 2)) %>%
layer_flatten() %>%
layer_dense(units = 128, activation = "relu") %>%
layer_dropout(rate = 0.5) %>%
layer_dense(units = 10, activation = "softmax")
# 编译模型
model %>% compile(
loss = "categorical_crossentropy",
optimizer = optimizer_rmsprop(),
metrics = c("accuracy")
)
# 训练模型
history <- model %>% fit(
x_train, y_train,
epochs = 10, batch_size = 128,
validation_split = 0.2
)
# 评估模型性能
model %>% evaluate(x_test, y_test)
```
运行结果会输出模型在测试集上的准确率。
阅读全文