実現したいこと
kerasでIndexError: tuple index out of rangeが出てしまったので解決したい。
前提
pythonのkerasで画像認識を学習中に前述のエラーが出てしまいました。
kerasを始めたばかりで、調べてみても具体的な原因や解決方法がわかりませんでした。
発生している問題・エラーメッセージ
python
12023-11-22 09:12:44.959479: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. 2To enable the following instructions: SSE SSE2 SSE3 SSE4.1 SSE4.2 AVX AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. 3Traceback (most recent call last): 4 File "c:\Users\user\Documents\AI\ai_test2.py", line 54, in <module> 5 model.fit(x, y, epochs=1) 6 File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\utils\traceback_utils.py", line 70, in error_handler 7 raise e.with_traceback(filtered_tb) from None 8 File "C:\Users\user~1\AppData\Local\Temp\__autograph_generated_file5q6yansm.py", line 15, in tf__train_function 9 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope) 10 ^^^^^ 11TypeError: in user code: 12 13 File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\training.py", line 1377, in train_function * 14 return step_function(self, iterator) 15 File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\training.py", line 1360, in step_function ** 16 outputs = model.distribute_strategy.run(run_step, args=(data,)) 17 File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\training.py", line 1349, in run_step ** 18 outputs = model.train_step(data) 19 File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\training.py", line 1126, in train_step 20 y_pred = self(x, training=True) 21 File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\utils\traceback_utils.py", line 70, in error_handler 22 raise e.with_traceback(filtered_tb) from None 23 File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\base_layer.py", line 2800, in _name_scope 24 name_scope += "/" 25 26 TypeError: unsupported operand type(s) for +=: 'Dense' and 'str' 27 28PS C:\Users\user\Documents\AI> & C:/Users/user/AppData/Local/Programs/Python/Python311/python.exe c:/Users/user/Documents/AI/ai_test2.py 292023-11-22 09:13:06.603065: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. 30To enable the following instructions: SSE SSE2 SSE3 SSE4.1 SSE4.2 AVX AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. 31Traceback (most recent call last): 32 File "c:\Users\user\Documents\AI\ai_test2.py", line 54, in <module> 33 model.fit(x, y, epochs=1) 34 File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\utils\traceback_utils.py", line 70, in error_handler 35 raise e.with_traceback(filtered_tb) from None 36 File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 959, in __getitem__ 37 return self._dims[key] 38 ~~~~~~~~~~^^^^^ 39IndexError: tuple index out of range
該当のソースコード
python
1import keras 2from keras.models import Sequential 3from keras.layers import Dense 4from keras.preprocessing.image import load_img 5import numpy as np 6import glob 7 8# 'test_data/train/'の中のrock, paper, scissorsディレクトリの中に 9# 各5枚ずつ画像が入っている 10 11rps = ['rock/', 'paper/', 'scissors/'] 12 13BASE_DIR = 'test_data/train/' 14 15# 学習用データを格納 16x = [] 17# 学習用データに対応するラベルを格納 18y = [] 19 20# rock, paper, scissorsのラベルはそれぞれ0, 1, 2 21for idx, name in enumerate(rps): 22 path = BASE_DIR + name + '*.jpg' 23 files = glob.glob(path) 24 for file in files: 25 image = load_img(file) 26 image = np.asarray(image)/255.0 27 x.append(image) 28 y.append(np.asarray(idx)) 29 30# モデルを作成 31model = Sequential( 32 Dense(784, input_shape=(300, 300), activation='relu'), 33 Dense(3, activation='softmax') 34) 35 36model.compile( 37 optimizer='adam', 38 loss='categorical_crossentropy', 39 metrics=['accuracy'] 40) 41 42# 学習中なのでepochsは1 43model.fit(x, y, epochs=1) 44 45model.summary() 46
試したこと
範囲外を指定している可能を考慮し、
model.fit(x[0:15], y[0:15], epochs=1)
としてみたが以前同じ結果だった。
補足情報(FW/ツールのバージョンなど)
python
1>>> tensorflow.__version__ 2'2.14.0' 3>>> keras.__version__ 4'2.14.0'

あなたの回答
tips
プレビュー