In [1]:
import os
import numpy as np
from sklearn import linear_model
import tensorflow as tf

import matplotlib.pyplot as plt
%matplotlib inline

Very simple graph (scalars)

tensorboard --logdir=/tmp/tensorflow/00_tensorboard/logs_01_scalars


In [12]:
logdir='/tmp/tensorflow/00_tensorboard/logs_01_scalars'
if os.path.exists(logdir):
    os.system('rm -rf {}'.format(logdir))

with tf.Graph().as_default() as graph:
    a = tf.constant(1, name='a')
    b = tf.constant(2, name='b')
    c = tf.add(a, b, name='c')
    d = tf.multiply(a, c, name='d')

with tf.Session(graph=graph) as sess:    
    file_writer = tf.summary.FileWriter(logdir, graph)
    d_out = sess.run(d)

Very simple graph (tensors)

tensorboard --logdir=/tmp/tensorflow/00_tensorboard/logs_02_tensors


In [18]:
logdir='/tmp/tensorflow/00_tensorboard/logs_02_tensors'
if os.path.exists(logdir):
    os.system('rm -rf {}'.format(logdir))

with tf.Graph().as_default() as graph:
    a = tf.constant([[1,2], [3,4], [4,5]], name='a')  # 3x2 matrix
    b = tf.constant([[1,2,3], [4,5,6]], name='b')     # 2x3 matrix
    c = tf.matmul(a, b, name='c')

with tf.Session(graph=graph) as sess:    
    file_writer = tf.summary.FileWriter(logdir, graph)
    c_out = sess.run(c)
c_out


Out[18]:
array([[ 9, 12, 15],
       [19, 26, 33],
       [24, 33, 42]], dtype=int32)

Linear Regression


In [4]:
# Create mock data, solve an sklearn linear model
Nsamp = 50
Nfeatures = 1
xarr = np.linspace(-0.5, 0.5, Nsamp)
np.random.seed(83749)
beta_0 = -2.0
beta_1 = 4.3
yarr = (beta_0 + beta_1 * xarr) + (np.random.normal(size=Nsamp) * 0.5)

mdl = linear_model.LinearRegression(fit_intercept=False)
mdl = mdl.fit(np.c_[np.ones(Nsamp), xarr], yarr)
mdl.coef_


Out[4]:
array([-2.0517912 ,  4.08511933])

In [5]:
fig, ax = plt.subplots(figsize=(5,5))
plt.scatter(xarr, yarr, s=10, color='blue')
plt.plot(xarr, mdl.coef_[0] + mdl.coef_[1] * xarr, color='red')


Out[5]:
[<matplotlib.lines.Line2D at 0x7feb4df442b0>]

In [6]:
logdir='/tmp/tensorflow/00_tensorboard/logs'
if os.path.exists(logdir):
    print('removing logdir')
    os.system('rm -rf {}'.format(logdir))

with tf.Graph().as_default() as graph:

    Nfeatures=1
    X = tf.placeholder(tf.float32, [None, Nfeatures], name='X')
    y = tf.placeholder(tf.float32, [None, 1], name='y')

    with tf.name_scope('logit'):
        W = tf.Variable(tf.random_normal([Nfeatures, 1]), name='W')
        b = tf.Variable(tf.zeros([1]), name='b')
        z = tf.matmul(X, W) + b
        
        tf.summary.histogram('weights', W)
        tf.summary.histogram('biases', b)
        tf.summary.histogram('z', z)
            
    with tf.name_scope('mse'):
        loss = tf.reduce_mean(tf.squared_difference(z, y), name='loss')
        tf.summary.scalar('loss', loss)
        
    with tf.name_scope('train'):
        learning_rate = 0.1
        train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
    
    merged_summary = tf.summary.merge_all()
    
feed_dict = {X: xarr.reshape(Nsamp, 1), y: yarr.reshape(Nsamp,1)}
with tf.Session(graph=graph) as sess:    
    
    sess.run(tf.global_variables_initializer())
    file_writer = tf.summary.FileWriter(logdir)
    file_writer.add_graph(graph)
        
    for i in range(300):
        train_step_out = sess.run(train_step, feed_dict=feed_dict)
        loss_out = sess.run(loss, feed_dict=feed_dict)    
        print(i, loss_out)
        s = sess.run(merged_summary, feed_dict=feed_dict)
        file_writer.add_summary(s, i)
        
    W_out = sess.run(W, feed_dict=feed_dict)
    b_out = sess.run(b, feed_dict=feed_dict)
    
print('W_out: ', W_out)
print('b_out: ', b_out)


removing logdir
0 5.17122
1 4.12371
2 3.42805
3 2.95844
4 2.63434
5 2.40418
6 2.23492
7 2.10539
8 2.00202
9 1.9161
10 1.84202
11 1.77618
12 1.71624
13 1.6607
14 1.60856
15 1.55916
16 1.51208
17 1.46701
18 1.42373
19 1.38211
20 1.34202
21 1.30337
22 1.26609
23 1.23013
24 1.19541
25 1.16191
26 1.12956
27 1.09833
28 1.06817
29 1.03906
30 1.01094
31 0.983799
32 0.957588
33 0.932279
34 0.90784
35 0.884242
36 0.861456
37 0.839454
38 0.818208
39 0.797693
40 0.777884
41 0.758756
42 0.740285
43 0.72245
44 0.705229
45 0.6886
46 0.672542
47 0.657037
48 0.642065
49 0.627608
50 0.613649
51 0.600169
52 0.587153
53 0.574585
54 0.562449
55 0.55073
56 0.539415
57 0.528488
58 0.517938
59 0.50775
60 0.497913
61 0.488414
62 0.479241
63 0.470384
64 0.461832
65 0.453574
66 0.4456
67 0.4379
68 0.430465
69 0.423286
70 0.416353
71 0.409659
72 0.403196
73 0.396954
74 0.390927
75 0.385108
76 0.379489
77 0.374062
78 0.368823
79 0.363764
80 0.358878
81 0.354161
82 0.349606
83 0.345208
84 0.340961
85 0.33686
86 0.3329
87 0.329076
88 0.325384
89 0.321819
90 0.318376
91 0.315052
92 0.311842
93 0.308742
94 0.30575
95 0.30286
96 0.300069
97 0.297374
98 0.294772
99 0.29226
100 0.289834
101 0.287491
102 0.285229
103 0.283045
104 0.280936
105 0.278899
106 0.276933
107 0.275034
108 0.2732
109 0.27143
110 0.26972
111 0.268069
112 0.266475
113 0.264936
114 0.26345
115 0.262015
116 0.260629
117 0.259291
118 0.257999
119 0.256751
120 0.255546
121 0.254383
122 0.25326
123 0.252175
124 0.251127
125 0.250116
126 0.24914
127 0.248197
128 0.247286
129 0.246407
130 0.245558
131 0.244738
132 0.243946
133 0.243182
134 0.242444
135 0.241731
136 0.241043
137 0.240378
138 0.239737
139 0.239117
140 0.238519
141 0.237941
142 0.237383
143 0.236845
144 0.236325
145 0.235822
146 0.235337
147 0.234869
148 0.234417
149 0.23398
150 0.233559
151 0.233151
152 0.232758
153 0.232379
154 0.232012
155 0.231658
156 0.231317
157 0.230987
158 0.230668
159 0.23036
160 0.230063
161 0.229776
162 0.229499
163 0.229232
164 0.228973
165 0.228724
166 0.228483
167 0.228251
168 0.228026
169 0.227809
170 0.2276
171 0.227398
172 0.227202
173 0.227014
174 0.226832
175 0.226656
176 0.226486
177 0.226322
178 0.226164
179 0.226011
180 0.225864
181 0.225721
182 0.225584
183 0.225451
184 0.225323
185 0.225199
186 0.225079
187 0.224964
188 0.224852
189 0.224745
190 0.224641
191 0.22454
192 0.224443
193 0.22435
194 0.224259
195 0.224172
196 0.224088
197 0.224006
198 0.223928
199 0.223852
200 0.223779
201 0.223708
202 0.223639
203 0.223574
204 0.22351
205 0.223448
206 0.223389
207 0.223332
208 0.223276
209 0.223223
210 0.223171
211 0.223121
212 0.223073
213 0.223027
214 0.222982
215 0.222938
216 0.222897
217 0.222856
218 0.222817
219 0.222779
220 0.222743
221 0.222708
222 0.222674
223 0.222641
224 0.22261
225 0.222579
226 0.22255
227 0.222521
228 0.222494
229 0.222467
230 0.222441
231 0.222417
232 0.222393
233 0.22237
234 0.222347
235 0.222326
236 0.222305
237 0.222285
238 0.222266
239 0.222247
240 0.222229
241 0.222211
242 0.222194
243 0.222178
244 0.222162
245 0.222147
246 0.222133
247 0.222119
248 0.222105
249 0.222092
250 0.222079
251 0.222067
252 0.222055
253 0.222043
254 0.222032
255 0.222022
256 0.222011
257 0.222001
258 0.221992
259 0.221982
260 0.221973
261 0.221965
262 0.221956
263 0.221948
264 0.22194
265 0.221933
266 0.221926
267 0.221919
268 0.221912
269 0.221905
270 0.221899
271 0.221893
272 0.221887
273 0.221881
274 0.221876
275 0.22187
276 0.221865
277 0.22186
278 0.221856
279 0.221851
280 0.221847
281 0.221842
282 0.221838
283 0.221834
284 0.22183
285 0.221826
286 0.221823
287 0.221819
288 0.221816
289 0.221813
290 0.22181
291 0.221807
292 0.221804
293 0.221801
294 0.221798
295 0.221795
296 0.221793
297 0.22179
298 0.221788
299 0.221786
W_out:  [[ 4.05788183]]
b_out:  [-2.05179071]

In [7]:
tf.global_variables_initializer?

In [ ]:


In [ ]: