我已经在RVersion3.4.3中安装了mx.net包,使用
cran <- getOption("repos") cran"dmlc“<- "https://s3-us-west-2.amazonaws.com/apache-mxnet/R/CRAN/” 选项(repos= cran) install.packages("mxnet")
在估计神经网络时会出现一些问题。
在下面的代码中,我使用了https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+%28Original%29上可用的乳腺癌数据集。
BC_data <- read.csv("Data_breastcancer.csv", sep = ";")
# generate a train and test set
trainIndex = sample(1:nrow(BC_data), size = round(0.8*nrow(BC_data)), replace=FALSE)
train_data <- BC_data[trainIndex,]
test_data <- BC_data[-trainIndex,]
X_train <- train_data[,c(-1,-11)]
y_train <- train_data[,11]
# estimate neural network
model = mx.mlp(as.matrix(X_train), as.numeric(y_train), hidden_node = 10, out_node = 2, out_activation = "softmax", learning.rate = 0.1, num.round = 20)但是,与其以迭代方式返回精度值,我得到的唯一输出是
Start training with 1 devices
Warning message:
In mx.model.select.layout.train(X, y) :
Auto detect layout of input matrix, use rowmajor..因此,迭代过程似乎根本没有开始。
有人知道如何解决这个问题吗?
发布于 2018-01-29 21:49:10
你需要做一些调整:
下面是适用于乳腺癌的代码-wisconsin.data文件,我从您的链接中找到了该文件:
library(mxnet)
BC_data <- read.csv(
file = "breast-cancer-wisconsin.data",
sep = ",",
header = FALSE,
colClasses = c(rep("numeric", 11)),
na.strings = c("", "?") # Few records of the 7th column contain "?" - treat "?" as NA
)
BC_data[is.na(BC_data)] <- 0 # Replace NA with zeroes
# generate a train and test set
trainIndex = sample(1:nrow(BC_data), size = round(0.8*nrow(BC_data)), replace=FALSE)
train_data <- BC_data[trainIndex,]
test_data <- BC_data[-trainIndex,]
X_train <- train_data[,c(-1,-11)]
y_train <- train_data[,11]
# estimate neural network
model = mx.mlp(
data = as.matrix(X_train),
label = as.numeric(ifelse(y_train == 2, 0, 1)), # Replace classes with 0 and 1
hidden_node = 10,
out_node = 2,
out_activation = "softmax",
learning.rate = 0.1,
num.round = 20,
array.layout = "rowmajor", # get rid of a nasty warning
eval.metric=mx.metric.accuracy # set Accuracy as a metric
)如果运行此代码,将得到以下输出:
Start training with 1 devices
[1] Train-accuracy=0.64453125
[2] Train-accuracy=0.6515625
[3] Train-accuracy=0.65625
[4] Train-accuracy=0.9
[5] Train-accuracy=0.95625
[6] Train-accuracy=0.95
[7] Train-accuracy=0.94375
[8] Train-accuracy=0.9328125
[9] Train-accuracy=0.93125
[10] Train-accuracy=0.9328125
[11] Train-accuracy=0.9375
[12] Train-accuracy=0.9390625
[13] Train-accuracy=0.9484375
[14] Train-accuracy=0.95
[15] Train-accuracy=0.9453125
[16] Train-accuracy=0.946875
[17] Train-accuracy=0.9484375
[18] Train-accuracy=0.95
[19] Train-accuracy=0.9484375
[20] Train-accuracy=0.9515625https://stackoverflow.com/questions/48490010
复制相似问题