Last active
January 13, 2017 01:24
-
-
Save junichiro/78111e636a8a8e07a016f7ea40b2cf1a to your computer and use it in GitHub Desktop.
多分もっともわかりやすいTensorFlow 入門 (Introduction) ref: http://qiita.com/junichiro/items/8886f3976fc20f73335f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% virtualenv --system-site-packages ~/env/tensorflow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% source ~/env/tensorflow/bin/activate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Before starting, initialize the variables. We will 'run' this first. | |
init = tf.global_variables_initializer() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(tensorflow) % |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import numpy as np |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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