4

我正在使用 tensorflow 构建模型。我训练了我的模型,它工作正常。然后,我修改了我的代码,当我尝试训练我的模型时,我得到了一个 AlreadyExistError。我重新启动了 Jupyter Notebook,但仍然遇到同样的错误。我需要一些帮助。这是我构建网络并训练它的一段代码。问题出现在最后一行。

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv1D, Dropout, Dense, Flatten, LSTM, MaxPooling1D, Bidirectional
from tensorflow.keras.optimizers import Adam
from keras.callbacks import EarlyStopping, TensorBoard

model = Sequential()

model.add(Conv1D(32, kernel_size=3, activation='elu', padding='same',
                 input_shape=(vector_size, 1)))
model.add(Conv1D(32, kernel_size=3, activation='elu', padding='same'))
model.add(Conv1D(32, kernel_size=3, activation='relu', padding='same'))
model.add(MaxPooling1D(pool_size=3))

model.add(Bidirectional(LSTM(512, dropout=0.2, recurrent_dropout=0.3)))

model.add(Dense(512, activation='sigmoid'))
model.add(Dropout(0.2))
model.add(Dense(512, activation='sigmoid'))
model.add(Dropout(0.25))
model.add(Dense(512, activation='sigmoid'))
model.add(Dropout(0.25))

model.add(Dense(2, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.0001, decay=1e-6), metrics=['accuracy'])

tensorboard = TensorBoard(log_dir='logs/', histogram_freq=0, write_graph=True, write_images=True)

model.summary()
model.fit(np.array(x_train), np.array(y_train), batch_size=batch_size, epochs=no_epochs,
         validation_data=(np.array(x_test), np.array(y_test)),  callbacks=[tensorboard, EarlyStopping(min_delta=0.0001, patience=3)])

训练 90000 个样本,验证 10000 个样本 Epoch 1/10
500/90000 [........] - ETA :2:00:49 /anaconda3/lib/python3.7/site-packages/keras/callbacks/callbacks.py:846:RuntimeWarning:提前停止取决于val_loss不可用的指标。可用的指标是:(self.monitor, ','.join(list(logs.keys()))), RuntimeWarning ------------------------ -------------------------------------------------- -- 1 model.fit(np.array(x_train), np.array(y_train), batch_size=batch_size, epochs=no_epochs, ----> 2 validation_data=(np.array (x_test), np.array(y_test)), callbacks=[tensorboard, EarlyStopping(min_delta=0.0001, Patient=3)]) 3 print('你可以继续')

/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight , initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs) 817 max_queue_size=max_queue_size, 818 workers=workers, --> 819 use_multiprocessing=use_multiprocessing) 820 821 def evaluate(self,

/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight , sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs) 340 mode=ModeKeys.TRAIN, 341 training_context=training_context, --> 342 total_epochs=epochs) 343 cbks.make_logs(model, epoch_logs,训练结果,模式键。训练)344

/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py in run_one_epoch(模型,迭代器,execution_function,dataset_size,batch_size,strategy,steps_per_epoch,num_samples,mode,training_context,total_epochs)126 step=step, mode=mode, size=current_batch_size) as batch_logs: 127 try: --> 128 batch_outs = execution_function(iterator) 129 except (StopIteration, errors.OutOfRangeError): 130 # TODO(kaftan): File bug about tf function和errors.OutOfRangeError?

/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py in execution_function(input_fn) 96 #numpy在 Eager 模式下将张量转换为值。97 返回 nest.map_structure(_non_none_constant_value, ---> 98 分布式函数(input_fn)) 99 100 返回执行函数

/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in call (self, *args, **kwds) 566 xla_context.Exit() 567 else: --> 568 result = self ._call(*args, **kwds) 569 570 如果tracing_count == self._get_tracing_count():

/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in _call(self, *args, **kwds) 630 # 提升成功,所以变量被初始化,我们可以运行 631 #无状态函数。--> 632 return self._stateless_fn(*args, **kwds) 633 else: 634 canon_args, canon_kwds = \

/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in call (self, *args, **kwargs) 2361 with self._lock:
2362 graph_function, args, kwargs = self._maybe_define_function (args, kwargs) -> 2363 return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access 2364 2365 @property

/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _filtered_call(self, args, kwargs) 1609 if isinstance(t, (ops.Tensor, 1610
resource_variable_ops.BaseResourceVariable))), -> 1611 self.captured_inputs)1612 1613 def _call_flat(self,args,captured_inputs,cancellation_manager=None):

/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _call_flat(self, args, capture_inputs, canceling_manager)
1690 # 没有磁带在看;跳到运行函数。
第1691章返回self._build_call_outputs(self._inference_function.call(->1692 ctx,args,cancellation_manager=cancellation_manager))1693
forward_backward = self._select_forward_and_backward_functions(
1694 args,

/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in call(self, ctx, args, cancel_manager) 543 个输入=args, 544 attrs=("executor_type", executor_type, "config_proto ", 配置), --> 545 ctx=ctx) 546 else: 547 输出 = execute.execute_with_cancellation(

/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name) 65 else: 66 message = e.message ---> 67 Six.raise_from(core._status_to_exception(e.code, message), None) 68 除了 TypeError as e: 69 keras_symbolic_tensors = [

/anaconda3/lib/python3.7/site-packages/six.py in raise_from(value, from_value)

AlreadyExistsError:资源__per_step_0/sequential/bidirectional/forward_lstm/while_grad/body/_429/gradients/AddN_13/tmp_var/N10tensorflow19TemporaryVariableOp6TmpVarE [[{{节点顺序/双向/forward_lstm/while_grad/body/_429/gradients/AddN_13/tmp_var}}]] [操作:__inference_distributed_function_12060]

函数调用栈:distributed_function

4

2 回答 2

1

您必须在训练时看到与之前运行的相同架构的问题。

这应该重置 keras 会话:

from tensorflow.keras import backend
backend.clear_session()
于 2020-02-19T08:29:30.057 回答
0

就我而言,我在 AWS 上使用 EMR 时遇到了问题,我解决了只是卸载 Keras,如果你有 tensorflow,你已经有 Keras,但是还有其他包依赖于旧的 Keras 库。所以你需要卸载旧的 Keras 来避免这个问题。

!pip 卸载 keras

于 2021-11-04T06:20:00.187 回答