能免费做网站,平面设计师网上接单,建设网站修改图片,旅游网站专业化建设的要点pytorch中张量的有关操作 创建张量torch.tensor(data): 从数据创建张量torch.zeros(size): 创建元素全为0的张量torch.ones(size): 创建元素全为1的张量torch.empty(size): 创建未初始化的张量torch.randn(size): 创建服从标准正态分布的张量torch.arange(start, end, step): 创… pytorch中张量的有关操作 创建张量torch.tensor(data): 从数据创建张量torch.zeros(size): 创建元素全为0的张量torch.ones(size): 创建元素全为1的张量torch.empty(size): 创建未初始化的张量torch.randn(size): 创建服从标准正态分布的张量torch.arange(start, end, step): 创建一个范围内的一维张量torch.linspace(start, end, steps): 创建一个在指定范围内均匀间隔的张量 张量属性相关.dtype: 获取张量的数据类型.shape: 获取张量的形状.device: 获取张量所在的设备 张量索引、切片与拼接tensor[index]: 索引操作使用切片来获取张量的子张量沿着指定维度将多个张量连接在一起在一个新的维度上堆叠多个张量 张量变换tensor.view(shape): 返回给定形状的张量视图返回一个具有指定形状的新张量原始张量的元素数量必须与新形状一致交换张量中两个维度的位置按照给定顺序重新排列张量的维度删除张量中所有长度为1的维度在指定位置增加一个长度为1的新维度 创建张量
torch.tensor(data): 从数据创建张量
这个函数会根据提供的数据创建一个新的张量。数据可以是列表、数组等。
import torchdata [1, 2, 3, 4, 5]
tensor_data torch.tensor(data)
print(tensor_data)torch.zeros(size): 创建元素全为0的张量
创建一个指定大小的张量其中所有元素的值都为0。
import torchsize (2, 3)
zeros_tensor torch.zeros(size)
print(zeros_tensor)torch.ones(size): 创建元素全为1的张量
创建一个指定大小的张量其中所有元素的值都为1。
import torchsize (2, 3)
ones_tensor torch.ones(size)
print(ones_tensor)torch.empty(size): 创建未初始化的张量
创建一个指定大小的未初始化张量其值取决于内存的状态。
import torchsize (2, 3)
empty_tensor torch.empty(size)
print(empty_tensor)torch.randn(size): 创建服从标准正态分布的张量
创建一个指定大小的张量其中的元素值是从标准正态分布中随机抽取的。
import torchsize (2, 3)
randn_tensor torch.randn(size)
print(randn_tensor)torch.arange(start, end, step): 创建一个范围内的一维张量
创建一个一维张量其中的元素值从起始值到结束值步长为给定的步长。
import torchstart 0
end 5
step 1
arange_tensor torch.arange(start, end, step)
print(arange_tensor)torch.linspace(start, end, steps): 创建一个在指定范围内均匀间隔的张量
创建一个一维张量其中的元素值在指定范围内均匀分布。
import torchstart 0
end 5
steps 5
linspace_tensor torch.linspace(start, end, steps)
print(linspace_tensor)张量属性相关
.dtype: 获取张量的数据类型
返回张量中元素的数据类型。
import torchtensor torch.tensor([1, 2, 3])
print(tensor.dtype).shape: 获取张量的形状
返回一个元组表示张量的形状。
import torchtensor torch.tensor([[1, 2, 3], [4, 5, 6]])
print(tensor.shape).device: 获取张量所在的设备
返回一个字符串表示张量所在的设备如’cpu’或’cuda:0’。
import torchtensor torch.tensor([1, 2, 3])
print(tensor.device)张量索引、切片与拼接
tensor[index]: 索引操作
使用索引来访问张量中的元素。
import torchtensor torch.tensor([[1, 2, 3], [4, 5, 6]])
element tensor[0, 1] # Accesses the element at row 0, column 1
print(element)
tensor[start:end]: 切片操作使用切片来获取张量的子张量
import torchtensor torch.tensor([[1, 2, 3], [4, 5, 6]])
sub_tensor tensor[:, 1:] # Slices the tensor to get all rows and columns starting from the second column
print(sub_tensor)
torch.cat(tensors, dim): 在给定维度上连接张量沿着指定维度将多个张量连接在一起
import torchtensor1 torch.tensor([[1, 2], [3, 4]])
tensor2 torch.tensor([[5, 6], [7, 8]])
concatenated_tensor torch.cat((tensor1, tensor2), dim0) # Concatenates along the row dimension
print(concatenated_tensor)
torch.stack(tensors, dim): 在新维度上堆叠张量在一个新的维度上堆叠多个张量
import torchtensor1 torch.tensor([1, 2, 3])
tensor2 torch.tensor([4, 5, 6])
stacked_tensor torch.stack((tensor1, tensor2), dim1) # Stacks tensors along a new dimension
print(stacked_tensor)张量变换
tensor.view(shape): 返回给定形状的张量视图
返回一个具有指定形状的新张量原始张量的形状必须与新形状兼容。
import torchtensor torch.tensor([[1, 2], [3, 4]])
reshaped_tensor tensor.view(1, 4) # Reshapes the tensor to a 1x4 tensor
print(reshaped_tensor)
tensor.reshape(shape): 改变张量的形状返回一个具有指定形状的新张量原始张量的元素数量必须与新形状一致
import torchtensor torch.tensor([[1, 2], [3, 4]])
reshaped_tensor tensor.reshape(1, 4) # Reshapes the tensor to a 1x4 tensor
print(reshaped_tensor)
tensor.transpose(dim0, dim1): 交换两个维度交换张量中两个维度的位置
import torchtensor torch.tensor([[1, 2], [3, 4]])
transposed_tensor tensor.transpose(0, 1) # Swaps the first and second dimensions
print(transposed_tensor)
tensor.permute(*dims): 按照指定顺序排列张量的维度按照给定顺序重新排列张量的维度
import torchtensor torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
permuted_tensor tensor.permute(1, 0, 2) # Permutes the dimensions to (1, 0, 2)
print(permuted_tensor)
tensor.squeeze(): 删除所有长度为1的维度
删除张量中所有长度为1的维度
import torchtensor torch.tensor([[[1, 2], [3, 4]]])
squeezed_tensor tensor.squeeze() # Removes the single-dimensional entries
print(squeezed_tensor)
tensor.unsqueeze(dim): 在指定位置增加一个维度在指定位置增加一个长度为1的新维度
import torchtensor torch.tensor([[1, 2], [3, 4]])
unsqueezed_tensor tensor.unsqueeze(0) # Adds a dimension at index 0
print(unsqueezed_tensor)