使用带有图像识别的R中的MXNet进行回归

时间:2017-07-26 07:31:56

标签: r conv-neural-network mxnet

所以我尝试使用图像识别来使用CNN中的mxnet包在R中输出回归样式编号。

我已将此作为分析的基础:https://rstudio-pubs-static.s3.amazonaws.com/236125_e0423e328e4b437888423d3821626d92.html

这是使用CNN在R中使用mxnet的图像识别分析,因此我按照这些步骤通过执行相同的步骤,调整大小,灰度来准备我的数据以进行预处理。

我的"图像"数据集看起来像这样,我有784列像素,最后一列是一个带有"标签的数字列"我试图预测所以它将是:1132,1491,845等。

I have pixels in each cell with their numeric values, 784 columns of pixels and the last column is a numeric column with the "label" that I am trying to predict using the images

从那里,我创建了一个培训和测试:

    library(pbapply)
library(caret)
## test/training partitions
training_index <- createDataPartition(image$STOPPING_TIME, p = .9, times = 1)
training_index <- unlist(training_index)
train_set <- image[training_index,]
dim(train_set)
test_set <- image[-training_index,]
dim(test_set)


## Fix train and test datasets
train_data <- data.matrix(train_set)
train_x <- t(train_data[, -785])
train_y <- train_data[,785]
train_array <- train_x
dim(train_array) <- c(28, 28, 1, ncol(train_x))

test_data <- data.matrix(test_set)
test_x <- t(test_set[,-785])
test_y <- test_set[,785]
test_array <- test_x
dim(test_array) <- c(28, 28, 1, ncol(test_x))

现在我开始使用mxnet,这是造成问题的原因,不确定我做错了什么:

library(mxnet)
## Model
mx_data <- mx.symbol.Variable('data')
## 1st convolutional layer 5x5 kernel and 20 filters.
conv_1 <- mx.symbol.Convolution(data = mx_data, kernel = c(5, 5), num_filter = 20)
tanh_1 <- mx.symbol.Activation(data = conv_1, act_type = "tanh")
pool_1 <- mx.symbol.Pooling(data = tanh_1, pool_type = "max", kernel = c(2, 2), stride = c(2,2 ))
## 2nd convolutional layer 5x5 kernel and 50 filters.
conv_2 <- mx.symbol.Convolution(data = pool_1, kernel = c(5,5), num_filter = 50)
tanh_2 <- mx.symbol.Activation(data = conv_2, act_type = "tanh")
pool_2 <- mx.symbol.Pooling(data = tanh_2, pool_type = "max", kernel = c(2, 2), stride = c(2, 2))
## 1st fully connected layer
flat <- mx.symbol.Flatten(data = pool_2)
fcl_1 <- mx.symbol.FullyConnected(data = flat, num_hidden = 500)
tanh_3 <- mx.symbol.Activation(data = fcl_1, act_type = "tanh")
## 2nd fully connected layer
fcl_2 <- mx.symbol.FullyConnected(data = tanh_3, num_hidden = 2)
## Output
label <- mx.symbol.Variable("label")
NN_model <- mx.symbol.MakeLoss(mx.symbol.square(mx.symbol.Reshape(fcl_2, shape = 0) - label))


## Set seed for reproducibility
mx.set.seed(100)


## Train on 1200 samples
model <- mx.model.FeedForward.create(NN_model, X = train_array, y = train_y,
                                     num.round = 30,
                                     array.batch.size = 100,
                                    initializer=mx.init.uniform(0.002), 
                                     learning.rate = 0.05,
                                     momentum = 0.9,
                                     wd = 0.00001,
                                     eval.metric = mx.metric.rmse)
                                     epoch.end.callback = mx.callback.log.train.metric(100))

我收到错误:

[00:30:08] D:\Program Files (x86)\Jenkins\workspace\mxnet\mxnet\dmlc-core\include\dmlc/logging.h:308: [00:30:08] d:\program files (x86)\jenkins\workspace\mxnet\mxnet\src\operator\tensor\./matrix_op-inl.h:134: Check failed: oshape.Size() == dshape.Size() (100 vs. 200) Target shape size is different to source. Target: (100,)
Source: (100,2)
Error in symbol$infer.shape(list(...)) : 
  Error in operator reshape9: [00:30:08] d:\program files (x86)\jenkins\workspace\mxnet\mxnet\src\operator\tensor\./matrix_op-inl.h:134: Check failed: oshape.Size() == dshape.Size() (100 vs. 200) Target shape size is different to source. Target: (100,)
Source: (100,2)

如果我使用

,我可以使用它

NN_model&lt; - mx.symbol.SoftmaxOutput(data = fcl_2)

并保留rmse,但在30次迭代后它并没有提高我的模型的性能。

谢谢!

1 个答案:

答案 0 :(得分:2)

您上次完全连接的图层fcl_2 <- mx.symbol.FullyConnected(data = tanh_3, num_hidden = 2)会创建(batch_size, 2)的输出形状,重新塑造它会产生(2 * batch_size)

然后你正在做(mx.symbol.Reshape(fcl_2, shape = 0) - label),即你试图减去以下形状的张量:(200) - (100),这是无效的。

相反,您可能想要做的是将最后一个完全连接的图层更改为只有一个隐藏单元fcl_2 <- mx.symbol.FullyConnected(data = tanh_3, num_hidden = 1),正如您所说,您正在尝试学习预测单个标量输出的网络。