Initialize weights with predefined values


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


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.


Is this requesting the same functionality?


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

class TensorInitializer(mx.init.Initializer):

def init(self, 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?