PyTorch语法

张量的创建

import torch
a=[1,2,3.]
print(type(a))

b=torch.tensor(a)
print(b)
print(type(b))
print(b.dtype)

import numpy as np
c=np.random.normal((2,3))
d=torch.tensor(c)
print(d)

e=torch.ones_like(d)
print(e)
f=torch.zeros_like(d)
print(f)
g=torch.rand_like(d)
print(g)

print(torch.rand((2,2)))
print(torch.randn([2,2]))
print(torch.rand([2,2,]).dtype)

h=torch.rand([2,2,])
print(h.dtype)
print(h.shape)
print(h.device)
print(torch.is_tensor(h))

i=torch.tensor(0)
print(torch.is_nonzero(i))
print('-'*34)
print(torch.numel(h))
print(torch.zeros([5,5]))
a=torch.zeros([5,5],dtype=torch.int32)
print(a)
print(torch.zeros([5,5]).dtype)
print(torch.ones_like(a))
print('-'*34)
print(torch.arange(5))
print(torch.arange(0,5,2))
print(torch.range(0,5))
print(torch.range(0,2).dtype)
print(torch.arange(0,5).dtype)
for i in torch.arange(5):
    print('epoch:',i)
print(torch.eye(3))
print(torch.ones_like(a)*5)
print(torch.full([2,2],2))
print(torch.full_like(a,2))

a=torch.rand([3,2,])
print(a)
b=torch.rand([3,2,])
print(b)
print(torch.cat([a,b],dim=0))

张量的运算API(1)

import torch
b=torch.rand([3,2])
print(b)
c,d=torch.chunk(b,chunks=2,dim=1)
print(c)
print(d)
print(torch.reshape(torch.reshape(b,[2,3]),[-1]))
print('-'*34)
src = torch.tensor([[1,2],
                    [3,4],
                    [5,6]])

index = torch.tensor([[0,2],
                      [1,0],
                      [2,1]])

'''out = torch.zeros_like(src)
out.scatter_(dim=0, index=index, src=src)
#src [i][j] 这个数,要搬到 out 的 第 index [i][j] 行、第 j 列
print(out)
'''
a=torch.arange(10).reshape(5,2)
#print(a)
#print(torch.split(a,[1,4]))

'''
print(a.shape)
print(torch.squeeze(torch.reshape(a,[1,1,5,2]),dim=0).shape)
'''
b=torch.rand(5,2)
print(torch.stack([a,b],dim=0).shape)    #torch.Size([2, 5, 2])
print(torch.stack([a,b],dim=1).shape)    #torch.Size([5, 2, 2])
print(torch.cat([a,b],dim=0).shape)      #torch.Size([10, 2])
print(torch.cat([a,b],dim=1).shape)     #torch.Size([5, 4])

张量的运算API(2)

import torch
a=torch.rand([3,2])
print(a)
#print(torch.take(a,torch.tensor([0,2,4])))
'''
print(torch.tile(a,dims=[1,2]))
print(torch.tile(a,dims=[2,1]))
'''
'''
print(torch.transpose(a,0,1))
print('-'*34)
print(torch.unbind(a,dim=0))
print(torch.unbind(a,dim=1))
print('-'*34)
print(torch.unsqueeze(a,dim=0).shape)
print(torch.unsqueeze(a,dim=1).shape)
print(torch.unsqueeze(a,dim=-1).shape)
'''
b=torch.zeros_like(a)
print(torch.where(a>0.5,a,b))

dataset的基本代码实现

from torch.utils.data import Dataset
from PIL import Image
import os
class MyDataset(Dataset):
    def __init__(self,root_dir,label_dir):
        self.root_dir=root_dir
        self.label_dir=label_dir
        self.path=os.path.join(self.root_dir,self.label_dir)
        self.img_path=os.listdir(self.path)


    def __getitem__(self,index):
       img_name=self.img_path[index]
       img_item_path=os.path.join(self.root_dir,self.label_dir,img_name)
       img=Image.open(img_item_path)
       label=self.label_dir
       return img,label

    def __len__(self):
        return len(self.img_path)

root_dir='dataset'
ants_label_dir='ants'
bees_label_dir='bees'
ants_dataset=MyDataset(root_dir,ants_label_dir)
bees_dataset=MyDataset(root_dir,bees_label_dir)

dataset=ants_dataset+bees_dataset
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐