LoginSignup
4
2

More than 5 years have passed since last update.

Tensorflow導入(Win/Anaconda環境)

Last updated at Posted at 2017-05-28

今回の目標

  • Tensorflow用の環境構築
  • Tensorflowのインストール
  • チュートリアル

Tensorflow用Python環境構築

まずはTensorflow用(Python3.5)の環境を構築する。
Condaにて仮想環境に構築していく。
コマンドプロンプトにて下記コマンドを実行する

conda create -n tensorflow python=3.5

1.png

必要なパッケージインストールをするか聞かれるのでY選択
しばらくたつとインストール完了する。
インストール完了したら環境を切り替える。

activate tensorflow

2.png

Tensorflowのインストール

ここからはpipを利用してTensorflowをインストールする。
pipはpythonのPythonのパッケージ管理システム、よくつかう。

下記コマンドの実行をする。

pip install tensorflow
.
.
.
Successfully built protobuf
Installing collected packages: six, protobuf, numpy, werkzeug, tensorflow
Successfully installed numpy-1.12.1 protobuf-3.3.0 six-1.10.0 tensorflow-1.1.0 werkzeug-0.12.2

Successfully built protobufって表示されればおk

チュートリアル

Tensorflowの公式にサンプルがあるので動かしてみる。
https://www.tensorflow.org/get_started/get_started

import numpy as np
import tensorflow as tf
# Model parameters
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)
# loss
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# training data
x_train = [1,2,3,4]
y_train = [0,-1,-2,-3]
# training loop
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
  sess.run(train, {x:x_train, y:y_train})

# evaluate training accuracy
curr_W, curr_b, curr_loss  = sess.run([W, b, loss], {x:x_train, y:y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))

pythonで実行

(tensorflow) C:\work>python tutorial.py
W: [-0.9999969] b: [ 0.99999082] loss: 5.69997e-11

実行結果だけだとなにがなんだかわからないがざっくりと説明すると

XとYの関係性を線形回帰によって求めている
(要は直線(Y=WX + b)の傾き(W)とb(切片))

image

次回までに調べておくこと

  • TensorflowでのPythonプログラミング
  • TensorBoardの使い方
4
2
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
4
2