2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Digit Recognizer Sample (Deep Neural Network Keras way) を読んで勉強する

Posted at

Digit Recognizer の Tutorial で紹介されている Notebook その 2 (Deep Neural Network Keras way) を読んで見て機械学習を勉強する。

Import all required libraries

import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
%matplotlib inline

from keras.models import Sequential
from keras.layers import Dense, Dropout, Lambda, Flatten
from keras.optimizers import Adam, RMSprop
from sklearn.model_selection import train_test_split

from subprocess import check_output
  • Sequential
    • 層を重ねたニューラルネットワークのモデルを表す
  • Dense
    • 通常の全結合ニューラルネットワークレイヤー
  • Dropout
  • Lambda
    • 任意の式をラップする
    • レイヤー内で特殊な?複雑な?計算をしたいときに使用する
  • Flatten
  • Adam
  • RMSprop

Load Train and Test data

train = pd.read_csv("../input/train.csv")
print(train.shape)
train.head()
test= pd.read_csv("../input/test.csv")
print(test.shape)
test.head()
  • shape: 行数, 列数を返す
  • head: 最初の x 行のデータを返す。デフォルトは 5
X_train = (train.ix[:,1:].values).astype('float32')
y_train = train.ix[:,0].values.astype('int32')
X_test = test.values.astype('float32')
  • ix: 行・列を指定してデータを取り出す
    • loc, iloc を使えと warning が出た
  • astype: 複数データを型変換できる
X_train = X_train.reshape(X_train.shape[0], 28, 28)

for i in range(6, 9):
    plt.subplot(330 + (i+1))
    plt.imshow(X_train[i], cmap=plt.get_cmap('gray'))
    plt.title(y_train[i]);
  • (num_images, img_rows, img_cols) の形式にデータを変換
  • ラベル付きでデータを表示
X_train = X_train.reshape(X_train.shape[0], 28, 28,1)
X_train.shape

X_test = X_test.reshape(X_test.shape[0], 28, 28,1)
X_test.shape
  • グレースケールのために一次元追加

Preprocessing the digit images

  • 標準化
  • 最も大切な前処理
mean_px = X_train.mean().astype(np.float32)
std_px = X_train.std().astype(np.float32)

def standardize(x): 
    return (x-mean_px)/std_px

One Hot encoding of labels

  • One Hot ベクトルは多くの次元が 0 、一つの次元が 1 となる
  • 整数 n は、n 次元目が 1 となるベクトルで表現される
  • 例 : 3 would be [0,0,0,1,0,0,0,0,0,0]
  • to_categorical で One HOt ベクトルに変換している
from keras.utils.np_utils import to_categorical
y_train= to_categorical(y_train)
num_classes = y_train.shape[1]
num_classes

10 番目のラベルを plot すると

plt.title(y_train[9])
plt.plot(y_train[9])
plt.xticks(range(10));

3 となった

Designing Neural Network Architecture

  • 再現性のために random seed を指定
seed = 43
np.random.seed(seed)

Linear Model

  • EarlyStopping
    • コールバックの関数
    • 監視する値の変化が停止した時に訓練を終了する
  • BatchNormalization
  • Convolution2D
  • MaxPooling2D
    • 空間データのマックスプーリング演算
    • [Deep learning を説明している記事]
      • プーリングとは画像で言う低画像化のことで、畳み込み演算における計算の削減を可能にするとのこと
from keras.models import Sequential
from keras.layers.core import  Lambda, Dense, Flatten, Dropout
from keras.callbacks import EarlyStopping
from keras.layers import BatchNormalization, Convolution2D, MaxPooling2D
  • Keras Sequential 層からシンプルなモデルを作成する
    • 合計、平均、指数のようなシンプルな演算を Lambda 層で行う
      • 1st 層で (rows, columns, colour channel) 形式でデータの入力次元を定義する
    • 入力を一次配列に平滑化する
    • Dense は fully connected layer(全てのニューロンと接続する層)
model= Sequential()
model.add(Lambda(standardize,input_shape=(28,28,1)))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
print("input shape ",model.input_shape)
print("output shape ",model.output_shape)

Compile network

  • トレーニングのためのネットワークを作成する前に、以下のことを追加する必要がある

  • compile で optimizer, loss, metrics を設定

    • RMSprop
      • lr: 学習率
      • 学習率とは、学習していく中でどれくらい重みを変えていくかを決めるものらしい
    • categorical_crossentropy
    • metrics(評価関数)
      • accuracy(精度)を metrics として使う
      • 内容はわからなかったが Keras で用意している metrics
from keras.optimizers import RMSprop
model.compile(optimizer=RMSprop(lr=0.001),
 loss='categorical_crossentropy',
 metrics=['accuracy'])
from keras.preprocessing import image
gen = image.ImageDataGenerator()
  • flow
    • (データ, ラベル) を引数にバッチを生成する
from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.10, random_state=42)
batches = gen.flow(X_train, y_train, batch_size=64)
val_batches=gen.flow(X_val, y_val, batch_size=64)
history=model.fit_generator(batches, batches.n, nb_epoch=1, 
                    validation_data=val_batches, nb_val_samples=val_batches.n)
history_dict = history.history
history_dict.keys()
  • 以下でプロットしてる
    • epoch を 1 にしているで一点しかプロットされない
import matplotlib.pyplot as plt
%matplotlib inline
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
epochs = range(1, len(loss_values) + 1)

# "bo" is for "blue dot"
plt.plot(epochs, loss_values, 'bo')
# b+ is for "blue crosses"
plt.plot(epochs, val_loss_values, 'b+')
plt.xlabel('Epochs')
plt.ylabel('Loss')

plt.show()
plt.clf()   # clear figure
acc_values = history_dict['acc']
val_acc_values = history_dict['val_acc']

plt.plot(epochs, acc_values, 'bo')
plt.plot(epochs, val_acc_values, 'b+')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')

plt.show()

値を変えて学習

model.optimizer.lr=0.01
gen = image.ImageDataGenerator()
batches = gen.flow(X_train, y_train, batch_size=64)
history=model.fit_generator(batches, batches.n, nb_epoch=1)

提出用ファイル作成

predictions = model.predict_classes(X_test, verbose=0)

submissions=pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)),
                         "Label": predictions})
submissions.to_csv("DR.csv", index=False, header=True)
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?