LoginSignup
6
10

More than 5 years have passed since last update.

Google Colaboratoryを使ってみる(1日目)

Last updated at Posted at 2018-04-05

最初に

流行りの機械学習に手を出してみたかったので、ブラウザからPythonが利用可能かつGPUも使えるGoogle Colaboratoryが気になった。

以前、ローカルにJupyter Note環境を作ったこともあるけど、ブラウザでアクセス可能 & Googleドライブにノートや結果を保存出来るのでこっちの方が便利そう。

試したこと

初期設定

  • https://colab.research.google.com にアクセスして適当なノートブックを新規作成
  • デフォルトだとGPUがOFFなので下記で変更
    • 「編集」 -> 「ノートブックの設定」  -> 「ハードウェア アクセラレータ」で"GPU"を選択

kerasのインストール

  • TensorFlowはデフォルトで使えるようなので、kerasをインストールする。
  • 下記を"コード"ブロックとして追加して実行する。
!pip install -q keras
import keras

MNISTのロード

  • 機械学習界隈での"Hello, World!"であろう手書き文字画像データセットのMNISTを使えるようにする。
from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

データの内容を画像として表示

  • なんとなく気になるので画像として表示してみる。
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(8, 8))
fig.subplots_adjust(left=0, right=1, bottom=0, top=0.5, hspace=0.05, wspace=0.05)
for i in range(100):
    image = fig.add_subplot(10, 10, i + 1, xticks=[], yticks=[])
    image.imshow(x_train[i].reshape((28, 28)), cmap='gray')

学習させてみる

変更前 : model.fit(x_train, y_train,
変更後 : history = model.fit(x_train, y_train,

結果をプロット

  • 学習結果をグラフ表示してみる。
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
6
10
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
6
10