Question: ValueError: operands could not be broadcast together with shapes ( 1 , 3 ) ( 2 0 , ) #################################### # DO NOT MODIFY THE

ValueError: operands could not be broadcast together with shapes (1,3)(20,)
####################################
# DO NOT MODIFY THE DECODEE PART BELOW BUT PROVIDE A SOLUTION BY FIXING THE CLASSES ABOVE LIKE SELFATTENTION ETCETERA#
####################################
def decode(model: Transformer, dev_examples: List[LetterCountingExample], do_print=False, do_plot_attn=False):
"""
Decodes the given dataset, does plotting and printing of examples, and prints the final accuracy.
:param model: your Transformer that returns log probabilities at each position in the input
:param dev_examples: the list of LetterCountingExample
:param do_print: True if you want to print the input/gold/predictions for the examples, false otherwise
:param do_plot_attn: True if you want to write out plots for each example, false otherwise
:return:
"""
num_correct =0
num_total =0
if len(dev_examples)>100:
print("Decoding on a large number of examples (%i); not printing or plotting" % len(dev_examples))
do_print = False
do_plot_attn = False
for i in range(0, len(dev_examples)):
ex = dev_examples[i]
(log_probs, attn_maps)= model.forward(ex.input_tensor)
predictions = np.argmax(log_probs.detach().numpy(), axis=1)
if do_print:
print("INPUT %i: %s"%(i, ex.input))
print("GOLD %i: %s"%(i, repr(ex.output.astype(dtype=int))))
print("PRED %i: %s"%(i, repr(predictions)))
if do_plot_attn:
for j in range(0, len(attn_maps)):
attn_map = attn_maps[j]
fig, ax = plt.subplots()
im = ax.imshow(attn_map.detach().numpy(), cmap='hot', interpolation='nearest')
ax.set_xticks(np.arange(len(ex.input)), labels=ex.input)
ax.set_yticks(np.arange(len(ex.input)), labels=ex.input)
ax.xaxis.tick_top()
# plt.show()
plt.savefig("plots/%i_attns%i.png"%(i, j))
acc = sum([predictions[i]== ex.output[i] for i in range(0, len(predictions))])
num_correct += acc
num_total += len(predictions)
print("Accuracy: %i /%i =%f"%(num_correct, num_total, float(num_correct)/ num_total))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!