TobSta TobSta - 1 year ago 172
Python Question

lasagne.layers.DenseLayer: "__init__() takes at least 3 arguments"

I'm using Lasagne+Theano to create a ResNet and am struggling with the use of DenseLayer. If i use the example on it works.

l_in = InputLayer((100, 20))
l1 = DenseLayer(l_in, num_units=50)

But if I want to use it in my project:

#other layers

resnet['res5c_branch2c'] = ConvLayer(resnet['res5c_branch2b'], num_filters=2048, filter_size=1, pad=0, flip_filters=False)
resnet['pool5'] = PoolLayer(resnet['res5c'], pool_size=7, stride=1, mode='average_exc_pad', ignore_border=False)
resnet['fc1000'] = DenseLayer(resnet['pool5'], num_filter=1000)

Traceback (most recent call last):File "", line 167, in <module>
resnet['fc1000'] = DenseLayer(resnet['pool5'], num_filter=1000)TypeError: __init__() takes at least 3 arguments (2 given)

Answer Source

DenseLayer takes two positional arguments: incoming, num_units. You are instantiating it like this:

DenseLayer(resnet['pool5'], num_filter=1000)

Note that this is different than the example code:

DenseLayer(l_in, num_units=50)

Since you are passing a keyword argument that is not num_units as the second argument, I think num_filter is being interpreted as one of the **kwargs, and DenseLayer is still wanting thatnum_units` argument, and raising an error since you don't provide it.

You can either provide a num_units argument before num_filter, or if that was just a typo, change num_filter to num_units. (The second option seems more likely to me since, although I am not familiar with the library that you are using, I do not see any reference to num_filter in the documentation you linked, although some classes seem to take a num_filters - note the trailing s - argument.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download