keras: predict()が実行できない
解決したいこと
LSTMを使うGANの実装をしています。
その際に使うpredict()が実行できずに困っております。
発生している問題・エラー
InvalidArgumentError Traceback (most recent call last)
<ipython-input-169-f04cfd1d3585> in <module>
6 print(len(x_train))
7 z = np.random.normal(0, 1, (batch_size, 100))
----> 8 gen_text = generator.predict(z)
9
10 print(z)
~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, *args, **kwargs)
128 raise ValueError('{} is not supported in multi-worker mode.'.format(
129 method.__name__))
--> 130 return method(self, *args, **kwargs)
131
132 return tf_decorator.make_decorator(
~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
1597 for step in data_handler.steps():
1598 callbacks.on_predict_batch_begin(step)
-> 1599 tmp_batch_outputs = predict_function(iterator)
1600 if data_handler.should_sync:
1601 context.async_wait()
~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
778 else:
779 compiler = "nonXla"
--> 780 result = self._call(*args, **kwds)
781
782 new_tracing_count = self._get_tracing_count()
~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
812 # In this case we have not created variables on the first call. So we can
813 # run the first trace but we should fail if variables are created.
--> 814 results = self._stateful_fn(*args, **kwds)
815 if self._created_variables:
816 raise ValueError("Creating variables on a non-first call to a function"
~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in __call__(self, *args, **kwargs)
2827 with self._lock:
2828 graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
-> 2829 return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access
2830
2831 @property
~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _filtered_call(self, args, kwargs, cancellation_manager)
1841 `args` and `kwargs`.
1842 """
-> 1843 return self._call_flat(
1844 [t for t in nest.flatten((args, kwargs), expand_composites=True)
1845 if isinstance(t, (ops.Tensor,
~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
1921 and executing_eagerly):
1922 # No tape is watching; skip to running the function.
-> 1923 return self._build_call_outputs(self._inference_function.call(
1924 ctx, args, cancellation_manager=cancellation_manager))
1925 forward_backward = self._select_forward_and_backward_functions(
~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in call(self, ctx, args, cancellation_manager)
543 with _InterpolateFunctionError(self):
544 if cancellation_manager is None:
--> 545 outputs = execute.execute(
546 str(self.signature.name),
547 num_outputs=self._num_outputs,
~\anaconda3\lib\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
57 try:
58 ctx.ensure_initialized()
---> 59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: indices[0,3] = -1 is not in [0, 50)
[[node sequential_117/embedding_73/embedding_lookup (defined at <ipython-input-168-f04cfd1d3585>:8) ]]
(1) Invalid argument: indices[0,3] = -1 is not in [0, 50)
[[node sequential_117/embedding_73/embedding_lookup (defined at <ipython-input-168-f04cfd1d3585>:8) ]]
[[sequential_117/embedding_73/embedding_lookup/_6]]
0 successful operations.
0 derived errors ignored. [Op:__inference_predict_function_68877]
Errors may have originated from an input operation.
Input Source operations connected to node sequential_117/embedding_73/embedding_lookup:
sequential_117/embedding_73/embedding_lookup/68418 (defined at C:\Users\gest\anaconda3\lib\contextlib.py:113)
Input Source operations connected to node sequential_117/embedding_73/embedding_lookup:
sequential_117/embedding_73/embedding_lookup/68418 (defined at C:\Users\gest\anaconda3\lib\contextlib.py:113)
Function call stack:
predict_function -> predict_function
該当するソースコード
def build_generator(seq_length,z_dim):
model = Sequential()
model.add(Dense(32, input_dim=z_dim))
model.add(Embedding(50,32,input_length=z_dim))
model.add(LSTM(32))
model.add(Dense(seq_length*jigen_length, activation='softmax'))
model.add(Reshape((5, 1)))
model.summary()
return model
z = np.random.normal(0, 1, (batch_size, 100))
gen_text = generator.predict(z) #実行できない
また、EmbeddingレイヤーとLSTMレイヤーを削除したらpredict()が実行出来ました。
Embeddingレイヤーのみ削除、LSTMレイヤーのみ削除の場合は実行出来ませんでした。
両コードともコンパイルは通ります。
どうか宜しくお願いします。
#EmbeddingレイヤーとLSTMレイヤーを削除
def build_generator(seq_length,z_dim):
model = Sequential()
model.add(Dense(32, input_dim=z_dim))
model.add(Dense(seq_length*jigen_length, activation='softmax'))
model.summary()
return model
z = np.random.normal(0, 1, (batch_size, 100))
gen_text = generator.predict(z) #実行できる
#実際関数を呼び出してGANのモデルをコンパイルしてあげる
discriminator = build_discriminatior()
discriminator.compile(loss='binary_crossentropy', optimizer=Adam(), metrics=['accuracy'])
generator = build_generator(seq_length,z_dim)
#識別器の学習機能をオフにしてあげる。識別器と生成器を別々に学習させてあげられる
discriminator.trainable = False
gan = build_gan(generator, discriminator)
gan.compile(loss='binary_crossentropy', optimizer=Adam())
自分で試したこと
どこに問題があるのか色々試してみたり調べたところ、おそらくEmbeddingレイヤーとLSTMレイヤーに問題がありそうです。
しかし、なぜ出来ないかがわかりません。
GPUは、GeForce GTX1060 6GBを使っています。
エラーコードをGoogleで調べた際に、メモリが足りないかもという記事が出てきたので一応書いておきました。
0