You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/12/30 20:34:03 UTC

[GitHub] jimmychou0704 opened a new issue #13748: custom loss cause "kernel died" in Jupyter notebook

jimmychou0704 opened a new issue #13748: custom loss cause "kernel died" in Jupyter notebook
URL: https://github.com/apache/incubator-mxnet/issues/13748
 
 
   ## Description
   I am implementing mxnet SoftmaxOutput from scratch as a warm up for a more complicated custom loss function. However when I fit the model my jupyter notebook dies immediately. 
   
   ## Minimum reproducible example
   Here is how I defined my custom loss function,
   ```
   class MySoftmaxOutput(mx.operator.CustomOp):
   
       def forward(self, is_train, req, in_data, out_data, aux):
           numerator = mx.nd.exp(in_data[0] - mx.nd.max(in_data[0]))
           denominator = mx.nd.sum(numerator, axis = 1, keepdims=True)
           self.assign(out_data[0], req[0], mx.nd.divide(numerator,denominator))
   
       def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
           #in_grad[0][:] = (out_data[0] - in_data[1])
           self.assign(in_grad[0], req[0], (out_data[0] - in_data[1]))
   
   @mx.operator.register('MySoftmaxOutput')
   class MySoftmaxOuptProp(mx.operator.CustomOpProp):
       
       def __init__(self):
           super(MySoftmaxOuptProp, self).__init__(need_top_grad=False)
   
       #Required.
       def list_arguments(self):
           return ['data', 'label'] 
   
       #Required.
       def infer_shape(self, in_shape):
           data_shape = in_shape[0]
           label_shape = (in_shape[0][0],)
           output_shape = in_shape[0]
           return [data_shape, label_shape], [output_shape], 
       def create_operator(self, ctx, shapes, dtypes):
           return MySoftmaxOutput()
   ```
   Then a model is constructed as following, 
   ```
   data = mx.sym.Variable('data')
   label = mx.sym.Variable('label')
   affine1 = mx.sym.FullyConnected(data=data,name='fc1',num_hidden=124)
   hidden1 = mx.sym.Activation(data=affine1, act_type='relu')
   affine2 = mx.sym.FullyConnected(data=data,name='fc2',num_hidden=2)
   out = mx.sym.Custom(data=affine2, label=label, name='softmax', op_type='MySoftmaxOutput')
   mod = mx.mod.Module(out, data_names=['data'], label_names=['label'], context=mx.cpu())
   mod.bind(data_shapes=nd_iter.provide_data, label_shapes=nd_iter.provide_label)
   mod.init_params(initializer=mx.init.Xavier())
   ```
   So far so good. But when I run` mod.fit(nd_iter, num_epoch=10, optimizer='adam')` my jupyter notebook died immediately. The data set I used is small (less than 10mb).
   ## What have you tried to solve it?
   
   1.  Used mx.sym.SoftmaxOutput then everything is working.
   

----------------------------------------------------------------
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