szha commented on a change in pull request #8512: gluon rnn refactor URL: https://github.com/apache/incubator-mxnet/pull/8512#discussion_r150146517
########## File path: python/mxnet/gluon/rnn/rnn_cell.py ########## @@ -321,56 +345,35 @@ def __init__(self, hidden_size, activation='tanh', i2h_weight_initializer=None, h2h_weight_initializer=None, i2h_bias_initializer='zeros', h2h_bias_initializer='zeros', input_size=0, prefix=None, params=None): - super(RNNCell, self).__init__(prefix=prefix, params=params) - self._hidden_size = hidden_size - self._activation = activation - self._input_size = input_size - self.i2h_weight = self.params.get('i2h_weight', shape=(hidden_size, input_size), - dtype=None, init=i2h_weight_initializer, - allow_deferred_init=True) - self.h2h_weight = self.params.get('h2h_weight', shape=(hidden_size, hidden_size), - dtype=None, init=h2h_weight_initializer, - allow_deferred_init=True) - self.i2h_bias = self.params.get('i2h_bias', shape=(hidden_size,), - dtype=None, init=i2h_bias_initializer, - allow_deferred_init=True) - self.h2h_bias = self.params.get('h2h_bias', shape=(hidden_size,), - dtype=None, init=h2h_bias_initializer, - allow_deferred_init=True) - - def state_info(self, batch_size=0): - return [{'shape': (batch_size, self._hidden_size), '__layout__': 'NC'}] - - def _alias(self): - return 'rnn' + super(RNNCell, self).__init__(hidden_size, + i2h_weight_initializer, h2h_weight_initializer, + i2h_bias_initializer, h2h_bias_initializer, + input_size, 1, 1, 'rnn', prefix, params) + with self.name_scope(): + self.activation = HybridLambda(activation, prefix='') def __repr__(self): s = '{name}({mapping}' if hasattr(self, '_activation'): s += ', {_activation}' s += ')' - shape = self.i2h_weight.shape + shape = self.i2h.weight.shape mapping = '{0} -> {1}'.format(shape[1] if shape[1] else None, shape[0]) return s.format(name=self.__class__.__name__, mapping=mapping, **self.__dict__) - def hybrid_forward(self, F, inputs, states, i2h_weight, - h2h_weight, i2h_bias, h2h_bias): - prefix = 't%d_'%self._counter - i2h = F.FullyConnected(data=inputs, weight=i2h_weight, bias=i2h_bias, - num_hidden=self._hidden_size, - name=prefix+'i2h') - h2h = F.FullyConnected(data=states[0], weight=h2h_weight, bias=h2h_bias, - num_hidden=self._hidden_size, - name=prefix+'h2h') - output = self._get_activation(F, i2h + h2h, self._activation, - name=prefix+'out') + def hybrid_forward(self, F, inputs, states): + if F is symbol: + prefix = 't%d_out'%self._counter + output = self.activation(self.gate_forward(inputs, states), prefix) Review comment: OK. I think the readability improvement is marginal for regular RNN anyway. Will revert this part. ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services