「ゼロから作るDeepLearning」のサンプルコードをGoogle Colaboratoryで動かしてみている。
3章でうまく動かなかったところをメモしておく。
【問題点】
MNISTデータのダウンロードが出来ない
【原因】
データのあるサーバからエラーが返されているらしい 1
【対策】
データをダウンロードする代わりに、keras等からMNISTデータを取得する 2
from keras.datasets import mnist
(x_train, t_train), (x_test, t_test) = mnist.load_data()
# それぞれのデータの形状を出力
print(x_train.shape)
print(t_train.shape)
print(x_test.shape)
print(t_test.shape)
【備考】
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
上記コードを実行後、ブラウザ上で認証手続きを行うとマウントできる
インポートは以下の要領で行う
import sys
sys.path.append('/content/drive/My Drive/ColabNotebooks/path/to/dataset/')
from mnist import load_mnist
x_train
やx_test
のサイズに注意
【問題点】
MNIST画像の表示ができない
【原因】
Google Colaboratory上だとPIL
のImage
のshow()
がうまく動かない
【対策】
代わりにIPython.display()
を使う 3
from PIL import Image
from IPython.display import display
def img_show(img):
pil_img = Image.fromarray(np.uint8(img))
display(pil_img)
【問題点】
MNISTのデータをkerasから取得したが、データのshapeがサンプルコードと違う
【対策】
以下の要領で変換したデータを使う
reshaped_data = x_test.reshape(10000, 784)
【備考】
サンプルコードでは画像データを正規化しているので、同様の処理をすると本に書かれている通りのAccuracyが出る
normalized_data = x_test.reshape(10000, 784) / 255.0
Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。
また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!
こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?
コメント