在Colab上训练网络时AlreadyExistsError

时间:2019-04-09 18:32:07

标签: tensorflow keras jupyter google-colaboratory

我正在尝试在Google Colab上训练LSTM网络。但是,会发生此错误:

    AlreadyExistsError: Resource __per_step_116/training_4/Adam/gradients/bidirectional_4/while/ReadVariableOp/Enter_grad/ArithmeticOptimizer/AddOpsRewrite_Add/tmp_var/N10tensorflow19TemporaryVariableOp6TmpVarE
     [[{{node training_4/Adam/gradients/bidirectional_4/while/ReadVariableOp/Enter_grad/ArithmeticOptimizer/AddOpsRewrite_Add/tmp_var}}]]

我不知道问题出在哪里。这是网络的模型:

sl_model = keras.models.Sequential()
sl_model.add(keras.layers.Embedding(max_index+1, hidden_size, mask_zero=True))

sl_model.add(keras.layers.Bidirectional(keras.layers.LSTM(hidden_size, 
     activation='tanh', dropout=0.2, recurrent_dropout = 0.2, return_sequences=True)))

sl_model.add(keras.layers.Bidirectional(keras.layers.LSTM(hidden_size, activation='tanh', dropout=0.2, recurrent_dropout = 0.2, return_sequences=False))
            )

sl_model.add(keras.layers.Dense(max_length, activation='softsign')) 

optimizer = keras.optimizers.Adam()
sl_model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['acc'])

batch_size = 128
epochs = 3
cbk = keras.callbacks.TensorBoard("logging/keras_model")

print("\nStarting training...")

sl_model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size,
          shuffle=True, validation_data=(x_dev, y_dev), callbacks=[cbk])

非常感谢您!

1 个答案:

答案 0 :(得分:0)

您需要重新启动运行时-当您在单个jupyter(Colaboratory)运行时中定义了多个图形时,就会发生这种情况。

调用tf.reset_default_graph()可能也有帮助,但是取决于您是否正在使用急切执行以及如何定义会话,这可能行不通。