RuntimeError Traceback (most recent call last) Cell In[1], line 71 68 labels = np.concatenate([np.ones((half_batch, 1)), np.zeros((half_batch, 1))]) 70 # 識別器をトレーニング —> 71 d_loss = discriminator.train_on_batch(imgs, labels) 73 # 生成器をトレーニング 74 noise = np.random.normal(0, 1, (batch_size, latent_dim)) File ~anaconda3libsite-packageskerasenginetraining.py:2498, in Model.train_on_batch(self, x, y, sample_weight, class_weight, reset_metrics, return_dict) 2450 def train_on_batch( 2451 self, 2452 x, (…) 2457 return_dict=False, 2458 ): 2459 “””Runs a single gradient update on a single batch of data. 2460 2461 Args: (…) 2496 RuntimeError: If `model.train_on_batch` is wrapped in a `tf.function`. 2497 “”” -> 2498 self._assert_compile_was_called() 2499 self._check_call_args(“train_on_batch”) 2500 _disallow_inside_tf_function(“train_on_batch”) File ~anaconda3libsite-packageskerasenginetraining.py:3685, in Model._assert_compile_was_called(self) 3679 def _assert_compile_was_called(self): 3680 # Checks whether `compile` has been called. If it has been called, 3681 # then the optimizer is set. This is different from whether the 3682 # model is compiled 3683 # (i.e. whether the model is built and its inputs/outputs are set). 3684 if not self._is_compiled: -> 3685 raise RuntimeError( 3686 “You must compile your model before ” 3687 “training/testing. ” 3688 “Use `model.compile(optimizer, loss)`.” 3689 ) RuntimeError: You must compile your model before training/testing. Use `model.compile(optimizer, loss)`.

このエラーは、識別器(discriminator)モデルをトレーニングしようとした際に、モデルがコンパイルされていないために発生しています。Kerasのモデルをトレーニングする前には、compileメソッドを使用してモデルをコンパイルする必要があります。

以下のように、識別器のモデルをコンパイルしてからトレーニングを行うように修正してみてください。

python
discriminator.compile(loss='binary_crossentropy', optimizer=Adam())

これにより、モデルのコンパイルが行われ、トレーニングが正常に進むはずです。

未分類

Posted by ぼっち