Solved by ptrblck in post #2 
              
              
                You are passing the target as a numpy array, which works differently for size() (it returns the number of elements as an int, thus raising this error). 
Remove the numpy() call and the code should work fine.