Now we compute the accuracy of our model:
with graph.as_default() as g:
with tf.variable_scope('accuracy'):
Compute the length of each activity vector in the digit capsule:
v_length = tf.sqrt(tf.reduce_sum(tf.square(digit_capsules), axis=2, keep_dims=True) + epsilon)
Apply softmax to the length and get the probabilities:
softmax_v = tf.nn.softmax(v_length, dim=1)
Select the index that had the highest probability; this will give us the predicted digit:
argmax_idx = tf.to_int32(tf.argmax(softmax_v, axis=1))
predicted_digit = tf.reshape(argmax_idx, shape=(batch_size, ))
Compute the accuracy:
actual_digit = tf.to_int32(tf.argmax(y, axis=1))
correct_pred = tf.equal(predicted_digit,actual_digit)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))