Blockost
Blockost

Reputation: 583

Cannot run Tensorflow code multiple times in Jupyter Notebook

I'm struggling running Tensorflow (v1.1) code multiple times in Jupyter Notebook.

For example, I execute this simple code snippet that creates an encoding layer for a seq2seq model:

# Construct encoder layer (LSTM)
encoder_cell = tf.contrib.rnn.LSTMCell(encoder_hidden_units)
encoder_outputs, encoder_final_state = tf.nn.dynamic_rnn(
    encoder_cell, encoder_inputs_embedded, 
    dtype=tf.float32, time_major=False
)

First time is totally fine, my encoder is created.

However, if I rerun it (no matter the changes I've applied), I get this error: Attempt to have a second RNNCell use the weights of a variable scope that already has weights

It's very annoying as it forces me to restart the kernel every time I want to change a layer.

Can someone explain me why this happens and how I can fix this ?

Thanks!

Upvotes: 2

Views: 1683

Answers (1)

pfm
pfm

Reputation: 6328

You are trying to build the exact same graph twice and therefore TensorFlow complains because the variables already exist in the default graph.

What you could do is to call tf.reset_default_graph() before trying to call the method a second time to ensure you create a new graph when required.

Just in case, I would also suggest using an interactive session as described here in the Start TensorFlow InteractiveSession section:

import tensorflow as tf
sess = tf.InteractiveSession()

Upvotes: 6

Related Questions