[딥러닝] 파이터치 학습(一)-torch tensor

1351 단어

[딥러닝] 파이터치 학습(一)-torch tensor


학습 노트.기록 공유.


학습한 코드 환경:python3.6 torch1.3 vscode+jupyter 확장
#%%
import torch
print(torch.__version__)
#   CUDA GPU    
a = torch.cuda.is_available()
print(a)

#%%
# torch.randperm
x = torch.randperm(6)
print(x)

#%%
# torch.view 
x = x.view(2,3)     #    numpy reshape
print(x)

#%%
# tensor.numpy()
#      from_numpy,tensor numpy       
a = torch.ones(2,2)
print(a)
b = a.numpy()
print(b)

#%%
# torch.from_numpy
import numpy as np
a = np.ones(3)
print(a)
b = torch.from_numpy(a)
print(b)

#%%
# troch.cat
xx = torch.cat((x,x),0)
xxx = torch.cat((x,x),1)
print(xx)
print(xxx)


#%%
# torch.tensor
scalar = torch.tensor(3.1415926)
print(scalar)
print(scalar.shape)

#                  ,  tensor.item  
scalar.item()
# xx.item()     #      
# ValueError: only one element tensors can be converted to Python scalars


#%%
f = x.float()
print(f)
print(f.dtype)
b = torch.HalfTensor(2,3)
b

#%%
# .cuda()
cpu_a = torch.rand(4,3)
print(cpu_a.type())
gpu_a = cpu_a.cuda()
print(gpu_a.type())
a = gpu_a.cpu()
print(a.type())


#%% 
#  numpy     
# eg: max randn ...          dim=
print(a)
m = torch.max(a,dim=1)
print(m[0])
print(m[1])


#%%
#  _    ,       
aa = torch.ones(3,4)
bb = torch.eye(3,4)
aa.add_(bb)
print(aa)

좋은 웹페이지 즐겨찾기