-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-TF-Neural-Network.py
291 lines (131 loc) · 3.68 KB
/
03-TF-Neural-Network.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python
# coding: utf-8
# # First Neurons
# In[6]:
import numpy as np
import tensorflow as tf
# ** Set Random Seeds for same results **
# In[7]:
np.random.seed(101)
tf.set_random_seed(101)
# ** Data Setup **
# Setting Up some Random Data for Demonstration Purposes
# In[8]:
rand_a = np.random.uniform(0,100,(5,5))
rand_a
# In[9]:
rand_b = np.random.uniform(0,100,(5,1))
rand_b
# In[10]:
# CONFIRM SAME RANDOM NUMBERS (EXECUTE SEED IN SAME CELL!) Watch video for explanation
np.random.seed(101)
rand_a = np.random.uniform(0,100,(5,5))
rand_b = np.random.uniform(0,100,(5,1))
# ### Placeholders
# In[11]:
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
# ### Operations
# In[13]:
add_op = a+b # tf.add(a,b)
mult_op = a*b #tf.multiply(a,b)
# ### Running Sessions to create Graphs with Feed Dictionaries
# In[17]:
with tf.Session() as sess:
add_result = sess.run(add_op,feed_dict={a:rand_a,b:rand_b})
print(add_result)
print('\n')
mult_result = sess.run(mult_op,feed_dict={a:rand_a,b:rand_b})
print(mult_result)
# ________________________
#
# ________________________
# ## Example Neural Network
# In[18]:
n_features = 10
n_dense_neurons = 3
# In[19]:
# Placeholder for x
x = tf.placeholder(tf.float32,(None,n_features))
# In[21]:
# Variables for w and b
b = tf.Variable(tf.zeros([n_dense_neurons]))
W = tf.Variable(tf.random_normal([n_features,n_dense_neurons]))
# ** Operation Activation Function **
# In[22]:
xW = tf.matmul(x,W)
# In[23]:
z = tf.add(xW,b)
# In[24]:
# tf.nn.relu() or tf.tanh()
a = tf.sigmoid(z) #Activation Function
# ** Variable Intializer! **
# In[25]:
init = tf.global_variables_initializer()
# In[27]:
with tf.Session() as sess:
sess.run(init)
layer_out = sess.run(a,feed_dict={x : np.random.random([1,n_features])})
# In[28]:
print(layer_out)
# We still need to finish off this process with optimization! Let's learn how to do this next.
#
# _____
# ## Full Network Example
#
# Let's work on a regression example, we are trying to solve a very simple equation:
#
# y = mx + b
#
# y will be the y_labels and x is the x_data. We are trying to figure out the slope and the intercept for the line that best fits our data!
# ### Artifical Data (Some Made Up Regression Data)
# In[9]:
x_data = np.linspace(0,10,10) + np.random.uniform(-1.5,1.5,10)
# In[10]:
x_data
# In[11]:
y_label = np.linspace(0,10,10) + np.random.uniform(-1.5,1.5,10)
# In[14]:
import matplotlib.pyplot as plt
get_ipython().magic(u'matplotlib inline')
# In[18]:
plt.plot(x_data,y_label,'*')
# ** Variables **
# In[79]:
np.random.rand(2)
# In[20]:
m = tf.Variable(0.39)
b = tf.Variable(0.2)
# ### Cost Function
# In[22]:
error = 0
for x,y in zip(x_data,y_label):
y_hat = m*x + b #Our predicted value
error += (y-y_hat)**2 # The cost we want to minimize (we'll need to use an optimization function for the minimization!)
# ### Optimizer
# In[23]:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
train = optimizer.minimize(error)
# ### Initialize Variables
# In[24]:
init = tf.global_variables_initializer()
# ### Create Session and Run!
# In[31]:
with tf.Session() as sess:
sess.run(init)
epochs = 1000
for i in range(epochs):
sess.run(train)
# Fetch Back Results
final_slope , final_intercept = sess.run([m,b])
# In[32]:
final_slope
# In[33]:
final_intercept
# ### Evaluate Results
# In[34]:
x_test = np.linspace(-1,11,10)
y_pred_plot = final_slope*x_test + final_intercept
plt.plot(x_test,y_pred_plot,'b')
plt.plot(x_data,y_label,'*')
# # Great Job!