<jupyter_start><jupyter_text>Convolution and Pooling layer<jupyter_code>x = tf.placeholder(tf.float32 , [None , 4096])
y_ = tf.placeholder(tf.int32 , [None , 40])

#Add Function to create weight variable
def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)


#Add function to create bias variable
def bias_variable(shape):
    initial = tf.constant(0.1,shape = shape)
    return tf.Variable(initial)

#Add convolution function
def conv2d(x, W):
    return (tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding = "SAME"))

#Add pooling function
def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding= "SAME")

#Reshape the data for inputting it to the input layer
x_image= tf.reshape(x, [-1, 64, 64, 1])

#create 1st convolutional and pooling layer
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])

h_conv1 = tf.nn.relu(conv2d(x_image,  W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

print("Layer 1(CONV + POOLING)", h_pool1)

#create 2nd convolutional and pooling layer
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])


h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

print("Layer 2(CONV + POOLING)", h_pool2)

#create 3rd convolutional and pooling layer
W_conv3 = weight_variable([5, 5, 64, 128])
b_conv3 = bias_variable([128])

h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3)
h_pool3 = max_pool_2x2(h_conv3)

print("Layer 3(CONV + POOLING)", h_pool3)

#1st fully connected layer
W_fc1 = weight_variable([8*8*128, 1024])
b_fc1 = bias_variable([1024])

#Reshape the last convolutional layer output to connect with fully connected layer
h_pool2_flat = tf.reshape(h_pool3, [-1, 8*8*128])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1)+ b_fc1)

#Add dropout layer
rate = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, rate)

print('FC Layer 4', h_fc1_drop)
#output layer
W_fc2 = weight_variable([1024, 40])
b_fc2 = bias_variable([40])

y_conv = tf.matmul(h_fc1_drop, W_fc2)+ b_fc2

print('Output Layer 5', y_conv)<jupyter_output>Layer 1(CONV + POOLING) Tensor("MaxPool_3:0", shape=(?, 32, 32, 32), dtype=float32)
Layer 2(CONV + POOLING) Tensor("MaxPool_4:0", shape=(?, 16, 16, 64), dtype=float32)
Layer 3(CONV + POOLING) Tensor("MaxPool_5:0", shape=(?, 8, 8, 128), dtype=float32)
FC Layer 4 Tensor("dropout_1/mul_1:0", shape=(?, 1024), dtype=float32)
Output Layer 5 Tensor("add_9:0", shape=(?, 40), dtype=float32)<jupyter_text>Training<jupyter_code>#Network Base of Computation

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_ , logits=y_conv))
training_op = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

#Prediction Measure
correct_prediction = tf.equal(tf.argmax(y_conv , 1),tf.arg_max(y_ , 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction , tf.float32))

saver = tf.train.Saver()

#Mention about the number of epochs training has to happen
n_epochs= 200

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for epochs in range(n_epochs):
        sess.run(training_op, feed_dict = {x: images_DataTraining, y_: labels_DataTraining , rate:0.5})
        acc_train = accuracy.eval(feed_dict = {x: images_DataTraining, y_:labels_DataTraining, rate:1})
        acc_validation = accuracy.eval(feed_dict = {x: images_DataValidation, y_:labels_DataValidation, rate:0.5})
        print(epochs, "Train accuracy", acc_train, "validation accuracy", acc_validation)
    save_path = saver.save(sess, "./my_model_find.ckpt")
with tf.Session() as sess:
    saver.restore(sess, "./my_model_find.ckpt")
    acc_testing = accuracy.eval(feed_dict = {x: images_DataTesting, y_:labels_DataTesting, rate:1})
    print("Testing Accuracy", acc_testing)<jupyter_output>Testing Accuracy 0.92
{'fraction_non_alphanumeric': 0.13797566658037794, 'fraction_numerical': 0.05617395806368108, 'mean_word_length': 5.7434554973821985, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '95381', 'license': 'Apache-2.0', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1501, 'n_words': 338, 'path': 'FaceRecog_TF.ipynb', 'repo_name': 'rarpit1994/Machine-Learning-and-Deep-Learning-models-using-Tensorflow-estimator-API', 'size': '3863'}