<jupyter_start><jupyter_text>torch基础 1D张量的创建<jupyter_code>import torch
import numpy as np

# 从list转换
torch.tensor([1,2,3,4])
# 张量属性
t_1d = torch.tensor([1,2,3,4])
print(t_1d.dtype)
t_1d = torch.tensor([1,2,3,4], dtype=torch.float32)
print(t_1d.dtype)
# 更多创建方法
torch.tensor(range(10))
torch.tensor(np.array([1,2,3,4]),dtype=torch.float32)<jupyter_output><empty_output><jupyter_text>2D和多维张量创建<jupyter_code>torch.tensor([[1,2,3],[4,5,6]])
 torch.empty(5, 3) # 通过内置函数创建
torch.rand(3,3) #0-1的均匀分布
 torch.zeros(5, 3, dtype=torch.long)
torch.randn(3,4) # 0,1标准分布
torch.randint(0,5,(3,3),dtype=torch.float32)<jupyter_output><empty_output><jupyter_text>通过已知张量创建形状类似的张量<jupyter_code>t = torch.randn(3,3)
torch.zeros_like(t)
torch.randn_like(t)
torch.ones_like(t)<jupyter_output><empty_output><jupyter_text>与维度相关的张量方法<jupyter_code>t = torch.randn(3,4,5)
t.ndimension()
t.nelement()
t.size()
t.shape #属性
t.size(2)
t.view(12,5)
t.view(-1,6).shape
t.view(-1,6).transpose(1,0).shape<jupyter_output><empty_output><jupyter_text>索引和切片<jupyter_code>t[0,0,2]
t[:,1,1]
t>0
t[t>0] #.size()<jupyter_output><empty_output><jupyter_text>PyTorch张量的存储设备<jupyter_code>torch.randn(3,3,device='cpu')
torch.randn(3,3,device='cuda') #cuda:0
torch.randn(3,3).device
torch.randn(3,3).to('cuda:0')
torch.randn(3,3).cuda()<jupyter_output><empty_output><jupyter_text>单个张量的函数运算<jupyter_code>t = torch.rand(3,4)
t.sqrt()
t.sqrt_() #原地操作，不产生新张量
print(torch.sum(t))
print(torch.sum(t, axis=0))
t.sum()
t.mean(axis=0)
torch.argmax(t, axis=0) #按列求最大值位置
torch.argmin(t,axis=1)<jupyter_output><empty_output><jupyter_text>多个张量的函数运算两个形状相似的张量之间逐个元素的四则运算主要包括：add, sub,mul, div以及其内置操作add_等<jupyter_code>t1 = torch.rand(2,3)
t2 = torch.rand(2,3)
t1.add(t2)
t1+t2
t1.sub(t2)
t1*t2<jupyter_output><empty_output><jupyter_text>矩阵乘法<jupyter_code>a = torch.randn(3,4)
b = torch.randn(4,3)
a*b #不可以的?
torch.mm(a,b)
a@b
a = torch.randn(2,3,4)
b = torch.randn(2,4,3)
torch.bmm(a,b)<jupyter_output><empty_output><jupyter_text>张量的拼接和分割<jupyter_code>t1 = torch.rand(3,4)
t2 = torch.rand(3,4)
t3 = torch.rand(3,4)
t4 = torch.rand(3,2)
# stack函数将传入的张量列表堆叠起来，创建一个新维度张量
torch.stack([t1,t2,t3],axis=0).shape
# cat 对列表沿着指定维度堆叠返回
torch.cat([t1,t2,t3,t4],axis=1).shape
# squeeze unsqueeze
torch.unsqueeze(t1, dim=0).shape
torch.unsqueeze(t1, dim=0).squeeze(dim=0).shape<jupyter_output><empty_output><jupyter_text>OpenCV基础使用<jupyter_code>import cv2
img = cv2.imread('data/cat.jpg') # HxW BGR
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('cat-copy.jpg',img)<jupyter_output><empty_output><jupyter_text>OpenCV读取视频并显示<jupyter_code>cap = cv2.VideoCapture(0)
while(cap.isOpened()):
    ret, img = cap.read()
    cv2.imshow('image', img)
    if cv2.waitKey(10) & 0xFF == ord('q'):        
        break
cap.release()
cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>matplotlib的基本使用<jupyter_code>import torch
from  matplotlib import pyplot as plt
%matplotlib inline #jupyter里直接画图，否则需要plt.show()展示
t = torch.randn(100)
plt.plot(t) #plt.show()

img = cv2.imread('data/cat.jpg') # HxW BGR
plt.imshow(img[:,:,::-1])
plt.show()
from PIL import Image
pil_img = Image.open('data/cat.jpg')
plt.imshow(pil_img)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18818380743982493, 'fraction_numerical': 0.05064082525789309, 'mean_word_length': 8.667673716012084, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '167665', 'license': 'Apache-2.0', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1786, 'n_words': 157, 'path': 'code/lecture1-basic-torch.ipynb', 'repo_name': 'pengxj/DeepLearningCourse', 'size': '3199'}