# 加载所需库
library(keras)
reticulate::use_condaenv("tf")
# 读取并预处理数据
# 安装和加载rattle包
if (!require(rattle)) {
pak::pak("rattle")
}
# 加载wine数据集
data(wine, package = "rattle")
# 将数据分为训练集和测试集
set.seed(123)
indices <- sample(1:nrow(wine), nrow(wine)*0.7)
train_data <- wine[indices, ] |> as.numeric()
test_data <- wine[-indices, ] |> as.numeric()
# 分离特征和标签
train_X <- as.matrix(train_data[, -1])
train_Y <- train_data$type
test_X <- as.matrix(test_data[, -1])
test_Y <- test_data$type
# 数据标准化
mean <- apply(train_X, 2, mean, na.rm = TRUE)
std <- apply(train_X, 2, sd, na.rm = TRUE)
train_X <- scale(train_X, center = mean, scale = std)
test_X <- scale(test_X, center = mean, scale = std)
# 构建神经网络模型
model <- keras_model_sequential()
model %>%
layer_dense(units = 64, activation = 'relu', input_shape = ncol(train_X)) %>%
layer_dense(units = 32, activation = 'relu') %>%
layer_dense(units = 1)
# 编译模型
model %>% compile(
loss = 'mse',
optimizer = optimizer_rmsprop(),
metrics = c('mae')
)
# 训练模型
history <- model %>% fit(
train_X, train_Y,
epochs = 300, batch_size = 16,
validation_split = 0.2
)