Question
I have uploaded my answer here, but i wonder what I am doing wrong. Would you kindly look at my code here and explain what
I have uploaded my answer here, but i wonder what I am doing wrong. Would you kindly look at my code here and explain what I'm doing is wrong? I assume it got something to do with how i defined my confusion matrix. Also any fix for why the for loop is giving me an error?
question is Write code to calculate the maximum a posteriori (MAP) estimate of the model on the test data, and compare to the true labels to calculate a confusion matrix with tf.confusion_matrix. (For the inexperienced, what is a confusion matrix?)
(Hint #0: Re-use and modify the code from below. I have posted them after my answer)
(Hint #1: The MAP estimate is just the class whose probability is greatest. I reccomend using tf.argmax with the correct axis argument to find this to find the max over the correct dimension of the output.)
(Hint #2: tf.confusion_matrix is a function that needs be run in a session.run call that returns matrices. Store the resulting matrices in a list and then sum to get the matrix for the full test dataset. Remember to specify the num_classes argument.)
My answer:
the code to use for hints;
batch_size- 32 session-tf.Session() session.run(tf.global_variables_initializer()) conf-mat tf.confusion, matrix(labels, predictions, num classes-10, dtype=tf.int32, name-None, labels tf.argmax(y, axis-1, name None, dimension None, output type-tf.int32) predictions tf.argmax(output, ax15-1, name-None, dimension=None, output-type=tf.int32) ce val = np. zeros ((10,10), dtype-np.int32 weights=None) # train for one epoch for i in range(train_num_examples // batch_size) batch_xs -train_images [i*batch_size:(i+1)*batch_size, batch-ys train-labels[jtbatch-size: (1+1)*batch-size, session.run(train_op, (x: batch_xs, y: batch_ysj) :] :] # Loop through each test datum once for i in range(test_num_examples // batch_size): batch_xs -test_images[i*batch_size: (i+1)*batch_size, :] batch_ys-test_labels[i*batch_size: (i+1)*batch_size, :] ce_va1 += session.run(conf-mat, {x: batch-xs, y: batch-ys}) # now plot per-datum Losses loss_vals np.concatenate(loss_vals) hist, bin-edges = np.histogram(loss_vals pit . bar(bin-edges(:-1], hist, width = 1) plt.xlim(min(bin_edges), max(bin_edges) plt.show() File "Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started