In [19]:
import tensorflow as tf
import numpy as np

In [25]:
x_data = np.random.rand(100).astype(np.float32)
print(x_data)


[ 0.43469468  0.65527654  0.78675216  0.79427147  0.23657775  0.88878953
  0.94384634  0.05068941  0.54046369  0.87063062  0.1574633   0.65103573
  0.85449117  0.37055284  0.9151392   0.65974134  0.95800644  0.94433361
  0.32356024  0.15978876  0.94846904  0.37061155  0.29533964  0.19133037
  0.04159376  0.33898985  0.20884091  0.86296666  0.19866869  0.84852368
  0.2617054   0.48187113  0.73228848  0.38402605  0.47428289  0.03392767
  0.13606964  0.66884035  0.54789156  0.07279083  0.96695447  0.19278114
  0.33895227  0.00257203  0.7011593   0.8077938   0.09738129  0.08923178
  0.53565872  0.62010688  0.89305645  0.81902188  0.48001835  0.96633303
  0.90523988  0.29254562  0.74303544  0.35649765  0.72041565  0.02808734
  0.00415172  0.20355359  0.75164062  0.96870512  0.03173685  0.24996719
  0.13896663  0.62147403  0.64108646  0.71893662  0.0799912   0.58506507
  0.14437298  0.54614198  0.53348452  0.69093692  0.55897504  0.20684461
  0.48934555  0.56976855  0.096649    0.18480648  0.55376893  0.25846988
  0.97604162  0.23481321  0.63729632  0.08544814  0.12049389  0.70328301
  0.92926657  0.18205433  0.8891415   0.43182161  0.55089813  0.67362851
  0.9926098   0.26285392  0.41304225  0.19253661]

In [27]:
y_data = x_data*0.1+0.3
print(y_data)


[ 0.34346947  0.36552766  0.37867522  0.37942716  0.32365778  0.38887897
  0.39438465  0.30506894  0.35404637  0.38706309  0.31574634  0.3651036
  0.38544914  0.3370553   0.39151394  0.36597416  0.39580065  0.39443338
  0.33235604  0.31597888  0.39484692  0.33706117  0.32953396  0.31913304
  0.3041594   0.33389899  0.32088411  0.38629669  0.3198669   0.38485238
  0.32617056  0.34818712  0.37322885  0.33840263  0.34742829  0.30339277
  0.31360698  0.36688405  0.35478917  0.30727911  0.39669546  0.31927812
  0.33389524  0.30025721  0.37011594  0.38077939  0.30973813  0.30892318
  0.35356587  0.36201069  0.38930565  0.38190222  0.34800184  0.39663333
  0.390524    0.32925457  0.37430355  0.33564979  0.37204158  0.30280873
  0.30041519  0.32035536  0.37516409  0.39687052  0.30317369  0.32499674
  0.31389669  0.36214742  0.36410865  0.37189367  0.30799913  0.35850653
  0.3144373   0.3546142   0.35334846  0.36909372  0.35589752  0.32068446
  0.34893456  0.35697687  0.30966491  0.31848067  0.3553769   0.325847
  0.39760417  0.32348132  0.36372966  0.30854481  0.31204939  0.37032831
  0.39292666  0.31820545  0.38891417  0.34318218  0.35508981  0.36736286
  0.399261    0.32628539  0.34130424  0.31925368]

In [32]:
weights = tf.Variable(tf.random_uniform([1],-1.0,-1.0))
biases = tf.Variable(tf.zeros([1]))
y = weights * x_data + biases
print(y)


Tensor("add_2:0", shape=(100,), dtype=float32)

In [34]:
loss = tf.reduce_mean(tf.square(y-y_data))

In [35]:
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

In [36]:
init = tf.global_variables_initializer()

In [53]:
import matplotlib.pyplot as plt
%pylab inline
sess = tf.Session()
sess.run(init)          # Very important
plt.scatter(0.1,0.3)

for step in range(201):
    sess.run(train)
    if step % 10 == 0:
        x = plt.scatter(sess.run(weights),sess.run(biases))
        print(step, sess.run(weights), sess.run(biases))


Populating the interactive namespace from numpy and matplotlib
0 [-0.4881385] [ 0.83851647]
10 [-0.21896747] [ 0.46848932]
20 [-0.04912771] [ 0.37877411]
30 [ 0.03027799] [ 0.33682945]
40 [ 0.06740271] [ 0.31721896]
50 [ 0.08475972] [ 0.30805042]
60 [ 0.09287469] [ 0.30376384]
70 [ 0.09666869] [ 0.30175972]
80 [ 0.09844251] [ 0.30082273]
90 [ 0.09927183] [ 0.30038467]
100 [ 0.09965955] [ 0.30017984]
110 [ 0.09984084] [ 0.30008408]
120 [ 0.09992559] [ 0.30003932]
130 [ 0.09996521] [ 0.3000184]
c:\python35\lib\site-packages\IPython\core\magics\pylab.py:160: UserWarning: pylab import has clobbered these variables: ['step']
`%matplotlib` prevents importing * from pylab and numpy
  "\n`%matplotlib` prevents importing * from pylab and numpy"
140 [ 0.09998373] [ 0.30000859]
150 [ 0.0999924] [ 0.30000404]
160 [ 0.09999644] [ 0.30000189]
170 [ 0.09999834] [ 0.30000088]
180 [ 0.09999923] [ 0.30000043]
190 [ 0.09999963] [ 0.30000022]
200 [ 0.09999982] [ 0.3000001]

In [ ]: