8
8

More than 5 years have passed since last update.

tensorflowのdockerイメージを動かしてみた

Last updated at Posted at 2016-05-23

参考

virtualenvを使う場合

  • ubuntu 16.04にて実施
sudo apt install -y python-pip3 python-dev virtualenv
sudo pip3 install --upgrade pip
sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp34-cp34m-linux_x86_64.whl
virtualenv --system-site-packages ~/tensorflow
source ~/tensorflow/bin/activate
(tensorflow)$ deactivate

素の環境に入れる場合

sudo apt-get install -y python-pip python-dev
pip install tensorflow

dockerで動かす場合

docker run -it \
 -p 8888:8888 \
 gcr.io/tensorflow/tensorflow

web.png

2.png

--

チュートリアル

サンプルの関数が変更になっているため以下を参考。

python起動
python
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
  • ./MNIST_data/ に 12MBダウンロードされる。
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)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i 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}))

0.9164 と出力されることを確認。


OpenAI Gym

  • 関係ないが見た目が面白かったのでここにメモっておこう。
git clone https://github.com/openai/gym
cd gym
pip install -e .

pip install gym
サンプル1
import gym
env = gym.make('CartPole-v0')
env.reset()
for _ in range(1000):
    env.render()
    env.step(env.action_space.sample()) # take a random action
サンプル2
import gym
env = gym.make('CartPole-v0')
for i_episode in range(20):
    observation = env.reset()
    for t in range(100):
        env.render()
        #print(observation)
        action = env.action_space.sample()
        observation, reward, done, info = env.step(action)
        if done:
            print("Episode finished after {} timesteps".format(t+1))
            break

CartPole-v0

この部分を以下で出てくるidを突っ込めば良さそう(前提となるソフトは必要だと思う。)

grep id gym/envs/__init__.py

Kerasを使う

pip3 install tensorflow
pip3 install keras

スクリーンショット_2018-07-03_20-42-43.png

8
8
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
8
8