Anaconda環境でTensorFlowをインストール Installing TensorFlow with Anaconda.

環境構築手順を整理しておこうと思ったが、
公式に素晴らしいチュートリアルがあるのでそれを実行し、補足点を記述する。

目次
1.インストール
2.チュートリアル
3.途中詰まったところ


1.インストール
ここ参照:https://www.tensorflow.org/install/install_windows

今回の目的はwindows上かつ、anacondaだが、他環境のものもある。
Installing TensorFlow  |  TensorFlow

CPU support only.とGPU supportとがあるが、GPUを利用したい場合でもまず前者をインストールしてみなさいとの記述がある。

環境を別で作って、アクティベートして、pipでインストールする。

C:> conda create -n tensorflow 
C:> activate tensorflow
(tensorflow)C:> pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.0.1-cp35-cp35m-win_amd64.whl 


2.チュートリアル
チュートリアル上のコードをそのまま実行しただけだが、以下のコード
https://www.tensorflow.org/get_started/mnist/beginners

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 784])

W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x, W) + b)

y_ = tf.placeholder(tf.float32, [None, 10])

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess = tf.InteractiveSession()

tf.global_variables_initializer().run()

for _ in range(1000):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))


3.途中詰まったところ
途中同じコードを実行しているはずなのに下図のエラーが出たりでなかったりがあった。
原因は未特定だが、"unknown op"とあるので、対象のopが読み込まれていない可能性がある。
なのでimport tensorflow as tf でもう一度ライブラリをインポートしなおすと直るかもしれない。
f:id:Tug-uca:20170317072050p:plain