本文整理匯總了Python中torch.nn.Softmax方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.Softmax方法的具體用法?Python nn.Softmax怎麽用?Python nn.Softmax使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn
的用法示例。
在下文中一共展示了nn.Softmax方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _attn
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Softmax [as 別名]
def _attn(self, q, k, v, sequence_mask):
w = torch.matmul(q, k)
if self.scale:
w = w / math.sqrt(v.size(-1))
b_subset = self.b[:, :, :w.size(-2), :w.size(-1)]
if sequence_mask is not None:
b_subset = b_subset * sequence_mask.view(
sequence_mask.size(0), 1, -1)
b_subset = b_subset.permute(1, 0, 2, 3)
w = w * b_subset + -1e9 * (1 - b_subset)
w = nn.Softmax(dim=-1)(w)
w = self.attn_dropout(w)
return torch.matmul(w, v)
示例2: r_duvenaud
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Softmax [as 別名]
def r_duvenaud(self, h):
# layers
aux = []
for l in range(len(h)):
param_sz = self.learn_args[l].size()
parameter_mat = torch.t(self.learn_args[l])[None, ...].expand(h[l].size(0), param_sz[1],
param_sz[0])
aux.append(torch.transpose(torch.bmm(parameter_mat, torch.transpose(h[l], 1, 2)), 1, 2))
for j in range(0, aux[l].size(1)):
# Mask whole 0 vectors
aux[l][:, j, :] = nn.Softmax()(aux[l][:, j, :].clone())*(torch.sum(aux[l][:, j, :] != 0, 1) > 0).expand_as(aux[l][:, j, :]).type_as(aux[l])
aux = torch.sum(torch.sum(torch.stack(aux, 3), 3), 1)
return self.learn_modules[0](torch.squeeze(aux))
示例3: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Softmax [as 別名]
def __init__(self, sentence_encoder, num_class, rel2id):
"""
Args:
sentence_encoder: encoder for sentences
num_class: number of classes
id2rel: dictionary of id -> relation name mapping
"""
super().__init__()
self.sentence_encoder = sentence_encoder
self.num_class = num_class
self.fc = nn.Linear(self.sentence_encoder.hidden_size, num_class)
self.softmax = nn.Softmax(-1)
self.rel2id = rel2id
self.id2rel = {}
self.drop = nn.Dropout()
for rel, id in rel2id.items():
self.id2rel[id] = rel
示例4: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Softmax [as 別名]
def __init__(self,block,block_list):
super(ResNet,self).__init__()
self.head_conv = nn.Sequential(
nn.Conv2d(3,64,7,2,3,bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),)
self.maxpool_1 = nn.MaxPool2d(3,2,1)
b_ = block.expansion
self.layer_1 = self._make_layer(block,64,64*b_,block_list[0],1)
self.layer_2 = self._make_layer(block,64*b_,128*b_,block_list[1],2)
self.layer_3 = self._make_layer(block,128*b_,256*b_,block_list[2],2)
self.layer_4 = self._make_layer(block,256*b_,512*b_,block_list[3],2)
self.avgpool_1 = nn.AdaptiveAvgPool2d((1,1))
self.fc_1 = nn.Sequential(
nn.Flatten(),
nn.Linear(512*b_,1000),
nn.Softmax(dim = 1),)
self._initialization()
示例5: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Softmax [as 別名]
def __init__(self,block,block_list,cardinality):
super(ResNet,self).__init__()
self.head_conv = nn.Sequential(
nn.Conv2d(3,64,7,2,3,bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),)
self.maxpool_1 = nn.MaxPool2d(3,2,1)
b_ = block.expansion
self.layer_1 = self._make_layer(block,64,128*b_,block_list[0],1,cardinality)
self.layer_2 = self._make_layer(block,128*b_,256*b_,block_list[1],2,cardinality)
self.layer_3 = self._make_layer(block,256*b_,512*b_,block_list[2],2,cardinality)
self.layer_4 = self._make_layer(block,512*b_,1024*b_,block_list[3],2,cardinality)
self.avgpool_1 = nn.AdaptiveAvgPool2d((1,1))
self.fc_1 = nn.Sequential(
nn.Flatten(),
nn.Linear(1024*b_,1000),
nn.Softmax(dim = 1),)
self._initialization()
示例6: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Softmax [as 別名]
def __init__(self,k,block_list,num_init_features=64, bn_size=4,
drop_rate=0, memory_efficient=False):
super(DenseNet,self).__init__()
self.head_conv = nn.Sequential(
nn.Conv2d(3,num_init_features,7,2,3,bias=False),
nn.BatchNorm2d(num_init_features),
nn.ReLU(inplace=True),)
self.maxpool_1 = nn.MaxPool2d(3,2,1)
self.dense_body, self.final_channels = self._make_layers(num_init_features,
bn_size,block_list,k,drop_rate, memory_efficient)
self.avgpool_1 = nn.AdaptiveAvgPool2d((1,1))
self.fc_1 = nn.Sequential(
nn.Flatten(),
nn.Linear(self.final_channels,1000),
nn.Softmax(dim = 1),)
self._initialization()
示例7: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Softmax [as 別名]
def __init__(self,):
super(MobileNet_V1,self).__init__()
self.conv = nn.Sequential(BasicConv(3,32,3,2,1),
DPConv(32,64,1),
DPConv(64,128,2),
DPConv(128,128,1),
DPConv(128,256,2),
DPConv(256,256,1),
DPConv(256,512,2),
DPConv(512,512,1),
DPConv(512,512,1),
DPConv(512,512,1),
DPConv(512,512,1),
DPConv(512,512,1),
DPConv(512,1024,2),
DPConv(1024,1024,1),)
self.final = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Flatten(),
nn.Linear(1024,1000),
nn.Softmax(dim=1)
)
示例8: compute_madd
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Softmax [as 別名]
def compute_madd(module, inp, out):
if isinstance(module, nn.Conv2d):
return compute_Conv2d_madd(module, inp, out)
elif isinstance(module, nn.ConvTranspose2d):
return compute_ConvTranspose2d_madd(module, inp, out)
elif isinstance(module, nn.BatchNorm2d):
return compute_BatchNorm2d_madd(module, inp, out)
elif isinstance(module, nn.MaxPool2d):
return compute_MaxPool2d_madd(module, inp, out)
elif isinstance(module, nn.AvgPool2d):
return compute_AvgPool2d_madd(module, inp, out)
elif isinstance(module, (nn.ReLU, nn.ReLU6)):
return compute_ReLU_madd(module, inp, out)
elif isinstance(module, nn.Softmax):
return compute_Softmax_madd(module, inp, out)
elif isinstance(module, nn.Linear):
return compute_Linear_madd(module, inp, out)
elif isinstance(module, nn.Bilinear):
return compute_Bilinear_madd(module, inp[0], inp[1], out)
else:
return 0
示例9: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Softmax [as 別名]
def __init__(self, phase, size, base, extras, head, num_classes):
super(SSD, self).__init__()
self.phase = phase
self.num_classes = num_classes
if(size==300):
self.cfg = (coco, voc300)[num_classes == 21]
else:
self.cfg = (coco, voc512)[num_classes == 21]
self.priorbox = PriorBox(self.cfg)
self.priors = Variable(self.priorbox.forward(), volatile=True)
self.size = size
# SSD network
self.vgg = nn.ModuleList(base)
# Layer learns to scale the l2 normalized features from conv4_3
self.L2Norm = L2Norm(512, 20)
self.extras = nn.ModuleList(extras)
self.loc = nn.ModuleList(head[0])
self.conf = nn.ModuleList(head[1])
if phase == 'test':
self.softmax = nn.Softmax(dim=-1)
self.detect = Detect(num_classes, 0, 200, 0.01, 0.45)
示例10: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Softmax [as 別名]
def __init__(self, vocab, embedding_dim, hop, dropout, unk_mask):
super(DecoderMemNN, self).__init__()
self.num_vocab = vocab
self.max_hops = hop
self.embedding_dim = embedding_dim
self.dropout = dropout
self.unk_mask = unk_mask
for hop in range(self.max_hops+1):
C = nn.Embedding(self.num_vocab, embedding_dim, padding_idx=PAD_token)
C.weight.data.normal_(0, 0.1)
self.add_module("C_{}".format(hop), C)
self.C = AttrProxy(self, "C_")
self.softmax = nn.Softmax(dim=1)
self.W = nn.Linear(embedding_dim,1)
self.W1 = nn.Linear(2*embedding_dim,self.num_vocab)
self.gru = nn.GRU(embedding_dim, embedding_dim, dropout=dropout)
示例11: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Softmax [as 別名]
def __init__(self, lang, shared_emb, vocab_size, hidden_size, dropout, slots, nb_gate):
super(Generator, self).__init__()
self.vocab_size = vocab_size
self.lang = lang
self.embedding = shared_emb
self.dropout_layer = nn.Dropout(dropout)
self.gru = nn.GRU(hidden_size, hidden_size, dropout=dropout)
self.nb_gate = nb_gate
self.hidden_size = hidden_size
self.W_ratio = nn.Linear(3 * hidden_size, 1)
self.softmax = nn.Softmax(dim=1)
self.sigmoid = nn.Sigmoid()
self.slots = slots
self.W_gate = nn.Linear(hidden_size, nb_gate)
# Create independent slot embeddings
self.slot_w2i = {}
for slot in self.slots:
if slot.split("-")[0] not in self.slot_w2i.keys():
self.slot_w2i[slot.split("-")[0]] = len(self.slot_w2i)
if slot.split("-")[1] not in self.slot_w2i.keys():
self.slot_w2i[slot.split("-")[1]] = len(self.slot_w2i)
self.Slot_emb = nn.Embedding(len(self.slot_w2i), hidden_size)
self.Slot_emb.weight.data.normal_(0, 0.1)
示例12: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Softmax [as 別名]
def __init__(self, N_word, N_h, N_depth, use_ca):
super(AggPredictor, self).__init__()
self.use_ca = use_ca
self.agg_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2,
num_layers=N_depth, batch_first=True,
dropout=0.3, bidirectional=True)
if use_ca:
print "Using column attention on aggregator predicting"
self.agg_col_name_enc = nn.LSTM(input_size=N_word,
hidden_size=N_h/2, num_layers=N_depth,
batch_first=True, dropout=0.3, bidirectional=True)
self.agg_att = nn.Linear(N_h, N_h)
else:
print "Not using column attention on aggregator predicting"
self.agg_att = nn.Linear(N_h, 1)
self.agg_out = nn.Sequential(nn.Linear(N_h, N_h),
nn.Tanh(), nn.Linear(N_h, 6))
self.softmax = nn.Softmax()
示例13: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Softmax [as 別名]
def __init__(self, N_word, N_h, N_depth, max_col_num, max_tok_num, gpu):
super(Seq2SQLCondPredictor, self).__init__()
print "Seq2SQL where prediction"
self.N_h = N_h
self.max_tok_num = max_tok_num
self.max_col_num = max_col_num
self.gpu = gpu
self.cond_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2,
num_layers=N_depth, batch_first=True,
dropout=0.3, bidirectional=True)
self.cond_decoder = nn.LSTM(input_size=self.max_tok_num,
hidden_size=N_h, num_layers=N_depth,
batch_first=True, dropout=0.3)
self.cond_out_g = nn.Linear(N_h, N_h)
self.cond_out_h = nn.Linear(N_h, N_h)
self.cond_out = nn.Sequential(nn.Tanh(), nn.Linear(N_h, 1))
self.softmax = nn.Softmax()
示例14: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Softmax [as 別名]
def __init__(self, N_word, N_h, N_depth, max_tok_num, use_ca):
super(SelPredictor, self).__init__()
self.use_ca = use_ca
self.max_tok_num = max_tok_num
self.sel_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2,
num_layers=N_depth, batch_first=True,
dropout=0.3, bidirectional=True)
if use_ca:
print "Using column attention on selection predicting"
self.sel_att = nn.Linear(N_h, N_h)
else:
print "Not using column attention on selection predicting"
self.sel_att = nn.Linear(N_h, 1)
self.sel_col_name_enc = nn.LSTM(input_size=N_word, hidden_size=N_h/2,
num_layers=N_depth, batch_first=True,
dropout=0.3, bidirectional=True)
self.sel_out_K = nn.Linear(N_h, N_h)
self.sel_out_col = nn.Linear(N_h, N_h)
self.sel_out = nn.Sequential(nn.Tanh(), nn.Linear(N_h, 1))
self.softmax = nn.Softmax()
示例15: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Softmax [as 別名]
def __init__(self, base, extras, norm, head, feature_layer, num_classes):
super(RFB, self).__init__()
self.num_classes = num_classes
# RFB network
self.base = nn.ModuleList(base)
self.norm = nn.ModuleList(norm)
self.extras = nn.ModuleList(extras)
self.loc = nn.ModuleList(head[0])
self.conf = nn.ModuleList(head[1])
self.softmax = nn.Softmax(dim=-1)
self.feature_layer = feature_layer[0]
self.indicator = 0
for layer in self.feature_layer:
if isinstance(layer, int):
continue
elif layer == '' or layer == 'S':
break
else:
self.indicator += 1