本文整理汇总了Python中torch.nn.functional.max_pool1d方法的典型用法代码示例。如果您正苦于以下问题:Python functional.max_pool1d方法的具体用法?Python functional.max_pool1d怎么用?Python functional.max_pool1d使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.nn.functional
的用法示例。
在下文中一共展示了functional.max_pool1d方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: max_pool1d
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import max_pool1d [as 别名]
def max_pool1d(inputs, kernel_size, stride=1, padding='same'):
'''
inputs: [N, T, C]
outputs: [N, T // stride, C]
'''
inputs = inputs.transpose(1, 2) # [N, C, T]
if padding == 'same':
left = (kernel_size - 1) // 2
right = (kernel_size - 1) - left
pad = (left, right)
else:
pad = (0, 0)
inputs = F.pad(inputs, pad)
outputs = F.max_pool1d(inputs, kernel_size, stride) # [N, C, T]
outputs = outputs.transpose(1, 2) # [N, T, C]
return outputs
示例2: update_coatt_cat_maxpool
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import max_pool1d [as 别名]
def update_coatt_cat_maxpool(self, query_embed, in_memory_embed, out_memory_embed, query_att, atten_mask=None, ctx_mask=None, query_mask=None):
attention = torch.bmm(query_embed, in_memory_embed.view(in_memory_embed.size(0), -1, in_memory_embed.size(-1))\
.transpose(1, 2)).view(query_embed.size(0), query_embed.size(1), in_memory_embed.size(1), -1) # bs * N * M * k
if ctx_mask is not None:
attention[:, :, :, -1] = ctx_mask.unsqueeze(1) * attention[:, :, :, -1].clone() - (1 - ctx_mask).unsqueeze(1) * INF
if atten_mask is not None:
attention = atten_mask.unsqueeze(1).unsqueeze(-1) * attention - (1 - atten_mask).unsqueeze(1).unsqueeze(-1) * INF
if query_mask is not None:
attention = query_mask.unsqueeze(2).unsqueeze(-1) * attention - (1 - query_mask).unsqueeze(2).unsqueeze(-1) * INF
# Importance module
kb_feature_att = F.max_pool1d(attention.view(attention.size(0), attention.size(1), -1).transpose(1, 2), kernel_size=attention.size(1)).squeeze(-1).view(attention.size(0), -1, attention.size(-1))
kb_feature_att = torch.softmax(kb_feature_att, dim=-1).view(-1, kb_feature_att.size(-1)).unsqueeze(1)
in_memory_embed = torch.bmm(kb_feature_att, in_memory_embed.view(-1, in_memory_embed.size(2), in_memory_embed.size(-1))).squeeze(1).view(in_memory_embed.size(0), in_memory_embed.size(1), -1)
out_memory_embed = out_memory_embed.sum(2)
# Enhanced module
attention = F.max_pool1d(attention.view(attention.size(0), -1, attention.size(-1)), kernel_size=attention.size(-1)).squeeze(-1).view(attention.size(0), attention.size(1), attention.size(2))
probs = torch.softmax(attention, dim=-1)
new_query_embed = query_embed + query_att.unsqueeze(2) * torch.bmm(probs, out_memory_embed)
probs2 = torch.softmax(attention, dim=1)
kb_att = torch.bmm(query_att.unsqueeze(1), probs).squeeze(1)
in_memory_embed = in_memory_embed + kb_att.unsqueeze(2) * torch.bmm(probs2.transpose(1, 2), new_query_embed)
return new_query_embed, in_memory_embed, out_memory_embed
示例3: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import max_pool1d [as 别名]
def forward(self, x, x_len=None):
"""x: [batch_size * max_length]
x_len: reserved
"""
x = self.embed(x)
if self.dropout:
x = F.dropout(x, p=self.dropout, training=self.training)
# Trun(batch_size, seq_len, embed_size) to (batch_size, embed_size, seq_len) for cnn1d
x = x.transpose(1, 2)
z = [conv(x) for conv in self.cnns]
output = [F.max_pool1d(i, kernel_size=i.size(-1)).squeeze(-1) for i in z]
if len(output) > 1:
output = self.fc(torch.cat(output, -1))
else:
output = output[0]
return None, output
示例4: forward_discriminator_embed
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import max_pool1d [as 别名]
def forward_discriminator_embed(self, inputs):
"""
Inputs must be embeddings: mbsize x seq_len x emb_dim
"""
inputs = inputs.unsqueeze(1) # mbsize x 1 x seq_len x emb_dim
x3 = F.relu(self.conv3(inputs)).squeeze()
x4 = F.relu(self.conv4(inputs)).squeeze()
x5 = F.relu(self.conv5(inputs)).squeeze()
# Max-over-time-pool
x3 = F.max_pool1d(x3, x3.size(2)).squeeze()
x4 = F.max_pool1d(x4, x4.size(2)).squeeze()
x5 = F.max_pool1d(x5, x5.size(2)).squeeze()
x = torch.cat([x3, x4, x5], dim=1)
y = self.disc_fc(x)
return y
示例5: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import max_pool1d [as 别名]
def forward(self, inputs):
inputs = self.word_emb(inputs)
inputs = inputs.unsqueeze(1)
x3 = F.relu(self.conv3(inputs)).squeeze()
x4 = F.relu(self.conv4(inputs)).squeeze()
x5 = F.relu(self.conv5(inputs)).squeeze()
# Max-over-time-pool
x3 = F.max_pool1d(x3, x3.size(2)).squeeze()
x4 = F.max_pool1d(x4, x4.size(2)).squeeze()
x5 = F.max_pool1d(x5, x5.size(2)).squeeze()
x = torch.cat([x3, x4, x5], dim=1)
y = self.discriminator(x)
return y
示例6: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import max_pool1d [as 别名]
def forward(self, x):
# x.shape = (seq_len, batch_size)
embedded_sent = self.embeddings(x)
# embedded_sent.shape = (seq_len, batch_size, embed_size)
lstm_out, (h_n,c_n) = self.lstm(embedded_sent)
# lstm_out.shape = (seq_len, batch_size, 2 * hidden_size)
input_features = torch.cat([lstm_out,embedded_sent], 2).permute(1,0,2)
# final_features.shape = (batch_size, seq_len, embed_size + 2*hidden_size)
linear_output = self.tanh(
self.W(input_features)
)
# linear_output.shape = (batch_size, seq_len, hidden_size_linear)
linear_output = linear_output.permute(0,2,1) # Reshaping fot max_pool
max_out_features = F.max_pool1d(linear_output, linear_output.shape[2]).squeeze(2)
# max_out_features.shape = (batch_size, hidden_size_linear)
max_out_features = self.dropout(max_out_features)
final_out = self.fc(max_out_features)
return self.softmax(final_out)
示例7: test_conv1d_pool1d
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import max_pool1d [as 别名]
def test_conv1d_pool1d(self, minimum_ios_deployment_target='13'):
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv1d(in_channels=4,
out_channels=32, kernel_size=3, stride=1, padding=1)
self.conv2 = nn.Conv1d(in_channels=32,
out_channels=64, kernel_size=3, stride=1, padding=1)
def forward(self, x):
x = x.permute(0, 2, 1)
x = self.conv1(x)
x = F.relu(x)
x = F.max_pool1d(x, 2)
x = self.conv2(x)
x = F.relu(x)
return x
torch_model = Net() # type: ignore
torch_model.train(False)
_test_torch_model_single_io(torch_model, (2, 10, 4), (2, 10, 4),
minimum_ios_deployment_target=minimum_ios_deployment_target)
示例8: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import max_pool1d [as 别名]
def forward(self, x, mask=None):
r"""
:param torch.FloatTensor x: batch_size x max_len x input_size, 一般是经过embedding后的值
:param mask: batch_size x max_len, pad的地方为0。不影响卷积运算,max-pool一定不会pool到pad为0的位置
:return:
"""
# [N,L,C] -> [N,C,L]
x = torch.transpose(x, 1, 2)
# convolution
xs = [self.activation(conv(x)) for conv in self.convs] # [[N,C,L], ...]
if mask is not None:
mask = mask.unsqueeze(1) # B x 1 x L
xs = [x.masked_fill_(mask.eq(False), float('-inf')) for x in xs]
# max-pooling
xs = [F.max_pool1d(input=i, kernel_size=i.size(2)).squeeze(2)
for i in xs] # [[N, C], ...]
return torch.cat(xs, dim=-1) # [N, C]
示例9: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import max_pool1d [as 别名]
def forward(self, input):
# input: a batch of Example object [batch_size, N, seq_len]
batch_size, N, _ = input.size()
input = input.view(-1, input.size(2)) # [batch_size*N, L]
input_sent_len = ((input!=0).sum(dim=1)).int() # [batch_size*N, 1]
enc_embed_input = self.embed(input) # [batch_size*N, L, D]
input_pos = torch.Tensor([np.hstack((np.arange(1, sentlen + 1), np.zeros(self.sent_max_len - sentlen))) for sentlen in input_sent_len])
if self._hps.cuda:
input_pos = input_pos.cuda()
enc_pos_embed_input = self.position_embedding(input_pos.long()) # [batch_size*N, D]
# print(enc_embed_input.size())
# print(enc_pos_embed_input.size())
enc_conv_input = enc_embed_input + enc_pos_embed_input
enc_conv_input = enc_conv_input.unsqueeze(1) # (batch * N,Ci,L,D)
enc_conv_output = [F.relu(conv(enc_conv_input)).squeeze(3) for conv in self.convs] # kernel_sizes * (batch*N, Co, W)
enc_maxpool_output = [F.max_pool1d(x, x.size(2)).squeeze(2) for x in enc_conv_output] # kernel_sizes * (batch*N, Co)
sent_embedding = torch.cat(enc_maxpool_output, 1) # (batch*N, Co * kernel_sizes)
sent_embedding = sent_embedding.view(batch_size, N, -1)
return sent_embedding
示例10: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import max_pool1d [as 别名]
def forward(self, x, src_states, src_mask, tgt_mask):
"""
x: (batch_size, tgt_seq_len, d_model)
src_states: (batch_size, src_seq_len, d_model)
src_mask: (batch_size, 1, src_seq_len)
tgt_mask: (batch_size, tgt_seq_len, tgt_seq_len)
"""
if print_dims:
print("{0}: x: type: {1}, shape: {2}".format(self.__class__.__name__, x.type(), x.shape))
print("{0}: src_states: type: {1}, shape: {2}".format(self.__class__.__name__, src_states.type(), src_states.shape))
print("{0}: src_mask: type: {1}, shape: {2}".format(self.__class__.__name__, src_mask.type(), src_mask.shape))
print("{0}: tgt_mask: type: {1}, shape: {2}".format(self.__class__.__name__, tgt_mask.type(), tgt_mask.shape))
for layer in self.layers:
x = layer(x, src_states, src_mask, tgt_mask)
x = self.norm(x) # (batch_size, tgt_seq_len, d_model)
if print_dims:
print("{0}: x (output): type: {1}, shape: {2}".format(self.__class__.__name__, x.type(), x.shape))
# add max pooling across sequences
x = F.max_pool1d(x.permute(0,2,1), x.shape[1]).squeeze(-1) # (batch_size, d_model)
return x
示例11: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import max_pool1d [as 别名]
def forward(self, input_tensors):
feature = self.word_rep(input_tensors)
aspect_i = input_tensors[2]
aspect_v = self.AE(aspect_i) # (N, L', D)
feature = feature.view(1, feature.size()[0], -1)
x = [F.tanh(conv(feature.transpose(1, 2))) for conv in self.convs1] # [(N,Co,L), ...]*len(Ks)
y = [F.relu(conv(feature.transpose(1, 2)) + self.fc_aspect(aspect_v).unsqueeze(2)) for conv in self.convs2]
x = [i * j for i, j in zip(x, y)]
# pooling method
x0 = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in x] # [(N,Co), ...]*len(Ks)
x0 = [i.view(i.size(0), -1) for i in x0]
x0 = torch.cat(x0, 1)
logit = self.fc1(x0) # (N,C)
return logit
示例12: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import max_pool1d [as 别名]
def forward(self, x, umask):
num_utt, batch, num_words = x.size()
x = x.type(LongTensor) # (num_utt, batch, num_words)
x = x.view(-1, num_words) # (num_utt, batch, num_words) -> (num_utt * batch, num_words)
emb = self.embedding(x) # (num_utt * batch, num_words) -> (num_utt * batch, num_words, embedding_dim)
emb = emb.transpose(-2, -1).contiguous() # (num_utt * batch, num_words, embedding_dim) -> (num_utt * batch, embedding_dim, num_words)
convoluted = [F.relu(conv(emb)) for conv in self.convs]
pooled = [F.max_pool1d(c, c.size(2)).squeeze() for c in convoluted]
concated = torch.cat(pooled, 1)
features = F.relu(self.fc(self.dropout(concated))) # (num_utt * batch, embedding_dim//2) -> (num_utt * batch, output_size)
features = features.view(num_utt, batch, -1) # (num_utt * batch, output_size) -> (num_utt, batch, output_size)
mask = umask.unsqueeze(-1).type(FloatTensor) # (batch, num_utt) -> (batch, num_utt, 1)
mask = mask.transpose(0, 1) # (batch, num_utt, 1) -> (num_utt, batch, 1)
mask = mask.repeat(1, 1, self.feature_dim) # (num_utt, batch, 1) -> (num_utt, batch, output_size)
features = (features * mask) # (num_utt, batch, output_size) -> (num_utt, batch, output_size)
return features
示例13: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import max_pool1d [as 别名]
def forward(self, x, umask):
num_utt, batch, num_words = x.size()
x = x.type(LongTensor) # (num_utt, batch, num_words)
x = x.view(-1, num_words) # (num_utt, batch, num_words) -> (num_utt * batch, num_words)
emb = self.embedding(x) # (num_utt * batch, num_words) -> (num_utt * batch, num_words, 300)
emb = emb.transpose(-2, -1).contiguous() # (num_utt * batch, num_words, 300) -> (num_utt * batch, 300, num_words)
convoluted = [F.relu(conv(emb)) for conv in self.convs]
pooled = [F.max_pool1d(c, c.size(2)).squeeze() for c in convoluted]
concated = torch.cat(pooled, 1)
features = F.relu(self.fc(self.dropout(concated))) # (num_utt * batch, 150) -> (num_utt * batch, 100)
features = features.view(num_utt, batch, -1) # (num_utt * batch, 100) -> (num_utt, batch, 100)
mask = umask.unsqueeze(-1).type(FloatTensor) # (batch, num_utt) -> (batch, num_utt, 1)
mask = mask.transpose(0, 1) # (batch, num_utt, 1) -> (num_utt, batch, 1)
mask = mask.repeat(1, 1, self.feature_dim) # (num_utt, batch, 1) -> (num_utt, batch, 100)
features = (features * mask) # (num_utt, batch, 100) -> (num_utt, batch, 100)
return features
示例14: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import max_pool1d [as 别名]
def forward(self, x, umask):
num_utt, batch, num_words = x.size()
x = x.type(LongTensor) # (num_utt, batch, num_words)
x = x.view(-1, num_words) # (num_utt, batch, num_words) -> (num_utt * batch, num_words)
emb = self.embedding(x) # (num_utt * batch, num_words) -> (num_utt * batch, num_words, 300)
emb = emb.transpose(-2,
-1).contiguous() # (num_utt * batch, num_words, 300) -> (num_utt * batch, 300, num_words)
convoluted = [F.relu(conv(emb)) for conv in self.convs]
pooled = [F.max_pool1d(c, c.size(2)).squeeze() for c in convoluted]
concated = torch.cat(pooled, 1)
features = F.relu(self.fc(self.dropout(concated))) # (num_utt * batch, 150) -> (num_utt * batch, 100)
features = features.view(num_utt, batch, -1) # (num_utt * batch, 100) -> (num_utt, batch, 100)
mask = umask.unsqueeze(-1).type(FloatTensor) # (batch, num_utt) -> (batch, num_utt, 1)
mask = mask.transpose(0, 1) # (batch, num_utt, 1) -> (num_utt, batch, 1)
mask = mask.repeat(1, 1, self.feature_dim) # (num_utt, batch, 1) -> (num_utt, batch, 100)
features = (features * mask) # (num_utt, batch, 100) -> (num_utt, batch, 100)
return features
示例15: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import max_pool1d [as 别名]
def forward(self, input):
embed = self.embed(input)
embed = self.dropout(embed)
input = embed.view(len(input), embed.size(1), -1)
# gru
gru_out, _ = self.bigru(input)
gru_out = torch.transpose(gru_out, 0, 1)
gru_out = torch.transpose(gru_out, 1, 2)
# pooling
# gru_out = F.tanh(gru_out)
gru_out = F.max_pool1d(gru_out, gru_out.size(2)).squeeze(2)
gru_out = F.tanh(gru_out)
# linear
y = self.hidden2label(gru_out)
logit = y
return logit