keras.sequentiol.LSTMの入力データが負値でエラー

前提

kerasのsequentialモデルでLSTMを使おうとして、
参考サイト https://keras.io/ja/getting-started/sequential-model-guide/
を参考に実行しましたが、
Embedding層の入力でマイナスがあったらしいエラーが出ます。
Embedding層はよくわかりませんが、何かデータの形式整理とか正規化らしき処理のようで、並行処理もやるようで、走行のたびにエラーの位置、値が変化します。
試しにEnbedding層をスキップすると、LSTMは事前にその種らしきの処理が必要となります。
と言うことから、LSTMは負値データはできないのでしょうか。
SimpleRNNはそのようなことはありません。どんな理由があるでしょうか。

発生している問題・エラーメッセージ

その1 InvalidArgumentError: Graph execution error: その2 Node: 'sequential_29/embedding_25/embedding_lookup' indices[0,5] = -1 is not in [0, 50)

該当のソースコード

python

import numpy as np import tensorflow as tf from tensorflow import keras import random from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras.layers import Flatten from tensorflow.keras.layers import Activation from tensorflow.keras.layers import SimpleRNN from tensorflow.keras.layers import Embedding from tensorflow.keras.layers import LSTM from tensorflow.keras.layers import Dropout from IPython.display import SVG from keras.utils import plot_model offsct=10tstep = 10 #ベクトル長feature = 1ncat=15 # 主力カテゴリー数bat = 50 #ベクトル個数unit = 100 #第1層のunit数dense_unit = ncat #出力クラスが3個 categorical__crossentropymax_features = bat model = Sequential()model.add(Embedding(max_features,output_dim=20))model.add(LSTM(64))#model.add(Flatten())#model.add(Dropout(0.5))model.add(Dense(1, activation='sigmoid'))model.add(Dense(dense_unit, activation="softmax"))model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) #plot_model(model) model.summary() y = np.zeros(6*bat)y[0] = 0y[1] = 1y[2] = 1y[3] = 0y[4] = 0y[5] = -1 for n in range(1,bat): for i in range(0,6): y[6*n+i] = y[i]print(y) #y = l_x.astype("float32")print("y.size",y.size) l_x = np.zeros((bat,tstep))l_y = np.zeros((bat, 1)) cl_y = np.zeros((bat,ncat))#print(cl_y) for offs in range(1): # offs=0 の場合のみ for i in range(0, bat): for j in range(0, tstep): l_x[i,j] = y[i+offs +j] l_y[i] = y[i+offs+1 ] cl_y[i] = tf.keras.utils.to_categorical(l_y[i], ncat) #print(i,"cl_y",cl_y[i]) #tr_x = l_x.reshape(bat, ts, 1) #tr_y = l_y.reshape(bat, 1) print("tr_x.size",l_x.size) print("tr_y.size",l_y.size) #print("shape",l_x.shape,l_y.shape,cl_y.shape) history = model.fit(l_x, cl_y, epochs=1 ,batch_size=bat, verbose=1)

補足情報 コンソール出力

Model: "sequential_29"


embedding_25 (Embedding) (None, None, 20) 1000

lstm_27 (LSTM) (None, 64) 21760

dense_56 (Dense) (None, 1) 65

dense_57 (Dense) (None, 15) 30

=================================================================
Total params: 22,855
Trainable params: 22,855
Non-trainable params: 0


[ 0. 1. 1. 0. 0. -1. 0. 1. 1. 0. 0. -1. 0. 1. 1. 0. 0. -1.
0. 1. 1. 0. 0. -1. 0. 1. 1. 0. 0. -1. 0. 1. 1. 0. 0. -1.
0. 1. 1. 0. 0. -1. 0. 1. 1. 0. 0. -1. 0. 1. 1. 0. 0. -1.

          1. -1. 0. 1. 1. 0. 0. -1.]

y.size 300
tr_x.size 500
tr_y.size 50
Traceback (most recent call last):

File "C:\Users\.conda\envs\py38tfkr\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
exec(code, globals, locals)

File "c:\book\rnn1\lstmtest.py", line 78, in <module>
history = model.fit(l_x, cl_y, epochs=1 ,batch_size=bat, verbose=1)

File "C:\Users\.conda\envs\py38tfkr\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None

File "C:\Users\.conda\envs\py38tfkr\lib\site-packages\tensorflow\python\eager\execute.py", line 54, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,

InvalidArgumentError: Graph execution error:

Detected at node 'sequential_29/embedding_25/embedding_lookup' defined at (most recent call last):
File "C:\Users\.conda\envs\py38tfkr\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\.conda\envs\py38tfkr\lib\site-packages\keras\layers\core\embedding.py", line 199, in call
out = tf.nn.embedding_lookup(self.embeddings, inputs)
Node: 'sequential_29/embedding_25/embedding_lookup'
indices[0,5] = -1 is not in [0, 50)
[[{{node sequential_29/embedding_25/embedding_lookup}}]] [Op:__inference_train_function_100589]

コメントを投稿

0 コメント