Training the Capsule network

Set the number of epochs and number of steps:

num_epochs = 100
num_steps = int(len(mnist.train.images)/batch_size)

Now start the TensorFlow Session and perform training:

with tf.Session(graph=graph) as sess:

init_op = tf.global_variables_initializer()
sess.run(init_op)


for epoch in range(num_epochs):
for iteration in range(num_steps):
batch_data, batch_labels = mnist.train.next_batch(batch_size)
feed_dict = {x : batch_data, y : batch_labels}

_, loss, acc = sess.run([train_op, total_loss, accuracy], feed_dict=feed_dict)

if iteration%10 == 0:
print('Epoch: {}, iteration:{}, Loss:{} Accuracy: {}'.format(epoch,iteration,loss,acc))

You can see how the loss decreases over various iterations:

Epoch: 0, iteration:0, Loss:0.55281829834 Accuracy: 0.0399999991059
Epoch: 0, iteration:10, Loss:0.541650533676 Accuracy: 0.20000000298
Epoch: 0, iteration:20, Loss:0.233602654934 Accuracy: 0.40000007153

Thus, we have learned how Capsule networks work step by step, and how to build a Capsule network in TensorFlow.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset