In [1]:
%matplotlib inline
import tensorflow as tf

import numpy as np
import matplotlib.pyplot as plt

# 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

plt.scatter(x_data,y_data)

# 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.initialize_all_variables()

# 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]


(0, array([-0.08024696], dtype=float32), array([ 0.5588544], dtype=float32))
(20, array([ 0.03696739], dtype=float32), array([ 0.33439547], dtype=float32))
(40, array([ 0.0839621], dtype=float32), array([ 0.30875152], dtype=float32))
(60, array([ 0.09591936], dtype=float32), array([ 0.30222672], dtype=float32))
(80, array([ 0.09896174], dtype=float32), array([ 0.30056658], dtype=float32))
(100, array([ 0.09973581], dtype=float32), array([ 0.30014417], dtype=float32))
(120, array([ 0.09993279], dtype=float32), array([ 0.3000367], dtype=float32))
(140, array([ 0.09998291], dtype=float32), array([ 0.30000934], dtype=float32))
(160, array([ 0.09999566], dtype=float32), array([ 0.3000024], dtype=float32))
(180, array([ 0.0999989], dtype=float32), array([ 0.30000061], dtype=float32))
(200, array([ 0.09999973], dtype=float32), array([ 0.30000016], dtype=float32))

In [2]:
import sys

sys.path.append("/home/moonbury/github/")
import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
from sklearn.datasets import load_digits
mnist = load_digits()


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-5ea0260dfeae> in <module>()
      2 
      3 sys.path.append("/home/moonbury/github/")
----> 4 import input_data
      5 mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
      6 from sklearn.datasets import load_digits

/home/moonbury/github-book/input_data.py in <module>()
     27 from six.moves import xrange  # pylint: disable=redefined-builtin
     28 import tensorflow as tf
---> 29 from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
     30 

ImportError: No module named contrib.learn.python.learn.datasets.mnist

In [ ]:
import tensorflow as tf

# Create a Constant op that produces a 1x2 matrix.  The op is
# added as a node to the default graph.
#
# The value returned by the constructor represents the output
# of the Constant op.
matrix1 = tf.constant([[3., 3.]])

# Create another Constant that produces a 2x1 matrix.
matrix2 = tf.constant([[2.],[2.]])

# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.
# The returned value, 'product', represents the result of the matrix
# multiplication.
product = tf.matmul(matrix1, matrix2)
# Launch the default graph.
sess = tf.Session()

# To run the matmul op we call the session 'run()' method, passing 'product'
# which represents the output of the matmul op.  This indicates to the call
# that we want to get the output of the matmul op back.
#
# All inputs needed by the op are run automatically by the session.  They
# typically are run in parallel.
#
# The call 'run(product)' thus causes the execution of three ops in the
# graph: the two constants and matmul.
#
# The output of the op is returned in 'result' as a numpy `ndarray` object.
result = sess.run(product)
print(result)
# ==> [[ 12.]]

# Close the Session when we're done.
sess.close()
with tf.Session() as sess:
  result = sess.run([product])
  print(result)

In [ ]: