Skip to content

Instantly share code, notes, and snippets.

@Hanrui-Wang
Created July 17, 2019 23:50
Show Gist options
  • Save Hanrui-Wang/4b5a8d997a806957b951bd230fc9aaa3 to your computer and use it in GitHub Desktop.
Save Hanrui-Wang/4b5a8d997a806957b951bd230fc9aaa3 to your computer and use it in GitHub Desktop.
difference between torch.Tensor and torch.from_numpy()
a = np.arange(10)
ft = torch.Tensor(a) # same as torch.FloatTensor
it = torch.from_numpy(a)
a.dtype # == dtype('int64')
ft.dtype # == torch.float32
it.dtype # == torch.int64
@Gaju27
Copy link

Gaju27 commented Feb 2, 2020

I see that the above code is a typo herein code. because when you not defined the dtype in np.arrange(10). default takes to int32.
hence the output is incorrectly written as below.

a.dtype # == dtype('int64')
ft.dtype # == torch.float32
it.dtype # == torch.int64

the correct output is:

a.dtype # == dtype('int32')
ft.dtype # == torch.int32
it.dtype # == torch.int32

Please correct me if I'm wrong.
please see below image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment