TensorFlow MNIST For ML Beginners Tutorial

お久しぶりの更新。大学始まっちゃって触れる時間減っちゃったけど続けるように頑張るよ

 

文字認識のチュートリアルと言えばMNISTだよね。機械学習界のHellow Worldみたいな。

TensorFlow MNIST For ML Beginners チュートリアルの実施

mac推奨なんだろうけど私の環境はwin7です。

 

.pyファイル作ってAnaconda Promptで実行。

C:\Users\Work\Program>python mnist_for_ml_beginners.py
File "mnist_for_ml_beginners.py", line 14
print "開始時刻: " + str(start_time)
                    ^
SyntaxError: invalid syntax

 

最初文字コード関連で詰まってるんだと思い、utf-8になってるし一行目のおまじない

# coding:utf-8

も記述してるしで悩んでたんですけどどうやらprint文の記述方法がver3.x以降で変わってたらしい。従来は

print "開始時刻: " + str(start_time)

でよかったらしいんだけど今は

print ("開始時刻: " + str(start_time))

じゃないといかんのね。

Python始めたばかりで色々初歩的なとこで躓くけど躓いて覚えよう

 

で次のエラー

C:\Users\Work\Program>python mnist_for_ml_beginners.py
Traceback (most recent call last):
File "mnist_for_ml_beginners.py", line 4, in <module>
import tensorflow as tf
ModuleNotFoundError: No module named 'tensorflow'

あれーインストールしてなかったっけ。2機のPC使って色々試してたもんでごっちゃになってた。という訳でtensorflowをインストールします。以下をAnaconda Promptで実行。

pip install tensorflow

黄文字でpipのバージョンが古いとか言われたら言われたとおりupgradeしよう。(その場合は完了後上記を再実行)

 

改めてpython mnist_for_ml_beginners.pyを実行

C:\Users\Work\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from

`float` to `np.floating` is deprecated. In future, it will be treated as`np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters

Traceback (most recent call last):
File "mnist_for_ml_beginners.py", line 8, in <module>
import input_data
ModuleNotFoundError: No module named 'input_data'

んん…?input_dataが無い…??勝手にインポートしてくれるはずでは…???

f:id:otakkun:20180928003334p:plain

なかった。

tensorflow/input_data.py at r0.8 · tensorflow/tensorflow · GitHub

こちらにinput_data.pyがあったのでとりあえずもらって来て同じ階層に保存。

C:\Users\Work\Program>python mnist_for_ml_beginners.py
C:\Users\Work\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 ==np.dtype(float).type`.
from ._conv import register_converters as _register_converters
開始時刻: 1538062702.4394832
--- MNISTデータの読み込み開始 ---
WARNING:tensorflow:From mnist_for_ml_beginners.py:23: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From C:\Users\Work\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
WARNING:tensorflow:From C:\Users\Work\Anaconda3\lib\site-packages\tensorflow\con
trib\learn\python\learn\datasets\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting MNIST_data/train-images-idx3-ubyte.gz
WARNING:tensorflow:From C:\Users\Work\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting MNIST_data/train-labels-idx1-ubyte.gz
WARNING:tensorflow:From C:\Users\Work\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:110: dense_to_one_hot(from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.one_hot on tensors.
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
WARNING:tensorflow:From C:\Users\Work\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models
.
--- MNISTデータの読み込み完了 ---
WARNING:tensorflow:From C:\Users\Work\Anaconda3\lib\site-packages\tensorflow\pyt
hon\util\tf_should_use.py:118: initialize_all_variables (from tensorflow.python.
ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
--- 訓練開始 ---
--- 訓練終了 ---
精度
0.9174
終了時刻: 1538062705.0886347
かかった時間: 2.649151563644409

すげぇ警告出てる気がするけどやっと動いた。

精度は92%前後で実行するたびに微妙に変わります。仕組みとかは下記参照のこと。

超入門訳TensorFlow MNIST For ML Beginners

日本語訳が何でもかんでもあるのは便利な時代に生まれたなぁと実感するね

 

今日はこの辺で。