Your code doesn’t seem to raise this error, but the last line of code is failing because the shape of the numpy array to plt.imshow is wrong:
plt.imshow(output)
> TypeError: Invalid shape (16, 1, 28, 28) for image data

plt.imshow(output[0, 0]) # works

So I’m still unsure what raises the error.