前提
同じ長さのnumpy配列で2種類の値の設定方法(case1、case2)で実行すると
case2の場合は正常だが、case1の場合はエラーとなります。
case1はファイルからnp.frombufferで読みます
case2は同じサイズだけnp.zerosで確保したものです。
print(y.shape)で比べても同じです。yの値の内容によって違う処理はありません。ファイル読み込みの全体の長さに対してメモリは十分です。
ファイル読み込みなので簡単に他の環境でテストできないことが残念です。
発生している問題・エラーメッセージ
File "C:\Users\\.conda\envs\py38tfkr\lib\site-packages\keras\utils\np_utils.py", line 71, in to_categorical categorical[np.arange(n), y] = 1 IndexError: index 4377 is out of bounds for axis 1 with size 15
該当のソースコード
python
1import sys 2import numpy as np 3import tensorflow as tf 4from tensorflow import keras 5ncat=15 # カテゴリー数 6bat = 1000 #ベクトル個数 7 8#case1 9 10""" 11root = './data' 12file_path = "data/val.byte" 13with open("data/val.byte", 'rb') as f: 14 y = np.frombuffer(f.read(), np.int16, offset=0) 15""" 16 17#case2 18y=np.zeros(144000).astype('int16') 19 20#y設定後 21 22print("y.shape",y.shape) 23 24l_y = np.zeros((bat)) 25cl_y = np.zeros((bat,ncat)) 26print("shape l_y",l_y.shape,"cl_y",cl_y.shape,"l_y.szie",l_y.size,"cl_y.size",cl_y.size) 27 28 29for i in range(0, bat): 30 l_y[i] = y[i+1] 31 32cl_y = tf.keras.utils.to_categorical(l_y, ncat)
試したこと
コンソール出力
case1実行時
y.shape (144000,) dtype int16 shape l_y (1000,) cl_y (1000, 15) l_y.szie 1000 cl_y.size 15000 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\rnn4test.py", line 27, in <module> cl_y = tf.keras.utils.to_categorical(l_y, ncat) File "C:\Users\\.conda\envs\py38tfkr\lib\site-packages\keras\utils\np_utils.py", line 71, in to_categorical categorical[np.arange(n), y] = 1 IndexError: index 4377 is out of bounds for axis 1 with size 15
case2実行時
runfile('C:/book/rnn1/RNN4test.py', wdir='C:/book/rnn1') y.shape (144000,) dtype int16 shape l_y (1000,) cl_y (1000, 15) l_y.szie 1000 cl_y.size 15000
回答1件
あなたの回答
tips
プレビュー