<jupyter_start><jupyter_text>数据操作<jupyter_code>import torch
torch.manual_seed(0)
torch.__version__<jupyter_output><empty_output><jupyter_text>2.2.1 创建Tensor> **创建一个5x3的未初始化的Tensor**- torch.empty(5, 3)- torch.rand(5, 3)- torch.zeros(5, 3)- torch.tensor([5, 3])- x.new_ones(5, 3, dtype=torch.float)  在现有的tensor上穿件新的tensor- torch.rand_like(x, dtype=torch.float)  根据x维度来创建随机tensor<jupyter_code>x = torch.empty(5, 3)
x
x = torch.rand(5, 3)
x
x = torch.zeros(5, 3, dtype=torch.long)
x
x = torch.tensor([5.5, 3])
x<jupyter_output><empty_output><jupyter_text>通过现有的tensor来创建<jupyter_code>x = x.new_ones(5, 3, dtype=torch.float64)
x
x = torch.rand_like(x, dtype=torch.float)
x
# 返回结果是tuple
print(x.shape)
print(x.size())<jupyter_output>torch.Size([5, 3])
torch.Size([5, 3])<jupyter_text>2.2.2 操作 算术操作> 1. **加法形式一: x + y**> 2. **加法形式二: torch.add(x, y)**> 3. **加法形式三: x.add_(y), 此时为 x = x + y**<jupyter_code>y = torch.rand(5, 3)
x + y
torch.add(x, y)
x.add_(y)<jupyter_output><empty_output><jupyter_text>索引操作> **torch的索引操作类似于numpy, 索引出来的结果与原数据共享内存**<jupyter_code>z = x[0, :]
z
z.add_(1)
z
x[0, :]<jupyter_output><empty_output><jupyter_text>改变形状> 1. **通过tensor.view()改变形状**> 2. **tensor.view()返回的新tensor与原tensor共享内存**> 3. **通过tensor.clone()创造一个副本来进行修改**> 4. **通过tensor.item()将一个标量tensor转化为python number**<jupyter_code>y = x.view(15)
y.shape
a = x.view(5, -1)
a.shape
x_cp = x.clone().view(15)
x_cp
x -= 1
x_cp
x<jupyter_output><empty_output><jupyter_text>2.2.3 广播机制> 1. **每个张量具有至少一个维度**> 2. **在遍历维度大小时, 从尾部维度开始遍历, 两者维度必须"相同", 此处"相同"三种含义: 两者相等;其中一个是1;其中一个不存在**<jupyter_code>x = torch.arange(1, 3).view(1, 2)
print(x)
y = torch.arange(1, 4).view(3, 1)
z = x+y
print(y)
print(z)
z - y<jupyter_output><empty_output><jupyter_text>2.2.4 运算的内存开销<jupyter_code>x = torch.tensor([1, 2])
y = torch.tensor([3, 4])
id_before = id(y)
y = y + x
id(y) == id_before
x = torch.tensor([1, 2])
y = torch.tensor([3, 4])
id_before = id(y)
y[:] = y + x
id(y) == id_before
x = torch.tensor([1, 2])
y = torch.tensor([3, 4])
id_before = id(y)
torch.add(x, y, out=y) # 等价于 y += x; y.add_(x)
id(y) == id_before<jupyter_output><empty_output><jupyter_text>2.2.5 Tensor和Numpy相互转换> 1. **tenfor -> numpy : tensor.numpy()**> 1. **numpy -> tenfor : torch.from_numpy()**> 1. **numpy()和from_numpy()产生的tensor和 numpy array使用的相同的内存**> 4. **torch.tensor()会进行数据拷贝,返回的tensor不会和原来的数据共享存**<jupyter_code>a = torch.ones(5)
b = a.numpy()
print(a)
print(b)
a += 1
print(a)
print(b)
b += 1
print(a)
print(b)
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
print(a, b)
a += 1
print(a, b)
b += 1
print(a, b)
c = torch.tensor(a)
c += 1
print(a, c)<jupyter_output>[3. 3. 3. 3. 3.]tensor([4., 4., 4., 4., 4.], dtype=torch.float64)<jupyter_text>2.2.6 Tensor On GPU<jupyter_code>if torch.cuda.is_available():
    device = torch.device('cuda')                 # GPU
    y = torch.ones_like(x, device=device)         # 创建一个在GPU上的tensor
    x = x.to(device)                              # 等价于 .to("cuda")
    z = x + y                                    
    z.to("cpu", torch.double)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19960668633235004, 'fraction_numerical': 0.037037037037037035, 'mean_word_length': 4.651851851851852, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '510974', 'license': 'MIT', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1624, 'n_words': 252, 'path': 'dl/dive-into-dl/chapter02-prerequisite/2.1_tensor.ipynb', 'repo_name': 'tangchunbo/machine-learning', 'size': '3051'}