apache / mxnet

Lightweight, Portable, Flexible Distributed/Mobile Deep Learning with Dynamic, Mutation-aware Dataflow Dep Scheduler; for Python, R, Julia, Scala, Go, Javascript and more
https://mxnet.apache.org
Apache License 2.0
20.78k stars 6.79k forks source link

io.cc:54: Data and label shape in-consistent #7336

Closed dma100180 closed 7 years ago

dma100180 commented 7 years ago

Hi, I am creating a neural network with R and MXnet, the case is that when I work with a single neuron output it work well but when I put in tain.y two fields and I put the output layer two neurons measure the error:

Io.cc:54: Data and label shape in-consistent

I do not quite understand why

The network that works without problems is:

train.x<-DataNeurona[train.ind,-length(DataNeurona[1,])] train.y<-DataNeurona[train.ind,length(DataNeurona[1,])]

test.x<-DataNeurona[-c(1:200,train.ind),-length(DataNeurona[1,])] test.y<-DataNeurona[-c(1:200,train.ind),length(DataNeurona[1,])]

data <- mx.symbol.Variable("data") fc1<-mx.symbol.FullyConnected(data, name="fc1", num_hidden=40) #weight=w, act1<-mx.symbol.Activation(fc1,name="sigmoid1",act_type="sigmoid") fc2<-mx.symbol.FullyConnected(act1, name="fc2", num_hidden=50) act2<-mx.symbol.Activation(fc2,name="tanh1",act_type="sigmoid") fc4<-mx.symbol.FullyConnected(act2,name="fc4",num_hidden=1) lro=mx.symbol.LinearRegressionOutput(data=fc4, grad.scale=1) mx.set.seed(0)

model <- mx.model.FeedForward.create(symbol= lro, X=train.x, y=train.y, ctx=mx.cpu(),
num.round=25, array.batch.size=NumBatch, learning.rate=0.05, momentum=0.9,
eval.metric=mx.metric.mse)

And I only change this to take two exit fields train.x<-DataNeurona[train.ind,-c(length(DataNeurona[1,]),length(DataNeurona[1,])-1)] train.y<-DataNeurona[train.ind,c(length(DataNeurona[1,]),length(DataNeurona[1,])-1)]

And into the model num_hidden=0: fc4<-mx.symbol.FullyConnected(act2,name="fc4",num_hidden=2) lro=mx.symbol.LinearRegressionOutput(data=fc4, grad.scale=1)

Thank you

thirdwing commented 7 years ago

Please provide a reproducible example. We don't know what DataNeurona is.

dma100180 commented 7 years ago

Hi, sorry, DataNeurona is a list with market data from the IBEX35 index (Spain) and Calculated and normalized fields, I attach file as is it in DataNeurona.

In the next step, I convert the list DataNeurona into matrix: DataNeurona<-as.matrix(DataNeurona)

And then I do the steps from my first message

Many thanks

MyData2.zip

thirdwing commented 7 years ago

Are you using the prebuilt pkg?

On my Linux machine with the latest code, it works well

library(readr)
DataNeurona <- read_csv("MyData2.csv")
DataNeurona <- as.matrix(DataNeurona)

train.ind <- c(1:100)
train.x <- DataNeurona[train.ind, -length(DataNeurona[1, ])]
train.y <- DataNeurona[train.ind, length(DataNeurona[1, ])]
test.x <- DataNeurona[-train.ind, length(DataNeurona[1, ])]
test.y <- DataNeurona[-train.ind, length(DataNeurona[1, ])]

library(mxnet)

data <- mx.symbol.Variable("data")
fc1 <-
  mx.symbol.FullyConnected(data, name = "fc1", num_hidden = 40) #weight=w,
act1 <- mx.symbol.Activation(fc1, name = "sigmoid1", act_type = "sigmoid")
fc2 <- mx.symbol.FullyConnected(act1, name = "fc2", num_hidden = 50)
act2 <- mx.symbol.Activation(fc2, name = "tanh1", act_type = "sigmoid")
fc4 <- mx.symbol.FullyConnected(act2, name = "fc4", num_hidden = 1)
lro <- mx.symbol.LinearRegressionOutput(data = fc4, grad.scale = 1)
mx.set.seed(0)

model <- mx.model.FeedForward.create(
  symbol = lro,
  X = train.x,
  y = train.y,
  ctx = mx.cpu(),
  num.round = 25,
  array.batch.size = 20,
  learning.rate = 0.05,
  momentum = 0.9,
  eval.metric = mx.metric.mse
)
Start training with 1 devices
[1] Train-mse=0.189338970604364
[2] Train-mse=0.182918034135271
[3] Train-mse=0.139369296805449
[4] Train-mse=0.0981672365474814
[5] Train-mse=0.090259424948582
[6] Train-mse=0.091202815317992
[7] Train-mse=0.0735728471551251
[8] Train-mse=0.0755325079735637
[9] Train-mse=0.0776457090260113
[10] Train-mse=0.0703273524582357
[11] Train-mse=0.0727759200215761
[12] Train-mse=0.073669241397235
[13] Train-mse=0.0705466593146721
[14] Train-mse=0.0721189324018792
[15] Train-mse=0.0723587683907677
[16] Train-mse=0.0710235608978592
[17] Train-mse=0.0718809927804486
[18] Train-mse=0.0718809992951069
[19] Train-mse=0.0713206787760761
[20] Train-mse=0.0717545830533599
[21] Train-mse=0.071694452664629
[22] Train-mse=0.0714664637380885
[23] Train-mse=0.0716749304881256
[24] Train-mse=0.0716162021040038
[25] Train-mse=0.0715267213384503
Warning message:
In mx.model.select.layout.train(X, y) :
  Auto detect layout of input matrix, use rowmajor..
dma100180 commented 7 years ago

Hi, yes, it works like this with an output field, but when they are 2 or more, I change the fc4 layer with 2 neurons and train.y with two fields and it gives me the error:

Error in mx.io.internal.arrayiter(as.array(data), as.array(label), unif.rnds, : io.cc:54: Data and label shape in-consistent

I put all the code that you had but with the changes in train.x train.y and fc4

Thanks for everything

library(readr) DataNeurona <- read_csv("D:/DATOS_PROYECTO/MyData2.csv") DataNeurona <- as.matrix(DataNeurona)

train.ind <- c(1:100) train.x<-DataNeurona[train.ind,-c(length(DataNeurona[1,]),length(DataNeurona[1,])-1)] train.y<-DataNeurona[train.ind,c(length(DataNeurona[1,]),length(DataNeurona[1,])-1)] test.x <- DataNeurona[-train.ind, -c(length(DataNeurona[1,]),length(DataNeurona[1,])-1)] test.y <- DataNeurona[-train.ind, c(length(DataNeurona[1,]),length(DataNeurona[1,])-1)]

library(mxnet)

data <- mx.symbol.Variable("data") fc1 <- mx.symbol.FullyConnected(data, name = "fc1", num_hidden = 40) #weight=w, act1 <- mx.symbol.Activation(fc1, name = "sigmoid1", act_type = "sigmoid") fc2 <- mx.symbol.FullyConnected(act1, name = "fc2", num_hidden = 50) act2 <- mx.symbol.Activation(fc2, name = "tanh1", act_type = "sigmoid") fc4 <- mx.symbol.FullyConnected(act2, name = "fc4", num_hidden = 2) lro <- mx.symbol.LinearRegressionOutput(data = fc4, grad.scale = 1) mx.set.seed(0)

model <- mx.model.FeedForward.create( symbol = lro, X = train.x, y = train.y, ctx = mx.cpu(), num.round = 25, array.batch.size = 20, learning.rate = 0.05, momentum = 0.9, eval.metric = mx.metric.mse )

thirdwing commented 7 years ago

I think we have this kind of examples in our document. See https://github.com/apache/incubator-mxnet/blob/master/R-package/vignettes/fiveMinutesNeuralNetwork.Rmd#regression

data(BostonHousing, package="mlbench")
train.ind <- seq(1, 506, 3)
train.x <- data.matrix(BostonHousing[train.ind, -(13:14)])
train.y <- BostonHousing[train.ind, c(13:14)]
test.x <- data.matrix(BostonHousing[-train.ind, -(13:14)])
test.y <- BostonHousing[-train.ind, c(13:14)]

data <- mx.symbol.Variable("data")
fc2 <- mx.symbol.FullyConnected(data, num_hidden=2)
lro2 <- mx.symbol.LinearRegressionOutput(fc2)

mx.set.seed(0)
train_iter = mx.io.arrayiter(data = t(train.x), label = t(train.y))

model <- mx.model.FeedForward.create(lro2, X=train_iter,
                                     ctx=mx.cpu(), num.round=50, array.batch.size=20,
                                     learning.rate=2e-6, momentum=0.9)
dma100180 commented 7 years ago

Hello, yes, that was the problem, I changed it and I can continue!

Thank you very much for your help, regards