Initialize weights with predefined values


#1

Is it possible to assign values (e.g. in form of a numpy array) to a variable in a module? If yes, what is the best way? For example,

data = mx.sym.Variable(‘data’)
weights = mx.sym.Variable(‘weights’)
net = mx.sym.FullyConnected(data=data, weight=weights, name=‘fc1’, num_hidden=128)

Need to pass a numpy array to ‘weights’ .


#2

How about using the Load initializer (mxnet.initializer.Load) to initialize the weight?
mx.sym.Variable accepts an initializer too.

You can also use module’s init_params method with the mxnet.initializer.Mixed to use the mxnet.initializer.Load initializer for a particular weight.


#3

Is this requesting the same functionality?


#4

Yes. I use the following initializer (suggested by David)

@mx.init.register
class TensorInitializer(mx.init.Initializer):

def init(self, ini_tensor):
super(TensorInitializer,self).init(ini_tensor=ini_tensor)
self.ini_tensor = ini_tensor

def _init_weight(self, _, arr):
arr[:] = self.ini_tensor

It raises an error:
TypeError: array([…]]) is not JSON serializable

Any suggestions?