Tensorflow Keras〜初めのトレーニング_1〜

イントロダクション

前回は、TensorflowのKerasを使用して大まかな機械学習の「流れ」を追いかけてみました。

Tensorflow Keras 〜初めてのKeras〜
今回は、処理の内容について学んでいきます。

[補足]
1.Fashion MNISTを使用する
2.tensorflowでMNISTをロードできる
fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

ここで処理を一度実行してみます。自分の環境ではエラーが出ました。「pyplot」のインポートができませんでした。解決策はこちら

初めのニューラルネットワーク

基本的な分類処理を行うようです。「Basic classification」

上に記載したように、MNISTからトレーニングデータを取得(ロード)して機械学習を行います。そして参考サイトには以下のように記載されています。

データセットをロードすると、4つのNumPy配列が返されます。

  • train_imagesそしてtrain_labels配列は、トレーニングセットモデルを学ぶために使用する-theデータを。
  • モデルはテストセット、the test_images、およびtest_labels配列に対してテストされます。

この点に注意して学習を進めます。

分類(Classification)について

MNISTの画像データ(fashion_mnist)は以下のように分類されています。

ラベル クラス
0 Tシャツ/トップ
1 ズボン
2 路肩に寄せて下さい
3 ドレス
4 コート
5 サンダル
6 シャツ
7 スニーカー
8 バッグ
9 アンクルブーツ

そして下のような注意点があるので指示の通りにします。

各画像は単一のラベルにマッピングされます。
クラス名はデータセットに含まれていないので、
後で画像をプロットするときに使用するために、それらをここに保存します。

<コード>

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']


データを調べる(コード実行)

下のようなコードになりました。

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)

# Get MINST data
fashion_minst = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_minst.load_data()

# Chcke MINST data
print("トレーニングイメージ数(train_images.shape):%s " % (train_images.shape,))
print("トレーニングラベル数(len(train_labels)): %d" % len(train_labels))
print("トレーニングラベルの中身: %d" % train_labels[0])

今回はここまでにしておきます。

関連ページ一覧

  1. Tensorflow Keras 〜初めてのKeras〜
  2. Tensorflow Keras Errors〜"python is not installed as a framework."〜
  3. Python Tensorflow 〜初めての人工知能(TensorFlowインストール)〜

 

[rakuten ids="bagray:10006253"]

Tensorflow Keras Errors〜python is not installed as a framework.〜

TensorflowのKerasの学習を進める途中でつまづいた部分です。

<エラーメッセージ>

ImportError: Python is not installed as a framework. 
The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. 
See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.

<対処法>

参考にしたサイトはこちら

以下のコマンドで修正対象ファイルを調べる

$python
>>> import matplotlib
>>> matplotlib.matplotlib_fname()

出力されたファイル(matplotlibrc)を修正する

修正_前:backend : macosx 
修正_後:backend : Tkagg

確認する

pythonのコンソールを開いて、matplotlib.pyplotをインポートする

>>> import matplotlib.pyplot

これでエラーが出なければOK!