この二つのコードの差は何ですか?どうして片方だけエラーが出るのかわかる人教えてください
両方 torch.Tensor
型同士の足し算なのに、なぜ片方だけエラーが出るのですか?
わかる人教えてください。
コードA
a=torch.tensor([[10]]).to("cuda:0").half()
b=torch.tensor([2]).to("cuda:0").half()
print(type(a),a)
print(type(b),b)
print(a+b)
print("ok")
結果A
<class 'torch.Tensor'> tensor([[10.]], device='cuda:0', dtype=torch.float16)
<class 'torch.Tensor'> tensor([2.], device='cuda:0', dtype=torch.float16)
tensor([[12.]], device='cuda:0', dtype=torch.float16)
ok
コードB
targ=(gamma ** multireward_steps)*targetQN.forward(memory.buffer[idx][0],"net_v")
rew=memory.buffer[idx][2].to("cuda:0")
print(type(targ),targ)
print(type(rew),rew)
targets[i]=rew+targ
結果B
<class 'torch.Tensor'> tensor([[0.0208]], device='cuda:0', dtype=torch.float16)
<class 'torch.Tensor'> tensor([0.], device='cuda:0', dtype=torch.float16)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-dff82183a33b> in <module>
420 trin.pioritized_experience_replay(batch_size, gamma,step=episode,
421 state_size=state_,action_size=acthon,
--> 422 multireward_steps=multireward_steps)
423 trin.Done(episode)
424 mainQN.Done()
<ipython-input-1-dff82183a33b> in pioritized_experience_replay(self, batch_size, gamma, step, state_size, action_size, multireward_steps)
289 print(type(targ),targ)
290 print(type(rew),rew)
--> 291 targets[i]=rew+targ
292
293 priority = rank_sum(memory_TDerror.buffer[idx], self.alpha)
~\Anaconda3\envs\pyflan\lib\site-packages\torch\tensor.py in __array__(self, dtype)
492 return self.numpy()
493 else:
--> 494 return self.numpy().astype(dtype, copy=False)
495
496 # Wrap Numpy array again in a suitable tensor when done, to support e.g.
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.