前提
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
1import numpy as np 2import tensorflow as tf 3from tensorflow import keras 4import random 5from tensorflow.keras.models import Sequential 6from tensorflow.keras.layers import Dense 7from tensorflow.keras.layers import Flatten 8from tensorflow.keras.layers import Activation 9from tensorflow.keras.layers import SimpleRNN 10from tensorflow.keras.layers import Embedding 11from tensorflow.keras.layers import LSTM 12from tensorflow.keras.layers import Dropout 13from IPython.display import SVG 14from keras.utils import plot_model 15 16offsct=10 17tstep = 10 #ベクトル長 18feature = 1 19ncat=15 # 主力カテゴリー数 20bat = 50 #ベクトル個数 21unit = 100 #第1層のunit数 22dense_unit = ncat #出力クラスが3個 categorical__crossentropy 23max_features = bat 24 25model = Sequential() 26model.add(Embedding(max_features,output_dim=20)) 27model.add(LSTM(64)) 28#model.add(Flatten()) 29#model.add(Dropout(0.5)) 30model.add(Dense(1, activation='sigmoid')) 31model.add(Dense(dense_unit, activation="softmax")) 32model.compile(loss='categorical_crossentropy', 33 optimizer='rmsprop', 34 metrics=['accuracy']) 35 36#plot_model(model) 37 38model.summary() 39 40y = np.zeros(6*bat) 41y[0] = 0 42y[1] = 1 43y[2] = 1 44y[3] = 0 45y[4] = 0 46y[5] = -1 47 48for n in range(1,bat): 49 for i in range(0,6): 50 y[6*n+i] = y[i] 51print(y) 52 53 54#y = l_x.astype("float32") 55print("y.size",y.size) 56 57l_x = np.zeros((bat,tstep)) 58l_y = np.zeros((bat, 1)) 59cl_y = np.zeros((bat,ncat)) 60#print(cl_y) 61for offs in range(1): # offs=0 の場合のみ 62 63 for i in range(0, bat): 64 for j in range(0, tstep): 65 l_x[i,j] = y[i+offs +j] 66 67 l_y[i] = y[i+offs+1 ] 68 69 cl_y[i] = tf.keras.utils.to_categorical(l_y[i], ncat) 70 #print(i,"cl_y",cl_y[i]) 71 72 #tr_x = l_x.reshape(bat, ts, 1) 73 #tr_y = l_y.reshape(bat, 1) 74 print("tr_x.size",l_x.size) 75 print("tr_y.size",l_y.size) 76 #print("shape",l_x.shape,l_y.shape,cl_y.shape) 77 78 history = model.fit(l_x, cl_y, epochs=1 ,batch_size=bat, verbose=1) 79
補足情報 コンソール出力
Model: "sequential_29"
Layer (type) Output Shape Param #
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. 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]
回答1件
あなたの回答
tips
プレビュー