ニューラルネットワークを用いて画像認識のアルコリズムを実装しています。
「達人データサイエンティストによる理論と実践/python機械学習プログラミング」の第12章のコードをそのまま写生しています。
MNISTデータをnumpyの配列に読み込むところでエラーが発生しました。
エラーメッセージ
ValueError Traceback (most recent call last) <ipython-input-7-93436c289cee> in <module>() 23 return images, labels 24 ---> 25 X_train, y_train = load_mnist('mnist', kind='train') 26 print('Rows: %d, columns: %d' % (X_train.shape[0], X_train.shape[1])) 27 <ipython-input-7-93436c289cee> in load_mnist(path, kind) 20 #画像ピクセル情報の配列のサイズを変更 21 #(行数:ラベルのサイズ、列数:特徴量の個数) ---> 22 images = np.fromfile(imgpath, dtype=np.uint8).reshape(len(labels), 784) 23 return images, labels 24 ValueError: cannot reshape array of size 47040000 into shape (60008,784)
ソースコード
python
1import os 2import struct 3import numpy as np 4 5def load_mnist(path, kind='train'): 6 """MNISTデータをpathからロード""" 7 #引数から指定したパスを結合(ラベルや画像のパスを作成) 8 labels_path = os.path.join(path, '%s-labels-idx1-ubyte' % kind) 9 images_path = os.path.join(path, '%s-images-idx3-ubyte' % kind) 10 11 #ファイルを読み込む 12 #引数にファイル、モードを指定(rbは読み込みのバイナリモード) 13 with open(labels_path, 'rb') as lbpath: 14 # バイナリを文字列に変換:unpack関数の引数にフォーマット、8バイト分の 15 #バイナリデータを措定して、マジックナンバー、アイテムの個数を読み込む 16 labels = np.fromfile(lbpath,dtype=np.uint8) 17 18 with open(images_path, 'rb') as imgpath: 19 magic, num, rows, cols = struct.unpack(">IIII", imgpath.read(16)) 20 #画像ピクセル情報の配列のサイズを変更 21 #(行数:ラベルのサイズ、列数:特徴量の個数) 22 images = np.fromfile(imgpath, dtype=np.uint8).reshape(len(labels), 784) 23 return images, labels 24 25X_train, y_train = load_mnist('mnist', kind='train') 26print('Rows: %d, columns: %d' % (X_train.shape[0], X_train.shape[1])) 27 28X_test, y_test = load_mnist('mnist', kind='t10k') 29print('Rows: %d, columns: %d' % (X_test.shape[0], X_test.shape[1])) 30
numpyのreshape関数の第2引数はintまたはintのタプルorリストを拾うことは知っています。
784は、MNISTデータセットの画像(28*28ピクセル)を1次元の行ベクトルで表している際の行数を示すらしいです。
解決法をご教授いただけるとうれしいです。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/19 22:37