Create a gist now

Instantly share code, notes, and snippets.

@junichiro /file0.txt
Last active Jan 13, 2017

What would you like to do?
多分もっともわかりやすいTensorFlow 入門 (Introduction) ref: http://qiita.com/junichiro/items/8886f3976fc20f73335f
% virtualenv --system-site-packages ~/env/tensorflow
% source ~/env/tensorflow/bin/activate
# Before starting, initialize the variables. We will 'run' this first.
init = tf.global_variables_initializer()
# Launch the graph.
sess = tf.Session()
sess.run(init)
# Fit the line.
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(W), sess.run(b))
# Learns best fit is W: [0.1], b: [0.3]
# Close the Session when we're done.
sess.close()
0 [ 0.17769754] [ 0.34861392]
20 [ 0.1106104] [ 0.29447961]
40 [ 0.10272982] [ 0.29857972]
60 [ 0.10070232] [ 0.29963461]
80 [ 0.10018069] [ 0.29990602]
100 [ 0.1000465] [ 0.29997581]
120 [ 0.10001197] [ 0.29999378]
140 [ 0.10000309] [ 0.2999984]
160 [ 0.1000008] [ 0.29999959]
180 [ 0.10000021] [ 0.29999989]
200 [ 0.1000001] [ 0.29999995]
(tensorflow) %
(tensorflow) % export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.1-py3-none-any.whl
(tensorflow) % pip3 install --upgrade $TF_BINARY_URL
(tensorflow) % python
...
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42
import tensorflow as tf
import numpy as np
# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3
# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b
# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
# Before starting, initialize the variables. We will 'run' this first.
init = tf.global_variables_initializer()
# Launch the graph.
sess = tf.Session()
sess.run(init)
# Fit the line.
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(W), sess.run(b))
# Learns best fit is W: [0.1], b: [0.3]
# Close the Session when we're done.
sess.close()
import tensorflow as tf
import numpy as np
# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3
# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b
# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment