DNN Handson

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

X_data =X.

y_data =y.reshape(1,1000)

layer_dims = [2,9,9,1]

import tensorflow as tf

A_0 = tf.placeholder(dtype = tf.float64, shape = ([num_features,None]))


Y = tf.placeholder(dtype = tf.float64, shape = ([1,None]))

def initialize_parameters_deep(layer_dims):
L = len(layer_dims)
parameters = {}
for l in range(1,L):
parameters['W' + str(l)] =
tf.Variable(initial_value=tf.random_normal([layer_dims[l], layer_dims[l-1]],
dtype=tf.float64)* 0.01)
parameters['b' + str(l)] =
tf.Variable(initial_value=tf.zeros([layer_dims[l],1],dtype=tf.float64) * 0.01)

return parameters

def linear_forward_prop(A_prev,W,b, activation):


Z = tf.add(tf.matmul(W, A_prev), b)
if activation == "sigmoid":
A = Z
elif activation == "relu":
A = tf.nn.relu(Z) #apply relu
activation on Z using tf.nn.relu() function
return A

def l_layer_forwardProp(A_0, parameters):


A = A_0
L = len(parameters)//2
for l in range(1,L):
A_prev = A
A = linear_forward_prop(A_prev,parameters['W' + str(l)],parameters['b' +
str(l)], "relu")
A_final = linear_forward_prop(A, parameters['W' + str(L)], parameters['b' +
str(L)], "sigmoid" )
return A_final

def deep_layer_network(X_data,y_data, layer_dims, learning_rate, num_iter):


num_features = layer_dims[0]
A_0, Y = placeholders(num_features) #call
placeholder function to initialize placeholders A_0 and Y
parameters = initialize_parameters_deep(layer_dims)
#Initialse Weights and bias using initialize_parameters_deep() with layer_dims as
parameters
Z_final = l_layer_forwardProp(A_0,parameters)
#call the function l_layer_forwardProp() to define the final output
cost =
tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=Z_final,labels=Y))
#define the mean cost using sigmoid cross entropy function
train_net = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
#call tensorflow's gradient descent optimizer function with minimize cost
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(num_iter):
_,c = sess.run([train_net, cost], feed_dict={A_0: X_data, Y: y_data})
if i % 1000 == 0:
print(c)
with open("Output.txt", "w") as text_file:
text_file.write("c= %f\n" % c)
params = sess.run(parameters)
return params

parameters = deep_layer_network(X_data, y_data, layer_dims ,0.3, 10000)

You might also like