我现在使用tensorflow和jupyter工作了一段时间,但这是我第一次遇到这个问题。我有一个 6 层的 NN 模型,我通过调用函数“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
我还有一个函数可以将模型摘要可视化为
def model_summary():
model_vars = tf.trainable_variables()
slim.model_analyzer.analyze_vars(model_vars, print_info=True)
print(model_summary())
我得到的模型实例为,
model_output=classifier(input_repr,prob)
问题是每当我调用它,然后我调用 model_summary() 时,层都会堆叠到先前的模型。例如,如果当我第一次调用“classifier()”时,model_Summary() 显示 5 层,但当我再次调用它时,它显示 10 层,依此类推。我总是在调用 classifier() 方法之前再次初始化,但它只是一次又一次地发生。不知道是不是jupyter的问题。我知道解决这个问题的唯一方法是完全重新启动内核,这会导致变量丢失。
最佳答案
在创建模型之前,不要忘记重置默认图表tf.reset_default_graph()
。问题是笔记本在单个线程中运行,并且每当您一遍又一遍地构建图形时,Tensorflow 都会在图形上堆叠新节点。这就是为什么在 Jupyter Notebook 中进行原型(prototype)设计时,在开始构建新图表时始终重置默认图表。
关于tensorflow - 每次调用模型的新实例时,神经网络模型中的层数都会不断增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56522630/