It’s strange! Can you share full stack trace error that shows which line is giving the error?
I ran your code for making sure about it and the proposed change should absolutely work. I guess your IDE/Kernel did not use changed file as you are having the exact same error which only can happen in that particular line given the weights and shapes.