I’m working on a Convolutional Sentiment Analysis model with gluon using a custom block. When trying to pass in data to my custom block, I encountered an error:
RuntimeError: Parameter convolutionlayer0_conv0_bias has not been initialized. Note that you should initialize parameters and create Trainer with Block.collect_params() instead of Block.params because the later does not include Parameters of nested child Blocks
My custom block is implemented like this:
class ConvolutionLayer(Block):
def __init__(self, **kwargs):
super(ConvolutionLayer, self).__init__(**kwargs)
self.conv_blocks = []
self.max_blocks = []
# self.ngram_conv = []
with self.name_scope():
for sz in filter_sizes:
conv = gluon.nn.Conv2D(channels=num_filters, kernel_size=(sz, 400), strides=(1, 400))
max = gluon.nn.MaxPool2D(pool_size=(2, 2))
self.conv_blocks.append(conv)
self.max_blocks.append(max)
self.out = gluon.nn.Dense(5)
def forward(self, x):
for conv, max in zip(self.conv_blocks, self.max_blocks):
conv0 = nd.relu(conv(x)).reshape(0, -1)
max0 = max(conv0)
x = nd.concat(x, max0, dim=1)
x = self.out(x)
return x
And I initialized it like this:
net = ConvolutionLayer()
#initialize
print('initializing')
net.collect_params().initialize(mx.init.Xavier(magnitude=2.24, rnd_type='gaussian'), ctx=ctx)
#Softmax
softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()
#Optimizer
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': .1})
Can somebody help me with the error? Ignore the hardcoded dimensions for now since I just want to make it work… Thank you so much!