Python view()和repeat()函数的用法

前言:今天在看了一套代码,里面多次用到view和repeat函数,暂且记录一下这俩函数的用法吧。

1. view函数

在pytorch中,view函数可用于重构tensor张量的维度,类似于array数组的resize操作,但用法上存在一定的区别。

>>> import torch
>>> x = torch.randn(2,3)  # 随机生成2行3列的数组
tensor([[-0.5826,  0.2356,  1.5760],
        [ 0.7678,  0.3738, -0.1174]])
>>> x.shape
torch.Size([2, 3])

>>> y = torch.ones(4)     # 生成长度为4的全1数组
tensor([1., 1., 1., 1.])
>>> y.shape
torch.Size([4])

1.1 Tensor.view(rows, cols,...)

含义:将Tensor张量冲构成rows * cols的张量形式(此处Tensor表示需要重构的任一张量)

>>> xx1 = x.view(3,2)
>>> xx1
tensor([[-0.5826,  0.2356],
        [ 1.5760,  0.7678],
        [ 0.3738, -0.1174]])
>>> xx1.shape
torch.Size([3, 2])

>>> yy1 = y.view(2,2)
>>> yy1
tensor([[1., 1.],
        [1., 1.]])
>>> yy1.shape
torch.Size([2, 2])

1.2 Tensor.view(-1)或Tensor.view(-1, 1)

含义:当参数中存在-1时,表示该位置的数值需要经过计算(参数列表里只能存在至多一个-1)

>>> xx2 = x.view(-1,1)
>>> xx2
tensor([[-0.5826],
        [ 0.2356],
        [ 1.5760],
        [ 0.7678],
        [ 0.3738],
        [-0.1174]])
>>> xx2.shape
torch.Size([6, 1])  

>>> yy2 = y.view(1, -1)
>>> yy2
tensor([[1., 1., 1., 1.]])
>>> yy2.shape
torch.Size([1, 4])

2. repeat函数

Tensor.repeat(*size): 用于在指定维度对张量进行复制操作

参数 sizes (torch.Size or int...) 为沿着各维度复制的次数。注:size参数的个数不能少于Tensor的维度

2.1 一维向量的复制

当size参数只有一个时,即 Tensor.repeat(num),表示在Tensor的列方向上复制num倍;

当size参数有两个时,Tensor.repeat(num_row, num_col),第一个num_row表示行方向的复制倍数,第二个num_col表示列方向的复制倍数;

当size参数有三个时,Tensor.repeat(num_channel, num_row, num_col),第一个num_channel表示通道数的复制倍数,num_row表示行方向的复制倍数,第二个num_col表示列方向的复制倍数。

>>> x = torch.tensor([1, 2, 3])  
>>> x.repeat(3)
tensor([1, 2, 3, 1, 2, 3, 1, 2, 3])   # 在列方向上复制三倍

>>> x.repeat(4, 2)
tensor([[ 1,  2,  3,  1,  2,  3],
        [ 1,  2,  3,  1,  2,  3],
        [ 1,  2,  3,  1,  2,  3],
        [ 1,  2,  3,  1,  2,  3]])    # 在行方向上复制4倍,列方向上复制2倍

>>> x.repeat(4, 2, 2) 
tensor([[[1, 2, 3, 1, 2, 3],
         [1, 2, 3, 1, 2, 3]],
        [[1, 2, 3, 1, 2, 3],
         [1, 2, 3, 1, 2, 3]],
        [[1, 2, 3, 1, 2, 3],
         [1, 2, 3, 1, 2, 3]],
        [[1, 2, 3, 1, 2, 3],
         [1, 2, 3, 1, 2, 3]]])        #在通道方向复制4倍,行列方向各复制2倍

>>> x.repeat(4, 2, 2).size()
torch.Size([4, 2, 6])

2.2 二维向量的复制

与一维向量相似,Tensor.repeat(num_row, num_col)表示在行方向上复制num_row倍,在列方向上复制num_col倍;

Tensor.repeat(num_channel, num_row, num_col),表示在通道方向上复制num_channel倍,在行方向上复制num_row倍,在列方向上复制num_col倍。

>>> y = torch.rand(3, 2)
tensor([[0.0027, 0.6878],
        [0.5797, 0.0492],
        [0.8220, 0.5905]])

>>> y.repeat(3,2)       # 在行方向复制3倍,在列方向复制2倍
tensor([[0.0027, 0.6878, 0.0027, 0.6878],
        [0.5797, 0.0492, 0.5797, 0.0492],
        [0.8220, 0.5905, 0.8220, 0.5905],
        [0.0027, 0.6878, 0.0027, 0.6878],
        [0.5797, 0.0492, 0.5797, 0.0492],
        [0.8220, 0.5905, 0.8220, 0.5905],
        [0.0027, 0.6878, 0.0027, 0.6878],
        [0.5797, 0.0492, 0.5797, 0.0492],
        [0.8220, 0.5905, 0.8220, 0.5905]])


>>> y.repeat(2,3,2)  # 在通道方向复制两倍,在行方向复制3倍,在列方向复制2倍
tensor([[[0.0027, 0.6878, 0.0027, 0.6878],
         [0.5797, 0.0492, 0.5797, 0.0492],
         [0.8220, 0.5905, 0.8220, 0.5905],
         [0.0027, 0.6878, 0.0027, 0.6878],
         [0.5797, 0.0492, 0.5797, 0.0492],
         [0.8220, 0.5905, 0.8220, 0.5905],
         [0.0027, 0.6878, 0.0027, 0.6878],
         [0.5797, 0.0492, 0.5797, 0.0492],
         [0.8220, 0.5905, 0.8220, 0.5905]],
        [[0.0027, 0.6878, 0.0027, 0.6878],
         [0.5797, 0.0492, 0.5797, 0.0492],
         [0.8220, 0.5905, 0.8220, 0.5905],
         [0.0027, 0.6878, 0.0027, 0.6878],
         [0.5797, 0.0492, 0.5797, 0.0492],
         [0.8220, 0.5905, 0.8220, 0.5905],
         [0.0027, 0.6878, 0.0027, 0.6878],
         [0.5797, 0.0492, 0.5797, 0.0492],
         [0.8220, 0.5905, 0.8220, 0.5905]]])

3. 总结示例

>>> x = torch.tensor([1, 2, 3, 4])
tensor([1, 2, 3, 4])

>>> y = torch.rand(2,3)
tensor([[0.5423, 0.4213, 0.9477],
        [0.0385, 0.8777, 0.5502]])

>>> xx = x.view(-1, 1).repeat(1, len(y)).view(-1)
tensor([1, 1, 2, 2, 3, 3, 4, 4])

>>> yy = y.view(-1, 1).repeat(1, len(x)).view(-1)
>>> yy
tensor([0.5423, 0.5423, 0.5423, 0.5423, 0.4213, 0.4213, 0.4213, 0.4213, 0.9477,
        0.9477, 0.9477, 0.9477, 0.0385, 0.0385, 0.0385, 0.0385, 0.8777, 0.8777,
        0.8777, 0.8777, 0.5502, 0.5502, 0.5502, 0.5502])

  • 13
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
注意力机制是深度学习中的一种重要技术,它可以帮助模型在处理序列数据时更加关注重要的部分。在Python中,我们可以使用TensorFlow或者PyTorch等深度学习框架来实现注意力机制。以下是一个使用PyTorch实现注意力机制的简单示例代码: ``` python import torch import torch.nn as nn class Attention(nn.Module): def __init__(self, hidden_size): super(Attention, self).__init__() self.hidden_size = hidden_size self.attention = nn.Linear(hidden_size * 2, hidden_size) self.softmax = nn.Softmax(dim=1) def forward(self, encoder_outputs, hidden): """ encoder_outputs: [seq_len, batch_size, hidden_size] hidden: [1, batch_size, hidden_size] """ seq_len = encoder_outputs.size(0) batch_size = encoder_outputs.size(1) hidden = hidden.repeat(seq_len, 1, 1) energy = torch.cat((encoder_outputs, hidden), dim=2) attention_energies = self.attention(energy).view(seq_len, batch_size, -1) attention_weights = self.softmax(attention_energies) context = attention_weights * encoder_outputs context = torch.sum(context, dim=0) return context ``` 在这个示例中,我们定义了一个名为Attention的PyTorch模块,它包含一个线性层和一个Softmax函数,用于计算注意力权重。该模块的输入包括encoder_outputs和hidden,其中encoder_outputs是一个形状为[seq_len, batch_size, hidden_size]的张量,代表编码器的输出;hidden是一个形状为[1, batch_size, hidden_size]的张量,代表解码器的隐藏状态。在forward函数中,我们首先将hidden张量复制seq_len次,然后将encoder_outputs和hidden按列连接起来,通过线性层计算注意力能量,再通过Softmax函数将能量转换为注意力权重。最后,我们将注意力权重与encoder_outputs按元素相乘并求和,得到注意力上下文向量context。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李AI飞刀^_^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值