naturomics / CapsLayer

CapsLayer: An advanced library for capsule theory
Apache License 2.0
361 stars 116 forks source link

ValueError when using cl.layers.conv2d #28

Closed tobystaines closed 6 years ago

tobystaines commented 6 years ago

I am trying to build my own capsule network using this library, but receive a value error from tf.get_variable() in the transforming() function called within the cl.layers.conv2d layer.

Since the variable scope is set within the transforming function I don't think this is something I can fix without poking around in the source code (but happy to be corrected if it is something I have done wrong).

When running:

class basicCapsNet(object): ... with tf.variable_scope('Conv_Caps'): net, activation = cl.layers.conv2d(inputs=net, activation=activation, filters=1, out_caps_dims=[16,1], kernel_size=1, strides=(1,1), padding="valid", routing_method="DynamicRouting", reuse=reuse) self.conv_caps = (net,activation) self.voice_mask = net ... model = basicCapsNet(mixed_mag, voice_mag, is_training=False)

I get an error:

ValueError: Variable basic_caps_net/Conv_Caps/conv2d/transforming/transformation_matrix does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=tf.AUTO_REUSE in VarScope?

naturomics commented 6 years ago

This error is caused by your setting reuse being True before you do have a cl.layers.conv2d layer with the same scope/name. Reuse means you want to share the weights of this layer with some layer that exists, meaning you should have created a same scope/name layer BEFORE reuse=True. It's a feature of tf.get_variable(), not an error of the code. Hope my explanation is clear enough.