You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by gi...@git.apache.org on 2017/08/28 18:33:11 UTC

[GitHub] jeremiedb commented on issue #7641: MXNetR: how can i get the shape (dimension) of a certain symbol

jeremiedb commented on issue #7641: MXNetR: how can i get the shape (dimension) of a certain symbol
URL: https://github.com/apache/incubator-mxnet/issues/7641#issuecomment-325439440
 
 
   There's typically no need to specify shape of data input when building the symbolic network. This will typically will be set at training time when the model is bind and the shapes infered from what the iterator provides as input data. This allows the same network to be trained with different batch sizes. 
   
   For the random noise, the shapes can be infered so there's no need to specify it. For example: 
   ```
   data <- mx.symbol.Variable("data")
   noise <- mx.symbol.random_normal(loc=0, scale=1, name = "noise")
   data_noise <- data + noise
   graph.viz(data_noise, shape=c(2:3))
   ```
   
   To infer the shape for a given data input shape, you need to pass the required input shapes as a list: `data_noise$infer.shape(list(data=2:3))`. This provides the shapes of each arguments in the network. To get the output shapes of each operator, you can use: `data_noise$get.internals()$infer.shape(list(data=2:3))$out.shapes`
   Only the data shape is required to get the shapes of the overall network. Other shapes for data can be specified: the same model could be reused if the batch size or number of input features were different. 
   
   Graph can confirm: `graph.viz(data_noise, shape=c(2:3))`
   ![rplot](https://user-images.githubusercontent.com/18605903/29787452-fd3e1510-8bfc-11e7-8a51-f064ead83b2f.png)
   
   You can finally validate that the noise layer operates as expected and generates new random noise at each batch iteration:  
   
   ```
   exec <- mxnet:::mx.symbol.bind(symbol = total, ctx = mx.cpu(), 
                                  arg.arrays = list(data=data.array), aux.arrays = NULL,
                                  grad.reqs = c("null"))
   mx.exec.forward(exec, is.train = T)
   exec$outputs
   ```
   
              [,1]       [,2]       [,3]
   [1,] -0.9185634 -0.7457144 -1.2080863
   [2,]  2.0088325  0.2863084  0.5604595
   
   ```
   mx.exec.forward(exec, is.train = T)
   exec$outputs
   ```
             [,1]       [,2]      [,3]
   [1,] 1.8140212 -1.5227430 -2.515245
   [2,] 0.9697598 -0.5285375 -1.889090
   
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services