您现在的位置是:首页 >技术杂谈 >pytorch使用网站首页技术杂谈

pytorch使用

勤劳的小dong 2026-03-02 00:01:03
简介pytorch使用

pytorch使用记录

1.cat使用(拼接# concatenate )

import torch

# 创建一些张量
tensor1 = torch.randn(4)  # 3行4列的随机张量
tensor2 = torch.randn(6)  # 3行4列的随机张量

print("tensor1",tensor1)
print("tensor2",tensor2)


# 1. 沿着第 0 维(行)拼接
concat_dim0 = torch.cat((tensor1, tensor2), dim=0)
print("拼接结果 (dim=0):
", concat_dim0)
print("结果形状:", concat_dim0.shape)
# /home/zyd/miniconda3/envs/dxtorch/bin/python /home/zyd/Multi_Robot_A3C/learn/learn_cat.py 
# tensor1 tensor([-0.1107,  0.8175, -0.1032, -2.1046])
# tensor2 tensor([-1.5329,  1.6610, -1.5647, -0.1882, -1.2715,  0.7370])

# 拼接结果 (dim=0):
#  tensor([-0.1107,  0.8175, -0.1032, -2.1046, -1.5329,  1.6610, -1.5647, -0.1882,
#         -1.2715,  0.7370])
# 结果形状: torch.Size([10])

2.Sequential(多个网络进行组合)

组成网络列表
def encoder(input_dimension,output_dimension):
    l1 = nn.Linear(input_dimension,output_dimension)
    l2 = nn.ReLU()
    model = nn.Sequential(l1, l2)
    return model

3.pytorch的大小显示

维度一切括号为准【】一维,【【】,【】】二维。

x = torch.tensor([2.0, 3.0], requires_grad=True)
print(x.size())  #torch.Size([2])
x = torch.tensor([[2.0, 3.0],
                [1.0, 2.0]])
print(x.size()) #torch.Size([2, 2])

然后统计横竖

3.1unsqueeze(增加维度:)

import torch
# 创建一个形状为 (3, 4) 的张量
tensor = torch.randn(3, 4)
print("原始张量:", tensor)
print("原始形状:", tensor.shape)

# 在第 0 维增加一个维度
tensor_unsqueezed_0 = tensor.unsqueeze(0)
print("在第 0 维增加维度后的张量:", tensor_unsqueezed_0)
print("新的形状:", tensor_unsqueezed_0.shape)

# 在第 1 维增加一个维度
tensor_unsqueezed_0 = tensor.unsqueeze(1)
print("在第 0 维增加维度后的张量:", tensor_unsqueezed_0)
print("新的形状:", tensor_unsqueezed_0.shape)
# /home/zyd/miniconda3/envs/pyg0.25.2/bin/python3 /code/zyd/Highway-Env-DQN/learn.py
# 原始张量: tensor([[-1.8370, -0.2652,  1.0194,  0.5157],
#         [ 1.1720, -0.5756, -0.4748,  0.2088],
#         [ 1.0068, -1.2134,  0.6083, -1.6266]])
# 原始形状: torch.Size([3, 4])
# 在第 0 维增加维度后的张量: tensor([[[-1.8370, -0.2652,  1.0194,  0.5157],
#          [ 1.1720, -0.5756, -0.4748,  0.2088],
#          [ 1.0068, -1.2134,  0.6083, -1.6266]]])
# 新的形状: torch.Size([1, 3, 4])

#在第 0 维增加维度后的张量: tensor([[[-0.0126,  0.1856, -0.9308,  0.0518]],
#        [[ 2.1489, -0.1034, -0.3129,  1.0605]],
#        [[-0.9770, -0.7828,  0.1455, -0.2740]]])
#新的形状: torch.Size([3, 1, 4])
3维是批次(找最外层括号几个[[)  2维是片(找最外层括号几个[)   1维是条(直接找数)

4.gather用法(dim参数含义是维度)

gather对dim=0按列  dim=1按行

import torch

# 假设我们有一个评估网络的输出矩阵,形状为 (batch_size, action_space_size)
# 这里我们创建一个随机的输出矩阵作为示例

eval_output = torch.randn(4, 3)

# 假设我们有一批动作索引,形状为 (batch_size, 1)
# 这里我们创建一个随机的动作索引矩阵作为示例
b_action = torch.randint(0, 3, (4, 1))

print("评估网络输出矩阵:
", eval_output)
print("动作索引矩阵:
", b_action)

# 使用 gather 从评估网络的输出中选择对应的动作值
q_eval = eval_output.gather(1, b_action)  ##1按行

print("选择的动作值:
", q_eval)
# 评估网络输出矩阵:
#  tensor([[-0.8939, -1.0939, -1.4746],
#         [ 0.0275,  0.4221, -1.0197],
#         [-0.4691, -0.3397, -0.1507],
#         [-1.2586, -0.1752, -0.2498]])
# 动作索引矩阵:
#  tensor([[0],
#         [2],
#         [2],
#         [1]])
# 选择的动作值:
#  tensor([[-0.8939],
#         [-1.0197],
#         [-0.1507],
#         [-0.1752]])

5.max方法(第一个重点 )

3为数据,dim=0,肯定取数去对比,
dim分解为2维,dim=1按列
dim为解为2维,dim=2按行

对比二维数据 0按列,1按行
import torch

# 假设我们有一个评估网络的输出矩阵,形状为 (batch_size, action_space_size)
batch_size = 4
action_space_size = 3
q_eval_max_a = torch.randn(batch_size, action_space_size)

print("评估网络输出矩阵:
", q_eval_max_a)

# 使用 max(1) 找到每个样本的最大动作值及其索引
max_values, max_indices = q_eval_max_a.max(1)

print("最大动作值:
", max_values)
print("最大动作值对应的索引:
", max_indices)


评估网络输出矩阵:
 tensor([[ 0.2201,  0.4031, -1.3806],
        [ 1.2398, -0.2451, -1.7606],
        [ 0.7916, -1.4379,  0.2541],
        [ 0.3865, -0.3608, -0.3701]])
最大动作值:
 tensor([0.4031, 1.2398, 0.7916, 0.3865])
最大动作值对应的索引:
 tensor([1, 0, 0, 0])

进程已结束,退出代码为 0

6.view

.view() 的作用
改变张量的形状:可以将一个张量从一种形状转换为另一种形状。
保持数据不变:.view() 不会改变张量的数据,只是重新组织数据的维度。
自动推断维度:如果某个维度的大小未知,可以用 -1 代替,PyTorch 会自动计算该维度的大小


风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。