创建tensor
 
import numpy as np
import torch
print(torch.__version__)
def creat_tensor():
    t1 = torch.tensor(7)
    print(t1)
    print(t1.shape)
    print(t1.dtype)
    print(type(t1))
    
    
def creat_tensor2():
    data = np.array([10,20,30,40])
    t1 = torch.tensor(data)
    print(t1.shape)
    print(t1.dtype)
    print(type(t1))
    
def creat_tensor3():
    data = [10,20,30,40]
    t1 = torch.tensor(data)
    print(t1.shape)
    print(t1.dtype)
    print(type(t1))
    
def creat_tensor4():
    t = torch.Tensor(3,4)
    print(t)
    print(t.shape)
    
def creat_tensor5():
    t = torch.Tensor([[10,20,30],[10,20,30]])
    print(t.shape)
    print(t.device)
    
def creat_tensor6():
    t = torch.IntTensor([10,20,30])
    
    
    
    
    
    
    print(t)
    print(t.shape)
    print(t.dtype)
    print(t.device)
    
if __name__ == '__main__':
    creat_tensor4()
 
import torch
import numpy as np
data = np.array([[1,2,3],[12,34,56]])
data = torch.tensor(data)
x = torch.ones_like(data)
print(x)
 
import torch
a = torch.Tensor([[1, 2], [3, 4]])
print(a)
print(a.dtype)  
b = torch.tensor([[1, 2], [3, 4]]).type(torch.float64)
print(b)
print(b.dtype)  
x = torch.tensor([1,2,3],dtype=torch.float32)
print(x)
x = torch.Tensor([1,2,3]).type(torch.int32)
print(x)
x = torch.Tensor([1,2,3]).to('cuda')
print(x.device)
 
创建线性和随机张量
 
import torch
def test01():
    x = torch.arange(1, 10, 2)  
    print(x)
    y = torch.linspace(1, 10, 5)  
    print(y)
    z = torch.logspace(1, 10, 3, base=2)  
    print(z, z.dtype)
def test02():
    
    torch.manual_seed(666)
    
    r = torch.initial_seed()
    print(r)
    
    x = torch.rand(10,5)
    print(x)
    
    y = torch.randn(3,3)
    print(y)
    
    z = torch.normal(2,1,(4,4))
    print(z)
if __name__ == '__main__':
    test01()
 
创建01张量
 
import numpy as np
import torch
def test01():
    x = torch.zeros(3, 3)
    print(x)
    y = torch.tensor([[10, 20, 30],
                      [22, 33, 44]])
    y = torch.rand(3, 2)
    
    
    
    
    y = torch.tensor(np.array([1, 2, 3]))
    y = torch.zeros_like(y)
    print(y)
def test02():
    x = torch.ones(3,5)
    print(x)
    
    y = torch.tensor([1,2,3])
    y = torch.rand(3, 2)
    y = torch.ones_like(y)
    print(y)
    
def test03():
    x = torch.full((3,4),999)
    print(x)
    
    y = torch.full_like(x,2)
    print(y)
    
def test04():
    x = torch.eye(4)
    print(x)
if __name__ == '__main__':
    test04()
 
常见的属性和类型转换
 
import torch
from numpy import dtype
def test01():
    
    x = torch.tensor([1, 2, 3], device='cuda')  
    print(x, x.dtype)  
    print(x.device)  
    
    
    
    
    print(x.shape)  
 
def test02():
    
    
    
    
    
    
    
    
    x = torch.tensor([1, 2, 3], device='cuda')
    print(1, x)
    
    y = torch.tensor([4, 5, 6])
    z = y.to('cuda:0')  
    print(2, z.device)
    
    res = torch.cuda.is_available()
    print(res)  
    c = 1 if 100 > 10 else 0
    print(c)
    x.to('cuda' if torch.cuda.is_available() else 'cpu')
    print(x.device)
    
    
    x = torch.tensor([123, 123])
    print(x.device)
    x = x.cuda()  
    print(x.device)
    x = x.cuda() if torch.cuda.is_available() else x
def test03():
    
    x = torch.tensor([10,20,30,40],dtype=torch.float16)
    print(x)
    
    
    x = x.type(torch.int8)
    print(x.dtype)
    
    
    x = x.half()
    print(x.dtype)
    x = x.double()
    print(x.dtype)
    x = x.float()
    print(x.dtype)    
    x = x.long()
    print(x.dtype)
    x = x.int()
    print(x.dtype)
    
if __name__ == '__main__':
    test03()
 
数据转换
 
import numpy as np
import torch
def test01():
    x = torch.tensor([1, 2, 3])
    print(x)
    
    x2 = x.numpy()
    print(x2)
    print(type(x2))
    
    x2[0] = 100
    print('x2',x2)
    print('x',x)
def test02():
    print('--------------')
    x = torch.tensor([1, 2, 3])
    x3 = x.numpy()
    x2 = x3.copy()  
    print(x2)
    x2[0] = 100
    print(x)
    print(x2)
    print(x3)
    
    
def test03():
    print('-----------------')
    
    x = np.array([1,2,3])
    x2 = torch.tensor(x)
    x[0]=122
    x2[1]=999
    print(x)
    print(x2)
    
    
    x3 = np.array([2,3,4])
    x4 = torch.from_numpy(x3)
    x3[0] = 111
    x4[1] =7678
    print(x3)
    print(x4)
    
if __name__ == '__main__':
    test01()
    test02()
    test03()
 
图像转张量
 
import cv2
import torch
from PIL import Image
from torchvision import transforms
def test01():    
    img = cv2.imread('data/1.png')
    print(img)
    print(type(img))
    img = torch.from_numpy(img)
    print(img)
    
    
def test02():
    
    path = r'./data/1.png'
    img = Image.open(path)
    print(img)
    transfer = transforms.ToTensor()
    img_tensor = transfer(img)
    print(img_tensor)
    print(img_tensor.shape)
    '''
    [[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]],
     [[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]],
     [[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]],
     [[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]]
    
    4,5,6
    '''
    print(img_tensor[0])
    
def test03():
    
    
    
    
    
    img_tensor = torch.rand(4,315,315)
    print(img_tensor)
    print(img_tensor.shape)
    
    trans = transforms.ToPILImage()
    img_pil = trans(img_tensor)
    img_pil.show()
    
    
def test04():
    path = r'./data/1.png'
    img = Image.open(path)
    print(img)
    transfer = transforms.ToTensor()
    img_tensor = transfer(img)
    img_tensor[:,10,10]=255
    tensor2pil = transforms.ToPILImage()
    img_pil = tensor2pil(img_tensor)
    img_pil.show()
    
    
if __name__ == '__main__':
    test03()