tensorflow / kfac

An implementation of KFAC for TensorFlow
Apache License 2.0
197 stars 40 forks source link

AttributeError: 'LayerCollection' object has no attribute 'auto_register_layers' #21

Closed voaneves closed 5 years ago

voaneves commented 5 years ago

Hi there!

While using tf.Keras, with tensorflow backend, I ran into the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-c84bd3e821f4> in <module>()
   2379     model = create_model(optimizer = RMSprop(), loss = clipped_error,
   2380                             stack = nb_frames, input_size = board_size,
-> 2381                             output_size = game.nb_actions)
   2382     target = None
   2383     sess.run(tf.global_variables_initializer())

<ipython-input-4-c84bd3e821f4> in create_model(optimizer, loss, stack, input_size, output_size, dueling, cnn)
   1149 
   1150     # Register layers.
-> 1151     layer_collection.auto_register_layers()
   1152 
   1153     # Construct training ops.

AttributeError: 'LayerCollection' object has no attribute 'auto_register_layers'

I create my model as follows:

def create_model(optimizer, loss, stack, input_size, output_size, dueling = False):
    inputs = tf.keras.layers.Input(shape = (stack, input_size, input_size))

    net = tf.keras.layers.Conv2D(32, (3, 3), activation = 'relu')(inputs)
    net = tf.keras.layers.Conv2D(64, (2, 2), activation = 'relu')(net)
    net = tf.keras.layers.Conv2D(64, (1, 1), activation = 'relu')(net)
    net = tf.keras.layers.Flatten()(net)

    final = tf.keras.layers.Dense(3136, activation = 'relu')(net)
    final = tf.keras.layers.Dense(output_size)(final)

    model = tf.keras.models.Model(inputs = inputs, outputs = final)
    outputs = [layer.output for layer in model.layers]

    # Register loss.
    layer_collection = kfac.LayerCollection()
    layer_collection.register_categorical_predictive_distribution(outputs)

    # Register layers.
    layer_collection.auto_register_layers()

    # Construct training ops.
    optimizer = opt.KfacOptimizer(learning_rate=0.0001,
                                  cov_ema_decay=0.95,
                                  damping=0.001,
                                  layer_collection=layer_collection,
                                  momentum=0.9)
    model.compile(optimizer = optimizer, loss = loss)

    return model

Is this expected? Am I doing anything wrong?

Regards, Victor.

voaneves commented 5 years ago

Reading about similar problems, I found out that a common cause is using different ammount of spaces in indentation. My setup uses 4 and yours uses 2. Do you guys know ny fix for that?

After listing all available methods, 'auto_register_layer' is not available. Listing all available methods:

CODE

layer_collection = kfac.LayerCollection()

method_list = [func for func in dir(layer_collection) if callable(getattr(layer_collection, func)) and not func.startswith("__")]
print(method_list)

OUTPUT

['_add_uses', '_get_block_type', '_get_linked_approx', '_get_use_count_map', 'as_default', 'check_registration', 'create_subgraph', 'define_linked_parameters', 'eval_losses', 'eval_losses_on_samples', 'get_blocks', 'get_factors', 'make_or_get_factor', 'register_block', 'register_categorical_predictive_distribution', 'register_conv2d', 'register_conv2d_multi', 'register_convolution', 'register_depthwise_conv2d', 'register_embedding', 'register_embedding_multi', 'register_fully_connected', 'register_fully_connected_multi', 'register_generic', 'register_loss_function', 'register_multi_bernoulli_predictive_distribution', 'register_normal_predictive_distribution', 'register_separable_conv2d', 'set_default_conv2d_approximation', 'set_default_embedding_approximation', 'set_default_fully_connected_approximation', 'set_default_fully_connected_multi_approximation', 'set_default_generic_approximation', 'total_loss', 'total_sampled_loss']
jlee4176901 commented 5 years ago

I ran into this error when I installed kfac using pip ; if you check the installed folders and files, you miss auto_register_layer and apparently it is existing in this github repos.

So what I did is I downloaded the source instead of pip install and it worked.

voaneves commented 5 years ago

@jlee4176901 Thanks a lot for your answer!

Changed my import in Jupyter Notebook from:

!pip install kfac
import kfac

to:

!pip install git+https://github.com/tensorflow/kfac.git
import kfac

And the 'auto_register_layers' worked as expected. I'm closing the issue.

Regards, Victor.