#python #tensorflow #keras #lstm
#python #tensorflow #keras #lstm
Вопрос:
Я пытаюсь создать модель машинного обучения в keras, которая угадывает следующее слово, учитывая серию слов, использующих LSTM. Это код для моей модели:
gen=Sequential()
gen.add(Embedding(vocab_size,embed_dim))
gen.add(LSTM(vocab_size,activation='softmax'))
gen.compile(loss='categorical_crossentropy',optimizer='adam')
gen.summary()
(X_train, X_test, Y_train, Y_test)=train_test_split(X,Y,test_size=.2)
gen.fit(X_train,Y_train,validation_data=(X_test,Y_test),batch_size=batch_size,epochs=epochs)
Мои данные X представляют собой массив дополненных массивов целых чисел, вот так: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59, 73, 4]
а мой Y — это один вектор с длиной, равной моему размеру vocab (который в данном случае равен 1994). Выполнение приведенного выше кода создает этот результат:
2020-08-23 11:19:34.390012: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not
load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2020-08-23 11:19:34.391222: E tensorflow/stream_executor/cuda/cuda_driver.cc:313] failed call to cuInit: UNKNOWN ERROR (303)
2020-08-23 11:19:34.405184: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: LAPTOP-J9AD5M4U
2020-08-23 11:19:34.406474: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: LAPTOP-J9AD5M4U
2020-08-23 11:19:34.407916: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-08-23 11:19:34.695283: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x20c4fc4a980 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-08-23 11:19:34.696499: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding (Embedding) (None, None, 32) 75040
_________________________________________________________________
lstm (LSTM) (None, 2345) 22305640
=================================================================
Total params: 22,380,680
Trainable params: 22,380,680
Non-trainable params: 0
_________________________________________________________________
2020-08-23 11:20:30.730043: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at variable_ops.cc:104 : Already exists: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
2020-08-23 11:20:30.943137: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at variable_ops.cc:104 : Already exists: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
2020-08-23 11:20:31.469228: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at variable_ops.cc:104 : Already exists: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
2020-08-23 11:20:32.787365: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at variable_ops.cc:104 : Already exists: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
2020-08-23 11:20:34.014073: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at variable_ops.cc:104 : Already exists: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
2020-08-23 11:20:34.752033: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at variable_ops.cc:104 : Already exists: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
2020-08-23 11:20:35.980107: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at variable_ops.cc:104 : Already exists: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
2020-08-23 11:20:36.790928: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at variable_ops.cc:104 : Already exists: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
2020-08-23 11:20:37.396431: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at variable_ops.cc:104 : Already exists: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
2020-08-23 11:20:38.314946: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at variable_ops.cc:104 : Already exists: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
2020-08-23 11:20:39.111772: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at variable_ops.cc:104 : Already exists: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
2020-08-23 11:20:39.859069: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at variable_ops.cc:104 : Already exists: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
2020-08-23 11:20:40.938812: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at variable_ops.cc:104 : Already exists: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
2020-08-23 11:20:42.485426: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at variable_ops.cc:104 : Already exists: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
2020-08-23 11:20:43.183461: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at variable_ops.cc:104 : Already exists: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
Traceback (most recent call last):
File "C:UsersjsmithDesktopCodeJohnWorkspacesMachine_LearningNext Wordtrain.py", line 45, in <module>
gen.fit(X_train,Y_train,validation_data=(X_test,Y_test),batch_size=batch_size,epochs=epochs)
File "C:Usersjsmithanaconda3libsite-packagestensorflowpythonkerasenginetraining.py", line 66, in _method_wrapper
return method(self, *args, **kwargs)
File "C:Usersjsmithanaconda3libsite-packagestensorflowpythonkerasenginetraining.py", line 848, in fit
tmp_logs = train_function(iterator)
File "C:Usersjsmithanaconda3libsite-packagestensorflowpythoneagerdef_function.py", line 580, in __call__
result = self._call(*args, **kwds)
File "C:Usersjsmithanaconda3libsite-packagestensorflowpythoneagerdef_function.py", line 644, in _call
return self._stateless_fn(*args, **kwds)
File "C:Usersjsmithanaconda3libsite-packagestensorflowpythoneagerfunction.py", line 2420, in __call__
return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access
File "C:Usersjsmithanaconda3libsite-packagestensorflowpythoneagerfunction.py", line 1665, in _filtered_call
self.captured_inputs)
File "C:Usersjsmithanaconda3libsite-packagestensorflowpythoneagerfunction.py", line 1746, in _call_flat
ctx, args, cancellation_manager=cancellation_manager))
File "C:Usersjsmithanaconda3libsite-packagestensorflowpythoneagerfunction.py", line 598, in call
ctx=ctx)
File "C:Usersjsmithanaconda3libsite-packagestensorflowpythoneagerexecute.py", line 60, in quick_execute
inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.AlreadyExistsError: Resource __per_step_0/gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var/struct tensorflow::TemporaryVariableOp::TmpVar
[[{{node gradient_tape/sequential/lstm/while/sequential/lstm/while_grad/body/_354/gradients/AddN_6/tmp_var}}]] [Op:__inference_train_function_3777]
Function call stack:
train_function
Иногда он будет обучаться на одном пакете, затем получит ошибку, а в других случаях он генерирует ошибку до того, как вообще произойдет какое-либо обучение. Я не знаю, имеет ли это какое-либо отношение к формам ввода и вывода LSTM, однако я бы ожидал, что если бы это была проблема, то она возникла бы gen.compile
, а не gen.fit
.
Ответ №1:
Итак, я только что провел эксперимент и обнаружил, что это была форма вывода LSTM, и меньшая длина вывода, а затем расширение ее плотным слоем устраняет ошибку.