In [1]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline


/home/santi/miniconda3/envs/data_science/lib/python3.5/site-packages/matplotlib/font_manager.py:280: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  'Matplotlib is building the font cache using fc-list. '

In [2]:
# Create sample data
num_samples = 500
x = np.linspace(0, 1, num_samples) + 0.01*np.random.randn(num_samples)
plt.figure(figsize=(10, 10))
plt.plot(x)


Out[2]:
[<matplotlib.lines.Line2D at 0x7f524d11cf28>]

In [3]:
y = 3 * x + 7
y += 0.5*np.random.randn(num_samples)
plt.figure(figsize=(10, 10))
plt.scatter(x, y, marker='x')


Out[3]:
<matplotlib.collections.PathCollection at 0x7f524d22fcf8>

In [4]:
x = np.atleast_2d(x)
y = np.atleast_2d(y)
print(x.shape)
print(y.shape)


(1, 500)
(1, 500)

Linear regression in TF


In [5]:
# Slope 
a = tf.Variable(0.)
# Intercept
b = tf.Variable(1.0)
# LR model
y_estimated = a * x + b

In [6]:
# Cost function
loss = tf.reduce_mean(tf.square(y - y_estimated))
# Gradient descent optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

In [77]:
# Train main loop
loss_values = []
with tf.Session() as session:
    session.run(tf.global_variables_initializer())
    print('Initial values: a={}  b={} loss={}'.format(
        *session.run([a, b, loss])
    ))
    for step in range(150):
        print('Step {}'.format(step))
        opt_res = session.run([train, loss, a, b])
        print('Loss={}  a={}  b={}'.format(*opt_res[1:]))
        print()
        loss_values.append(opt_res[1])


Initial values: a=0.0  b=1.0 loss=57.60578536987305
Step 0
Loss=57.60578536987305  a=0.08041410148143768  b=1.1504342555999756

Step 1
Loss=54.73299789428711  a=0.15878435969352722  b=1.297054648399353

Step 2
Loss=52.00410079956055  a=0.2351626455783844  b=1.439958095550537

Step 3
Loss=49.411903381347656  a=0.30959945917129517  b=1.579238772392273

Step 4
Loss=46.94953155517578  a=0.38214409351348877  b=1.7149885892868042

Step 5
Loss=44.61050796508789  a=0.452844500541687  b=1.847297191619873

Step 6
Loss=42.388641357421875  a=0.5217475295066833  b=1.9762517213821411

Step 7
Loss=40.27806091308594  a=0.5888987183570862  b=2.1019372940063477

Step 8
Loss=38.2732048034668  a=0.654342532157898  b=2.2244369983673096

Step 9
Loss=36.3687629699707  a=0.7181223034858704  b=2.3438315391540527

Step 10
Loss=34.55971145629883  a=0.7802802324295044  b=2.4601995944976807

Step 11
Loss=32.84126663208008  a=0.8408574461936951  b=2.573617935180664

Step 12
Loss=31.208906173706055  a=0.899894118309021  b=2.684161424636841

Step 13
Loss=29.65830421447754  a=0.9574292302131653  b=2.791903018951416

Step 14
Loss=28.18537139892578  a=1.0135009288787842  b=2.896913766860962

Step 15
Loss=26.78621482849121  a=1.0681463479995728  b=2.999263048171997

Step 16
Loss=25.45713996887207  a=1.1214016675949097  b=3.099018096923828

Step 17
Loss=24.194637298583984  a=1.173302173614502  b=3.196244955062866

Step 18
Loss=22.995370864868164  a=1.2238821983337402  b=3.2910077571868896

Step 19
Loss=21.856176376342773  a=1.2731752395629883  b=3.383368968963623

Step 20
Loss=20.77403450012207  a=1.321213960647583  b=3.4733893871307373

Step 21
Loss=19.746103286743164  a=1.3680301904678345  b=3.5611283779144287

Step 22
Loss=18.769657135009766  a=1.4136549234390259  b=3.646643877029419

Step 23
Loss=17.842117309570312  a=1.4581184387207031  b=3.729992389678955

Step 24
Loss=16.961036682128906  a=1.5014501810073853  b=3.8112287521362305

Step 25
Loss=16.12409210205078  a=1.5436787605285645  b=3.890406608581543

Step 26
Loss=15.329065322875977  a=1.5848323106765747  b=3.967578172683716

Step 27
Loss=14.573862075805664  a=1.6249380111694336  b=4.042794227600098

Step 28
Loss=13.856485366821289  a=1.664022445678711  b=4.116104602813721

Step 29
Loss=13.175039291381836  a=1.7021116018295288  b=4.187557220458984

Step 30
Loss=12.527728080749512  a=1.7392306327819824  b=4.257199287414551

Step 31
Loss=11.912839889526367  a=1.7754042148590088  b=4.325077056884766

Step 32
Loss=11.328750610351562  a=1.8106564283370972  b=4.3912353515625

Step 33
Loss=10.77391529083252  a=1.84501051902771  b=4.45571756362915

Step 34
Loss=10.246871948242188  a=1.8784892559051514  b=4.518566131591797

Step 35
Loss=9.746225357055664  a=1.9111149311065674  b=4.579822540283203

Step 36
Loss=9.270658493041992  a=1.9429091215133667  b=4.639527320861816

Step 37
Loss=8.818909645080566  a=1.9738930463790894  b=4.697719573974609

Step 38
Loss=8.389787673950195  a=2.004087209701538  b=4.754437446594238

Step 39
Loss=7.982163429260254  a=2.0335116386413574  b=4.809719085693359

Step 40
Loss=7.59495210647583  a=2.062185764312744  b=4.863600254058838

Step 41
Loss=7.227138042449951  a=2.0901286602020264  b=4.916116714477539

Step 42
Loss=6.877748489379883  a=2.117358922958374  b=4.967303276062012

Step 43
Loss=6.545855522155762  a=2.143894672393799  b=5.01719331741333

Step 44
Loss=6.230588912963867  a=2.1697535514831543  b=5.06581974029541

Step 45
Loss=5.93111515045166  a=2.1949524879455566  b=5.11321496963501

Step 46
Loss=5.646639823913574  a=2.219508409500122  b=5.159409999847412

Step 47
Loss=5.376412391662598  a=2.2434377670288086  b=5.204435348510742

Step 48
Loss=5.11972188949585  a=2.266756296157837  b=5.248320579528809

Step 49
Loss=4.875886917114258  a=2.2894794940948486  b=5.291094779968262

Step 50
Loss=4.6442646980285645  a=2.3116226196289062  b=5.3327860832214355

Step 51
Loss=4.424243927001953  a=2.333200216293335  b=5.373421669006348

Step 52
Loss=4.215244770050049  a=2.354226589202881  b=5.413028717041016

Step 53
Loss=4.016712188720703  a=2.374715805053711  b=5.451632976531982

Step 54
Loss=3.828125476837158  a=2.394681692123413  b=5.489260196685791

Step 55
Loss=3.6489832401275635  a=2.414137363433838  b=5.52593469619751

Step 56
Loss=3.4788153171539307  a=2.433095693588257  b=5.561680793762207

Step 57
Loss=3.317171335220337  a=2.4515693187713623  b=5.596522331237793

Step 58
Loss=3.1636223793029785  a=2.4695706367492676  b=5.630482196807861

Step 59
Loss=3.0177645683288574  a=2.4871115684509277  b=5.663582801818848

Step 60
Loss=2.879211664199829  a=2.5042037963867188  b=5.695845603942871

Step 61
Loss=2.7475996017456055  a=2.5208587646484375  b=5.727292060852051

Step 62
Loss=2.6225788593292236  a=2.5370874404907227  b=5.7579426765441895

Step 63
Loss=2.5038211345672607  a=2.552900791168213  b=5.78781795501709

Step 64
Loss=2.3910112380981445  a=2.5683093070983887  b=5.816937446594238

Step 65
Loss=2.283851385116577  a=2.5833232402801514  b=5.845320224761963

Step 66
Loss=2.1820590496063232  a=2.5979526042938232  b=5.872984886169434

Step 67
Loss=2.0853657722473145  a=2.6122071743011475  b=5.89995002746582

Step 68
Loss=1.993514895439148  a=2.626096487045288  b=5.926232814788818

Step 69
Loss=1.906265139579773  a=2.63962984085083  b=5.951850891113281

Step 70
Loss=1.8233858346939087  a=2.6528160572052  b=5.976821422576904

Step 71
Loss=1.7446566820144653  a=2.6656641960144043  b=6.001160621643066

Step 72
Loss=1.6698708534240723  a=2.678182601928711  b=6.024884223937988

Step 73
Loss=1.5988314151763916  a=2.6903798580169678  b=6.048007965087891

Step 74
Loss=1.5313501358032227  a=2.7022640705108643  b=6.070547103881836

Step 75
Loss=1.467248558998108  a=2.7138431072235107  b=6.0925164222717285

Step 76
Loss=1.4063576459884644  a=2.7251248359680176  b=6.113930702209473

Step 77
Loss=1.3485163450241089  a=2.736116647720337  b=6.134803771972656

Step 78
Loss=1.2935718297958374  a=2.746825933456421  b=6.155148983001709

Step 79
Loss=1.2413800954818726  a=2.7572600841522217  b=6.174980163574219

Step 80
Loss=1.1918022632598877  a=2.767425775527954  b=6.194310188293457

Step 81
Loss=1.1447076797485352  a=2.777329921722412  b=6.213151931762695

Step 82
Loss=1.0999715328216553  a=2.7869791984558105  b=6.231517791748047

Step 83
Loss=1.0574759244918823  a=2.7963802814483643  b=6.249419689178467

Step 84
Loss=1.0171087980270386  a=2.805539131164551  b=6.26686954498291

Step 85
Loss=0.9787631630897522  a=2.814462184906006  b=6.283878803253174

Step 86
Loss=0.9423381090164185  a=2.823155164718628  b=6.3004584312438965

Step 87
Loss=0.9077374935150146  a=2.8316240310668945  b=6.316619396209717

Step 88
Loss=0.8748698830604553  a=2.839874505996704  b=6.332372188568115

Step 89
Loss=0.8436486721038818  a=2.847912073135376  b=6.347727298736572

Step 90
Loss=0.8139911890029907  a=2.8557422161102295  b=6.362695217132568

Step 91
Loss=0.7858186364173889  a=2.863370180130005  b=6.377285003662109

Step 92
Loss=0.7590574026107788  a=2.8708009719848633  b=6.391506671905518

Step 93
Loss=0.7336366176605225  a=2.878039598464966  b=6.405369758605957

Step 94
Loss=0.7094885110855103  a=2.8850910663604736  b=6.418882846832275

Step 95
Loss=0.6865503191947937  a=2.8919601440429688  b=6.4320549964904785

Step 96
Loss=0.6647608876228333  a=2.898651361465454  b=6.444895267486572

Step 97
Loss=0.6440624594688416  a=2.9051692485809326  b=6.457411766052246

Step 98
Loss=0.6244004964828491  a=2.9115183353424072  b=6.4696125984191895

Step 99
Loss=0.60572350025177  a=2.9177026748657227  b=6.481505870819092

Step 100
Loss=0.5879817605018616  a=2.923726797103882  b=6.493099212646484

Step 101
Loss=0.5711286664009094  a=2.9295945167541504  b=6.504400253295898

Step 102
Loss=0.5551196932792664  a=2.935309886932373  b=6.515416622161865

Step 103
Loss=0.5399123430252075  a=2.9408767223358154  b=6.526155471801758

Step 104
Loss=0.5254666805267334  a=2.946298837661743  b=6.536623954772949

Step 105
Loss=0.5117442011833191  a=2.9515798091888428  b=6.546828746795654

Step 106
Loss=0.49870893359184265  a=2.956723213195801  b=6.556776523590088

Step 107
Loss=0.4863266944885254  a=2.9617326259613037  b=6.566473960876465

Step 108
Loss=0.4745643138885498  a=2.966611623764038  b=6.575927257537842

Step 109
Loss=0.4633910059928894  a=2.9713633060455322  b=6.585142612457275

Step 110
Loss=0.4527771770954132  a=2.9759910106658936  b=6.594126224517822

Step 111
Loss=0.4426947832107544  a=2.9804978370666504  b=6.602883815765381

Step 112
Loss=0.4331173002719879  a=2.984886646270752  b=6.61142110824585

Step 113
Loss=0.424019455909729  a=2.9891607761383057  b=6.619743824005127

Step 114
Loss=0.415377140045166  a=2.9933228492736816  b=6.627857208251953

Step 115
Loss=0.4071676433086395  a=2.997375965118408  b=6.635766506195068

Step 116
Loss=0.3993692994117737  a=3.0013227462768555  b=6.643476963043213

Step 117
Loss=0.3919614851474762  a=3.0051660537719727  b=6.650993824005127

Step 118
Loss=0.3849245011806488  a=3.008908271789551  b=6.658321857452393

Step 119
Loss=0.37823987007141113  a=3.012552261352539  b=6.665465831756592

Step 120
Loss=0.3718898892402649  a=3.0161001682281494  b=6.672430515289307

Step 121
Loss=0.3658579885959625  a=3.019554615020752  b=6.679220199584961

Step 122
Loss=0.36012813448905945  a=3.0229179859161377  b=6.685839653015137

Step 123
Loss=0.35468506813049316  a=3.0261926651000977  b=6.692293167114258

Step 124
Loss=0.34951433539390564  a=3.0293807983398438  b=6.69858455657959

Step 125
Loss=0.3446027934551239  a=3.032484769821167  b=6.704718112945557

Step 126
Loss=0.3399370014667511  a=3.0355064868927  b=6.710698127746582

Step 127
Loss=0.3355048596858978  a=3.0384480953216553  b=6.716528415679932

Step 128
Loss=0.331294447183609  a=3.041311740875244  b=6.722212314605713

Step 129
Loss=0.3272949159145355  a=3.0440993309020996  b=6.72775411605835

Step 130
Loss=0.323495477437973  a=3.0468127727508545  b=6.733157157897949

Step 131
Loss=0.31988635659217834  a=3.0494539737701416  b=6.738424777984619

Step 132
Loss=0.3164578378200531  a=3.0520248413085938  b=6.743560791015625

Step 133
Loss=0.31320086121559143  a=3.0545270442962646  b=6.748568534851074

Step 134
Loss=0.3101068139076233  a=3.056962251663208  b=6.753450870513916

Step 135
Loss=0.3071678578853607  a=3.0593323707580566  b=6.758211135864258

Step 136
Loss=0.30437585711479187  a=3.0616390705108643  b=6.762852668762207

Step 137
Loss=0.30172351002693176  a=3.0638837814331055  b=6.767378330230713

Step 138
Loss=0.29920393228530884  a=3.066068172454834  b=6.771790981292725

Step 139
Loss=0.2968103885650635  a=3.0681939125061035  b=6.776093482971191

Step 140
Loss=0.29453662037849426  a=3.0702621936798096  b=6.7802886962890625

Step 141
Loss=0.2923765778541565  a=3.072274684906006  b=6.784379005432129

Step 142
Loss=0.290324866771698  a=3.074232816696167  b=6.78836727142334

Step 143
Loss=0.2883757948875427  a=3.0761380195617676  b=6.7922563552856445

Step 144
Loss=0.2865240275859833  a=3.077991485595703  b=6.796048641204834

Step 145
Loss=0.28476494550704956  a=3.0797946453094482  b=6.799746513366699

Step 146
Loss=0.2830938994884491  a=3.0815486907958984  b=6.803352355957031

Step 147
Loss=0.28150641918182373  a=3.083254814147949  b=6.806868553161621

Step 148
Loss=0.2799983322620392  a=3.084914445877075  b=6.81029748916626

Step 149
Loss=0.2785656750202179  a=3.0865285396575928  b=6.81364107131958


In [78]:
plt.figure(figsize=(10, 10))
plt.plot(loss_values)
plt.ylabel('Loss')
plt.xlabel('Iteration')


Out[78]:
<matplotlib.text.Text at 0x2898340ba20>

In [ ]: