TensorFlowã®ãã¥ã¼ããªã¢ã«ã§è¦æ¦ãã¦ã¿ã
ä»æ´æãããããã©ãTensorFlowã®ãã¥ã¼ããªã¢ã«ãè¡ã£ã¦ã¿ãã
ãã¡ãããã
https://www.tensorflow.org/versions/master/tutorials/mnist/beginners/index.html
ã¾ãã¯ãå¿ è¦ãªãã®ãã¤ã³ã¹ãã¼ã«
$ python -V
Python 2.7.10
Pythonã®ãã¼ã¸ã§ã³ã¯å¤§ä¸å¤«ãããªã®ã§ãå¿ è¦ãªã¢ã¸ã¥ã¼ã«ãããã¡ãã
$ sudo easy_install pip $ sudo easy_install --upgrade six # ä»®æ³ã®ç°å¢å¤æ°ã使ããç°å¢ãæ§ç¯ãã # ãããããªãã¨Macã§TensorFlowãç°¡åã«ã¤ã³ã¹ãã¼ã«ã§ããªãï¼ $ sudo pip install --upgrade virtualenv The directory '/Users/aokayama/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. The directory '/Users/aokayama/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. Collecting virtualenv Downloading virtualenv-15.0.2-py2.py3-none-any.whl (1.8MB) 100% 1.8MB 383kB/s Installing collected packages: virtualenv Successfully installed virtualenv-15.0.2
Macã§ã¯ããã©ã«ãã®è¨å®ãéªéãã¦ãã¦ãã¾ãåããªãã®ã§ã
ä»åã®TensorFlowãå®è¡ããããã®ç°å¢è¨å®ãä½æãã¦ããã§éã¶ããã«ãã
# æ°ããenvãä½æ $ virtualenv --system-site-packages ~/tensorflow New python executable in /Users/aokayama/tensorflow/bin/python Installing setuptools, pip, wheel...done. # å ¥ãè¾¼ã $ source ~/tensorflow/bin/activate (tensorflow) $ pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py2-none-any.whl Collecting tensorflow==0.8.0 from https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py2-none-any.whl Downloading https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py2-none-any.whl (19.3MB) 100% 19.3MB 51kB/s Requirement already up-to-date: six>=1.10.0 in /Library/Python/2.7/site-packages/six-1.10.0-py2.7.egg (from tensorflow==0.8.0) Collecting protobuf==3.0.0b2 (from tensorflow==0.8.0) Downloading protobuf-3.0.0b2-py2.py3-none-any.whl (326kB) 100% 327kB 2.1MB/s Requirement already up-to-date: wheel in ./tensorflow/lib/python2.7/site-packages (from tensorflow==0.8.0) Collecting numpy>=1.10.1 (from tensorflow==0.8.0) Downloading numpy-1.11.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.9MB) 100% 3.9MB 248kB/s Requirement already up-to-date: setuptools in ./tensorflow/lib/python2.7/site-packages (from protobuf==3.0.0b2->tensorflow==0.8.0) Installing collected packages: protobuf, numpy, tensorflow Found existing installation: numpy 1.8.0rc1 Not uninstalling numpy at /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python, outside environment /Users/aokayama/tensorflow Successfully installed numpy-1.11.0 protobuf-3.0.0b2 tensorflow-0.8.0 # ç°å¢ãæããã¨ã㯠(tensorflow)$ deactivate
æä¾ããã¦ããæ師ãã¼ã¿ããã¦ã³ãã¼ã
$ wget https://raw.githubusercontent.com/tensorflow/tensorflow/r0.9/tensorflow/examples/tutorials/mnist/input_data.py
å¾ã¯ã³ã¼ããæ¸ãã ãã
Hello Worldãåçµ
import tensorflow as tf hello = tf.constant('Hello, TensorFlow!') sess = tf.Session() print sess.run(hello)
$ python helloworld.py Hello, TensorFlow!
ãã¼ããããã
èå¿ã®ãã¥ã¼ããªã¢ã«ãåçµ
softmaxæ³
import input_data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) import tensorflow as tf sess = tf.InteractiveSession() # create the model x = tf.placeholder("float", [None, 784]) W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) y= tf.nn.softmax(tf.matmul(x, W) + b) # define loss and optimizer y_ = tf.placeholder("float", [None, 10]) cross_entropy = -tf.reduce_sum(y_*tf.log(y)) train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) # train tf.initialize_all_variables().run() for i in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) train_step.run({x: batch_xs, y_:batch_ys}) # test trained model correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuray = tf.reduce_mean(tf.cast(correct_prediction, "float")) print(accuray.eval({x: mnist.test.images, y_: mnist.test.labels}))
Extracting MNIST_data/train-images-idx3-ubyte.gz Extracting MNIST_data/train-labels-idx1-ubyte.gz Extracting MNIST_data/t10k-images-idx3-ubyte.gz Extracting MNIST_data/t10k-labels-idx1-ubyte.gz 0.9158
ãªããåºãï¼ï¼æ£è§£ç(精度)ãåºãï¼ï¼
ãããªç°¡åã«ã§ãã¡ãã£ãï¼ï¼ï¼ï¼ããããï¼TensorFlow!!
ã¨ã¯ãªãããä½ããã£ã¦ããã®ããå
¨ãããããªãã
çµå±ãæ°å¦(softmaxæ³)ã®ç¥èããªãã¨ä½ãã¦ããã®ããããããªãã
å
¨ç¶ããããªãã®ã ããã©ãã¨ããããTensorFlowãåãç°å¢ãã§ããã¨ãããã¨ã§ã
ãTensorFlowで会話AIを作ってみたãã¨ããã®ã«æåãããã®ã§ããã¡ãã試ãã¦ã¿ãããªãã¨æã£ã¦ããã