As the error message suggests, you would have to push the tensor to the CPU first before converting it to a numpy array via tensor.cpu().
In particular np.array(targets.argmax(1)) seems to raise the error to use:
targets = targets.argmax(1).cpu().numpy()

instead.
PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.