Postingan lainnya
error memanggil kembali data histori pada data training
# The LSTM model is being constructed and compiled for the training process to take place
# Since the model forecasts into the future, in this case there are no validation data and no use of
# keras callbacks ('EarlyStopping')
ini koding pemanggil data histori
initializer = tf.keras.initializers.he_uniform(seed=0)
model=Sequential()
model.add(LSTM(12,activation='relu',input_shape=(length,n_features),kernel_initializer=initializer,
bias_initializer=initializers.Constant(0.01)))
model.add(Dense(1,activation='linear',kernel_initializer=initializer,
bias_initializer=initializers.Constant(0.01)))
model.compile(optimizer=opt,loss='mse')
data_generator=TimeseriesGenerator(scaled_set,scaled_set,length=length,batch_size=1)
model.fit_generator(data_generator,epochs=100)
ini error nya
Epoch 1/100
<ipython-input-441-67e4ec443553>:15: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
model.fit_generator(data_generator,epochs=100)
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-441-67e4ec443553> in <cell line: 15>()
13 data_generator=TimeseriesGenerator(scaled_set,scaled_set,length=length,batch_size=1)
14
---> 15 model.fit_generator(data_generator,epochs=100)
2 frames
/usr/local/lib/python3.10/dist-packages/keras/engine/training.py in tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
KeyError: in user code:
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1284, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1268, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1249, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1054, in train_step
self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
File "/usr/local/lib/python3.10/dist-packages/keras/optimizers/optimizer.py", line 543, in minimize
self.apply_gradients(grads_and_vars)
File "/usr/local/lib/python3.10/dist-packages/keras/optimizers/optimizer.py", line 1174, in apply_gradients
return super().apply_gradients(grads_and_vars, name=name)
File "/usr/local/lib/python3.10/dist-packages/keras/optimizers/optimizer.py", line 650, in apply_gradients
iteration = self._internal_apply_gradients(grads_and_vars)
File "/usr/local/lib/python3.10/dist-packages/keras/optimizers/optimizer.py", line 1200, in _internal_apply_gradients
return tf.__internal__.distribute.interim.maybe_merge_call(
File "/usr/local/lib/python3.10/dist-packages/keras/optimizers/optimizer.py", line 1250, in _distributed_apply_gradients_fn
distribution.extended.update(
File "/usr/local/lib/python3.10/dist-packages/keras/optimizers/optimizer.py", line 1247, in apply_grad_to_update_var **
return self._update_step(grad, var)
File "/usr/local/lib/python3.10/dist-packages/keras/optimizers/optimizer.py", line 232, in _update_step
raise KeyError(
KeyError: 'The optimizer cannot recognize variable lstm_24/lstm_cell_24/kernel:0. This usually means you are trying to call the optimizer to update different parts of the model separately. Please call `optimizer.build(variables)` with the full list of trainable variables before the training loop or use legacy optimizer `tf.keras.optimizers.legacy.Adam.'
1 Jawaban:
<div>Kesalahan yang Anda dapatkan adalah KeyError, yang berarti optimizer tidak dapat menemukan variabel yang ingin diperbarui. Hal ini kemungkinan besar terjadi karena Anda mencoba memanggil optimizer untuk memperbarui bagian model secara terpisah. Untuk memperbaikinya, Anda perlu memanggil optimizer.build(variables)
dengan daftar lengkap variabel yang dapat dilatih sebelum perulangan pelatihan.<br><br>Berikut ini adalah kode yang diperbarui:<br><br></div><pre>initializer = tf.keras.initializers.he_uniform(seed=0)
model=Sequential()
model.add(LSTM(12,activation='relu',input_shape=(length,n_features),kernel_initializer=initializer,
bias_initializer=initializers.Constant(0.01)))
model.add(Dense(1,activation='linear',kernel_initializer=initializer,
bias_initializer=initializers.Constant(0.01)))
model.compile(optimizer=opt,loss='mse')
data_generator=TimeseriesGenerator(scaled_set,scaled_set,length=length,batch_size=1)
Membangun optimizer dengan daftar lengkap variabel yang dapat dilatih
optimizer.build(model.trainable_variables)
Melatih model
model.fit_generator(data_generator,epochs=100)</pre><div><br>Hal ini seharusnya memperbaiki kesalahan dan memungkinkan Anda melatih model dengan sukses.</div>