First, build an environment for Tensorflow (Python 3.5). Build in a virtual environment with Conda. Execute the following command at the command prompt
conda create -n tensorflow python=3.5
You will be asked if you want to install the required packages, so select Y Installation is completed after a while. Switch the environment when the installation is complete.
activate tensorflow
From here, install Tensorflow using pip. pip is a Python package management system for python, which I often use.
Execute the following command.
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
If it says Successfully built protobuf
There is a sample in the official Tensorflow, so let's move it. 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))
Run with python
(tensorflow) C:\work>python tutorial.py
W: [-0.9999969] b: [ 0.99999082] loss: 5.69997e-11
I don't know what it is if it's just the execution result, but if I explain it roughly
Finding the relationship between X and Y by linear regression (In short, the slope (W) and b (intercept) of the straight line (Y = WX + b))
Recommended Posts