conv neural network - Regression using MXNet in R with image recognition -
so trying use image recognition output regression style number using mxnet package in r using cnn.
i have used basis of analysis: https://rstudio-pubs-static.s3.amazonaws.com/236125_e0423e328e4b437888423d3821626d92.html
this image recognition analysis using mxnet in r using cnn, have followed these steps prepare data preprocessing doing same steps, resizing, grayscaling.
my "image" dataset looks like this, have 784 columns of pixels, , last column numeric column "label" trying predict be: 1132, 1491, 845, etc.
from there, create training , testing:
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 , 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)) now onto using mxnet, causing problems, not sure doing wrong:
library(mxnet) ## model mx_data <- mx.symbol.variable('data') ## 1st convolutional layer 5x5 kernel , 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 , 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 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 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 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)) i error:
[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 different 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 different source. target: (100,) source: (100,2) i can work using if use
nn_model <- mx.symbol.softmaxoutput(data = fcl_2)
and keep rmse there, doesn't improve performance of model after 30 iterations.
thanks!
your last connected layer fcl_2 <- mx.symbol.fullyconnected(data = tanh_3, num_hidden = 2) creates output shape of (batch_size, 2), reshaping results in (2 * batch_size).
then doing (mx.symbol.reshape(fcl_2, shape = 0) - label), i.e. trying subtract tensors of following shapes: (200) - (100), cannot work.
instead want change last connected layer have 1 hidden unit fcl_2 <- mx.symbol.fullyconnected(data = tanh_3, num_hidden = 1), trying learn network predicts single scalar output.

Comments
Post a Comment