Saurabh Kumar
Saurabh Kumar

Reputation: 2763

Number of layers in the NN model keeps increasing every time I call a new instance of the model

I am working for a while now with tensorflow and jupyter but this is the first time I have encountered this problem. I have a NN model which has 6 layers and I get an instance of this model by calling the function "classifier"

def classifier(input_repr,prob,reuse=None):
    e_l1=tf.layers.dense(inputs=input_repr,units=512,activation=tf.nn.leaky_relu)
    e_l1=tf.nn.dropout(e_l1,prob)
    e_l2=tf.layers.dense(inputs=e_l1,units=256,activation=tf.nn.leaky_relu)
    e_l2=tf.nn.dropout(e_l2,prob)
    e_l3=tf.layers.dense(inputs=e_l2,units=128,activation=tf.nn.leaky_relu)  
    e_l3=tf.nn.dropout(e_l3,prob)
    e_l4=tf.layers.dense(inputs=e_l3,units=64,activation=tf.nn.leaky_relu)  
    e_l4=tf.nn.dropout(e_l4,prob)
    e_l5=tf.layers.dense(inputs=e_l4,units=32,activation=tf.nn.leaky_relu)  
    e_l5=tf.nn.dropout(e_l5,prob)
    d_l3=tf.layers.dense(inputs=e_l5,units=1,activation=tf.nn.leaky_relu)
    return d_l3

I also have a function to visualize the model summary as

def model_summary():
    model_vars = tf.trainable_variables()
    slim.model_analyzer.analyze_vars(model_vars, print_info=True)

print(model_summary())

And i get the model instance as,

model_output=classifier(input_repr,prob)

The problem is whenever I call this, and then i call model_summary() the layers gets stacked up to the previous model. For eg, if when i call the "classifier()" for the first time, model_Summary() shows 5 layers, but when i call it again it shows 10 layers and so on. I always initialize again before calling the classifier() method but it just happens again and again. I don't know if that is some issue with jupyter. The only way I know to solve this problem is to completely restart the kernel, which leads to loss of variables.

Upvotes: 0

Views: 430

Answers (2)

quester
quester

Reputation: 564

every time you call function classifier you create additional layers, when you will create your model and compile it only use model object to model.fit and model.predict

Upvotes: 0

gorjan
gorjan

Reputation: 5555

Don't forget to reset the default graph tf.reset_default_graph() before creating your model. The issue is that the notebook is running in a single thread, and Tensorflow stacks new nodes on the graph whenever you build the graph over and over again. That's why when prototyping in Jupyter notebook, always reset the default graph when you start build a new graph.

Upvotes: 2

Related Questions