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
####################################
# DO NOT MODIFY THE DECODEE PART BELOW BUT PROVIDE A SOLUTION BY FIXING THE CLASSES ABOVE LIKE SELFATTENTION ETCETERA#
####################################
def decodemodel: Transformer, devexamples: ListLetterCountingExample doprintFalse, doplotattnFalse:
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 devexamples: the list of LetterCountingExample
:param doprint: True if you want to print the inputgoldpredictions for the examples, false otherwise
:param doplotattn: True if you want to write out plots for each example, false otherwise
:return:
numcorrect
numtotal
if lendevexamples:
printDecoding on a large number of examples i; not printing or plotting" lendevexamples
doprint False
doplotattn False
for i in range lendevexamples:
ex devexamplesi
logprobs, attnmaps model.forwardexinputtensor
predictions npargmaxlogprobs.detachnumpy axis
if doprint:
printINPUT i: si exinput
printGOLD i: si reprexoutput.astypedtypeint
printPRED i: si reprpredictions
if doplotattn:
for j in range lenattnmaps:
attnmap attnmapsj
fig, ax pltsubplots
im aximshowattnmap.detachnumpy cmap'hot', interpolation'nearest'
axsetxticksnparangelenexinput labelsexinput
axsetyticksnparangelenexinput labelsexinput
axxaxis.ticktop
# pltshow
pltsavefigplotsiattnsipngi j
acc sumpredictionsi exoutputi for i in range lenpredictions
numcorrect acc
numtotal lenpredictions
printAccuracy: i i fnumcorrect, numtotal, floatnumcorrect numtotal
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
