使用 keras Tuner 构建 LSTM 模型时如何解决此错误?

问题描述 投票:0回答:1

我在 LSTM 的嵌入层中使用手套嵌入。我编写了一个构建模型的函数,如下所示:

def build_model(hp):
    model = keras.Sequential()
    model.add(Embedding(input_dim=vocab_size, # Size of the vocabulary
                            output_dim=EMBEDDING_DIM, # Length of the vector for each word
                            weights=[embedding_matrix],
                            input_length=MAX_SEQUENCE_LENGTH, trainable=False))
#   model.add(Embedding(input_dim=vocab_size, # Size of the vocabulary
#                         output_dim=50, # Length of the vector for each word
#                         input_length = MAX_SEQUENCE_LENGTH)) # Maximum length of a sequence
    model.add(SpatialDropout1D(hp.Choice('sdropout_', values=[0.2, 0.3, 0.4, 0.5, 0.6])
                              )
             )
    model.add(LSTM(hp.Int('units_', min_value=30, max_value=70, step=10), kernel_initializer='random_normal', dropout=0.5, recurrent_dropout=0.5))
    counter = 0
   
    for i in range(hp.Int('num_layers', 1, 5)):
        if counter == 0:
            model.add(layers.Dense(units=hp.Int('units_', min_value=16, max_value=512, step=32),
                                kernel_initializer='random_normal',
                                #kernel_initializer=hp.Choice('kernel_initializer_', ['random_normal', 'random_uniform', 'zeros']),
                                #bias_initializer=hp.Choice('bias_initializer_', ['random_normal', 'random_uniform', 'zeros']),
                                input_shape=y_train.shape,
                                #kernel_regularizer=regularizers.l2(0.03),
                                kernel_regularizer=keras.regularizers.l2(hp.Choice('l2_value', values = [1e-2, 3e-3, 2e-3, 1e-3, 1e-4])),
                                #kernel_regularizer=regularizers.L1L2(l1=1e-5, l2=1e-4),
                                #bias_regularizer=regularizers.L2(1e-4),
                                #activity_regularizer=regularizers.L2(1e-5),
                                activation=hp.Choice('dense_activation_' + str(i), values=['relu', 'tanh', 'sigmoid'], default='relu')
                                  )
                     )
            model.add(layers.Dropout(rate=hp.Float('dropout_' + str(i), min_value=0.0, max_value=0.6, default=0.25, step=0.05)
                                    )
                     )
        else:
            model.add(layers.Dense(units=hp.Int('units_' + str(i), min_value=12, max_value=512, step=32),
                                    kernel_initializer='random_normal',
                                    #kernel_initializer=hp.Choice('kernel_initializer_', ['random_normal', 'random_uniform', 'zeros']),
                                    #bias_initializer=hp.Choice('bias_initializer_', ['random_normal', 'random_uniform', 'zeros']),
                                    #kernel_regularizer=regularizers.l2(0.03),
                                    kernel_regularizer=keras.regularizers.l2(hp.Choice('l2_value', values = [1e-2, 3e-3, 2e-3, 1e-3, 1e-4])),
                                    #kernel_regularizer=regularizers.L1L2(l1=1e-5, l2=1e-4),
                                    #bias_regularizer=regularizers.L2(1e-4),
                                    #activity_regularizer=regularizers.L2(1e-5),
                                    activation=hp.Choice('dense_activation_' + str(i), values=['relu', 'tanh', 'sigmoid'], default='relu')
                                  )
                     )
            model.add(layers.Dropout(rate=hp.Float('dropout_' + str(i), min_value=0.0, max_value=0.5, default=0.25, step=0.05)
                                    )
                     )
                      
            counter+=1
            
            model.add(layers.Dense(classes, activation='softmax'))
            
    optimizer = hp.Choice('optimizer', values = ['adam' 
                                                # ,'sgd', 'rmsprop', 'adadelta'
                                                    ])
            
        
    lr = hp.Choice('learning_rate', values=[1e-2, 1e-3])
      
    if optimizer == 'adam':
        optimizer = keras.optimizers.Adam(learning_rate=lr)
    #elif optimizer == 'sgd':
    #    optimizer = keras.optimizers.SGD(learning_rate=lr)
    #elif optimizer == 'rmsprop':
    #    optimizer = keras.optimizers.RMSprop(learning_rate=lr)
       # else:
        #    optimizer = keras.optimizers.Adadelta(learning_rate=lr)

    model.compile(
        optimizer=optimizer,
        loss='sparse_categorical_crossentropy',
        #steps_per_execution=32,
        metrics=['accuracy']
    )
    
    return model

执行上述函数后,我构建了一个调谐器对象。

tuner = RandomSearch(
    build_model,
    objective='val_accuracy',  # Set the objective to 'accuracy'
    #objective='val_loss',  # Set the objective to 'val_loss'
    max_trials=3,  # Set the maximum number of trials
    executions_per_trial=3,  # Set the number of executions per trial
    overwrite=True,
    directory='my_dir',  # Set the directory where the results are stored
    project_name='consumer_complaints'  # Set the project name
)

# Display the search space summary
tuner.search_space_summary()

但是在这一步之后,当我运行下面的代码来搜索超参数空间时。

# Assume pad_data_train, pad_data_testare your data
tuner.search(pad_data_train, y_train,
             epochs=5, 
             validation_data=(pad_data_test, y_test)
             , 
             batch_size = 64
            )

我收到如下所示的错误。我似乎无法弄清楚我做错了什么。

Traceback (most recent call last):   File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/base_tuner.py", line 270, in _try_run_and_update_trial
    self._run_and_update_trial(trial, *fit_args, **fit_kwargs)   File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/base_tuner.py", line 235, in _run_and_update_trial
    results = self.run_trial(trial, *fit_args, **fit_kwargs)   File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/tuner.py", line 287, in run_trial
    obj_value = self._build_and_fit_model(trial, *args, **copied_kwargs)   File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/tuner.py", line 214, in _build_and_fit_model
    results = self.hypermodel.fit(hp, model, *args, **kwargs)   File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/hypermodel.py", line 144, in fit
    return model.fit(*args, **kwargs)   File "/opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None   File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/execute.py", line 53, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:

Detected at node 'sequential/embedding/embedding_lookup' defined at (most recent call last):
    File "/opt/conda/lib/python3.10/runpy.py", line 196, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "/opt/conda/lib/python3.10/runpy.py", line 86, in _run_code
      exec(code, run_globals)
    File "/opt/conda/lib/python3.10/site-packages/ipykernel_launcher.py", line 17, in <module>
      app.launch_new_instance()
    File "/opt/conda/lib/python3.10/site-packages/traitlets/config/application.py", line 1043, in launch_instance
      app.start()
    File "/opt/conda/lib/python3.10/site-packages/ipykernel/kernelapp.py", line 736, in start
      self.io_loop.start()
    File "/opt/conda/lib/python3.10/site-packages/tornado/platform/asyncio.py", line 195, in start
      self.asyncio_loop.run_forever()
    File "/opt/conda/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
      self._run_once()
    File "/opt/conda/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
      handle._run()
    File "/opt/conda/lib/python3.10/asyncio/events.py", line 80, in _run
      self._context.run(self._callback, *self._args)
    File "/opt/conda/lib/python3.10/site-packages/ipykernel/kernelbase.py", line 516, in dispatch_queue
      await self.process_one()
    File "/opt/conda/lib/python3.10/site-packages/ipykernel/kernelbase.py", line 505, in process_one
      await dispatch(*args)
    File "/opt/conda/lib/python3.10/site-packages/ipykernel/kernelbase.py", line 412, in dispatch_shell
      await result
    File "/opt/conda/lib/python3.10/site-packages/ipykernel/kernelbase.py", line 740, in execute_request
      reply_content = await reply_content
    File "/opt/conda/lib/python3.10/site-packages/ipykernel/ipkernel.py", line 422, in do_execute
      res = shell.run_cell(
    File "/opt/conda/lib/python3.10/site-packages/ipykernel/zmqshell.py", line 546, in run_cell
      return super().run_cell(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3009, in run_cell
      result = self._run_cell(
    File "/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3064, in _run_cell
      result = runner(coro)
    File "/opt/conda/lib/python3.10/site-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
      coro.send(None)
    File "/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3269, in run_cell_async
      has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
    File "/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3448, in run_ast_nodes
      if await self.run_code(code, result, async_=asy):
    File "/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3508, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "/tmp/ipykernel_42/3795112731.py", line 2, in <module>
      tuner.search(pad_data_train, y_train,
    File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/base_tuner.py", line 230, in search
      self._try_run_and_update_trial(trial, *fit_args, **fit_kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/base_tuner.py", line 270, in _try_run_and_update_trial
      self._run_and_update_trial(trial, *fit_args, **fit_kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/base_tuner.py", line 235, in _run_and_update_trial
      results = self.run_trial(trial, *fit_args, **fit_kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/tuner.py", line 287, in run_trial
      obj_value = self._build_and_fit_model(trial, *args, **copied_kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/tuner.py", line 214, in _build_and_fit_model
      results = self.hypermodel.fit(hp, model, *args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/hypermodel.py", line 144, in fit
      return model.fit(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/training.py", line 1742, in fit
      tmp_logs = self.train_function(iterator)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/training.py", line 1338, in train_function
      return step_function(self, iterator)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/training.py", line 1322, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/training.py", line 1303, in run_step
      outputs = model.train_step(data)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/training.py", line 1080, in train_step
      y_pred = self(x, training=True)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/training.py", line 569, in __call__
      return super().__call__(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/base_layer.py", line 1150, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 96, in error_handler
      return fn(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/sequential.py", line 405, in call
      return super().call(inputs, training=training, mask=mask)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/functional.py", line 512, in call
      return self._run_internal_graph(inputs, training=training, mask=mask)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/functional.py", line 669, in _run_internal_graph
      outputs = node.layer(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/base_layer.py", line 1150, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 96, in error_handler
      return fn(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/layers/core/embedding.py", line 272, in call
      out = tf.nn.embedding_lookup(self.embeddings, inputs) Node: 'sequential/embedding/embedding_lookup' indices[41,8] = 6402 is not in [0, 5201)   [[{{node sequential/embedding/embedding_lookup}}]] [Op:__inference_train_function_14440]
--------------------------------------------------------------------------- RuntimeError                              Traceback (most recent call last) Cell In[43], line 2
      1 # Assume x_train, y_train are your data
----> 2 tuner.search(pad_data_train, y_train,
      3              epochs=5, 
      4              validation_data=(pad_data_test, y_test)
      5              , 
      6              batch_size = 64
      7             )

File /opt/conda/lib/python3.10/site-packages/keras_tuner/engine/base_tuner.py:231, in BaseTuner.search(self, *fit_args, **fit_kwargs)
    229     self.on_trial_begin(trial)
    230     self._try_run_and_update_trial(trial, *fit_args, **fit_kwargs)
--> 231     self.on_trial_end(trial)
    232 self.on_search_end()

File /opt/conda/lib/python3.10/site-packages/keras_tuner/engine/base_tuner.py:335, in BaseTuner.on_trial_end(self, trial)
    329 def on_trial_end(self, trial):
    330     """Called at the end of a trial.
    331 
    332     Args:
    333         trial: A `Trial` instance.
    334     """
--> 335     self.oracle.end_trial(trial)
    336     # Display needs the updated trial scored by the Oracle.
    337     self._display.on_trial_end(self.oracle.get_trial(trial.trial_id))

File /opt/conda/lib/python3.10/site-packages/keras_tuner/engine/oracle.py:107, in synchronized.<locals>.wrapped_func(*args, **kwargs)
    105     LOCKS[oracle].acquire()
    106     THREADS[oracle] = thread_name
--> 107 ret_val = func(*args, **kwargs)
    108 if need_acquire:
    109     THREADS[oracle] = None

File /opt/conda/lib/python3.10/site-packages/keras_tuner/engine/oracle.py:434, in Oracle.end_trial(self, trial)
    432 if not self._retry(trial):
    433     self.end_order.append(trial.trial_id)
--> 434     self._check_consecutive_failures()
    436 self._save_trial(trial)
    437 self.save()

File /opt/conda/lib/python3.10/site-packages/keras_tuner/engine/oracle.py:386, in Oracle._check_consecutive_failures(self)
    384     consecutive_failures = 0
    385 if consecutive_failures == self.max_consecutive_failed_trials:
--> 386     raise RuntimeError(
    387         "Number of consecutive failures excceeded the limit "
    388         f"of {self.max_consecutive_failed_trials}.\n"
    389         + trial.message
    390     )

RuntimeError: Number of consecutive failures excceeded the limit of 3. Traceback (most recent call last):   File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/base_tuner.py", line 270, in _try_run_and_update_trial
    self._run_and_update_trial(trial, *fit_args, **fit_kwargs)   File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/base_tuner.py", line 235, in _run_and_update_trial
    results = self.run_trial(trial, *fit_args, **fit_kwargs)   File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/tuner.py", line 287, in run_trial
    obj_value = self._build_and_fit_model(trial, *args, **copied_kwargs)   File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/tuner.py", line 214, in _build_and_fit_model
    results = self.hypermodel.fit(hp, model, *args, **kwargs)   File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/hypermodel.py", line 144, in fit
    return model.fit(*args, **kwargs)   File "/opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None   File "/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/execute.py", line 53, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:

Detected at node 'sequential/embedding/embedding_lookup' defined at (most recent call last):
    File "/opt/conda/lib/python3.10/runpy.py", line 196, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "/opt/conda/lib/python3.10/runpy.py", line 86, in _run_code
      exec(code, run_globals)
    File "/opt/conda/lib/python3.10/site-packages/ipykernel_launcher.py", line 17, in <module>
      app.launch_new_instance()
    File "/opt/conda/lib/python3.10/site-packages/traitlets/config/application.py", line 1043, in launch_instance
      app.start()
    File "/opt/conda/lib/python3.10/site-packages/ipykernel/kernelapp.py", line 736, in start
      self.io_loop.start()
    File "/opt/conda/lib/python3.10/site-packages/tornado/platform/asyncio.py", line 195, in start
      self.asyncio_loop.run_forever()
    File "/opt/conda/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
      self._run_once()
    File "/opt/conda/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
      handle._run()
    File "/opt/conda/lib/python3.10/asyncio/events.py", line 80, in _run
      self._context.run(self._callback, *self._args)
    File "/opt/conda/lib/python3.10/site-packages/ipykernel/kernelbase.py", line 516, in dispatch_queue
      await self.process_one()
    File "/opt/conda/lib/python3.10/site-packages/ipykernel/kernelbase.py", line 505, in process_one
      await dispatch(*args)
    File "/opt/conda/lib/python3.10/site-packages/ipykernel/kernelbase.py", line 412, in dispatch_shell
      await result
    File "/opt/conda/lib/python3.10/site-packages/ipykernel/kernelbase.py", line 740, in execute_request
      reply_content = await reply_content
    File "/opt/conda/lib/python3.10/site-packages/ipykernel/ipkernel.py", line 422, in do_execute
      res = shell.run_cell(
    File "/opt/conda/lib/python3.10/site-packages/ipykernel/zmqshell.py", line 546, in run_cell
      return super().run_cell(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3009, in run_cell
      result = self._run_cell(
    File "/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3064, in _run_cell
      result = runner(coro)
    File "/opt/conda/lib/python3.10/site-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
      coro.send(None)
    File "/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3269, in run_cell_async
      has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
    File "/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3448, in run_ast_nodes
      if await self.run_code(code, result, async_=asy):
    File "/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3508, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "/tmp/ipykernel_42/3795112731.py", line 2, in <module>
      tuner.search(pad_data_train, y_train,
    File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/base_tuner.py", line 230, in search
      self._try_run_and_update_trial(trial, *fit_args, **fit_kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/base_tuner.py", line 270, in _try_run_and_update_trial
      self._run_and_update_trial(trial, *fit_args, **fit_kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/base_tuner.py", line 235, in _run_and_update_trial
      results = self.run_trial(trial, *fit_args, **fit_kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/tuner.py", line 287, in run_trial
      obj_value = self._build_and_fit_model(trial, *args, **copied_kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/tuner.py", line 214, in _build_and_fit_model
      results = self.hypermodel.fit(hp, model, *args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras_tuner/engine/hypermodel.py", line 144, in fit
      return model.fit(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/training.py", line 1742, in fit
      tmp_logs = self.train_function(iterator)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/training.py", line 1338, in train_function
      return step_function(self, iterator)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/training.py", line 1322, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/training.py", line 1303, in run_step
      outputs = model.train_step(data)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/training.py", line 1080, in train_step
      y_pred = self(x, training=True)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/training.py", line 569, in __call__
      return super().__call__(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/base_layer.py", line 1150, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 96, in error_handler
      return fn(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/sequential.py", line 405, in call
      return super().call(inputs, training=training, mask=mask)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/functional.py", line 512, in call
      return self._run_internal_graph(inputs, training=training, mask=mask)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/functional.py", line 669, in _run_internal_graph
      outputs = node.layer(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/engine/base_layer.py", line 1150, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 96, in error_handler
      return fn(*args, **kwargs)
    File "/opt/conda/lib/python3.10/site-packages/keras/src/layers/core/embedding.py", line 272, in call
      out = tf.nn.embedding_lookup(self.embeddings, inputs) Node: 'sequential/embedding/embedding_lookup' indices[41,8] = 6402 is not in [0, 5201)   [[{{node sequential/embedding/embedding_lookup}}]] [Op:__inference_train_function_14440]
python runtime-error hyperparameters keras-tuner
1个回答
0
投票

嵌入层是用

input_dim=vocab_size
定义的,但我在您提供的代码片段中没有看到
vocab_size
的定义。

试试这个:

def build_model(hp):
    model = keras.Sequential()
    model.add(Embedding(
        input_dim=vocab_size,  # Make sure vocab_size is defined
        output_dim=EMBEDDING_DIM,
        weights=[embedding_matrix],
        input_length=MAX_SEQUENCE_LENGTH, 
        trainable=False,
        input_shape=(MAX_SEQUENCE_LENGTH,)  # Add input_shape parameter
    ))
    # Rest of your code remains unchanged...

另请确保替换

MAX_SEQUENCE_LENGTH
并为
vocab_size
提供有效值。进行这些更改后,尝试再次运行超参数调整过程。祝你好运。

© www.soinside.com 2019 - 2024. All rights reserved.