本文整理匯總了Python中torch.nn.Linear方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.Linear方法的具體用法?Python nn.Linear怎麽用?Python nn.Linear使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn
的用法示例。
在下文中一共展示了nn.Linear方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Linear [as 別名]
def __init__(self, block, layers, num_classes=1000):
self.inplanes = 64
super(MyResNet, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
# note the increasing dilation
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2, dilation=1)
self.layer3 = self._make_layer(block, 256, layers[2], stride=1, dilation=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=1, dilation=4)
# these layers will not be used
self.avgpool = nn.AvgPool2d(7)
self.fc = nn.Linear(512 * block.expansion, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
示例2: _init_modules
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Linear [as 別名]
def _init_modules(self):
self._init_head_tail()
# rpn
self.rpn_net = nn.Conv2d(self._net_conv_channels, cfg.RPN_CHANNELS, [3, 3], padding=1)
self.rpn_cls_score_net = nn.Conv2d(cfg.RPN_CHANNELS, self._num_anchors * 2, [1, 1])
self.rpn_bbox_pred_net = nn.Conv2d(cfg.RPN_CHANNELS, self._num_anchors * 4, [1, 1])
self.cls_score_net_fast = nn.Linear(self._fc7_channels, self._num_classes+1)
self.bbox_pred_net_fast = nn.Linear(self._fc7_channels, (self._num_classes+1) * 4)
self.cls_score_net = nn.Linear(self._fc7_channels, self._num_classes) # between class
self.bbox_pred_net = nn.Linear(self._fc7_channels, self._num_classes) # between boxes
self.init_weights()
開發者ID:Sunarker,項目名稱:Collaborative-Learning-for-Weakly-Supervised-Object-Detection,代碼行數:20,代碼來源:network.py
示例3: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Linear [as 別名]
def __init__(self, input_size, hidden_size, correlation_func=1, do_similarity=False):
super(AttentionScore, self).__init__()
self.correlation_func = correlation_func
self.hidden_size = hidden_size
if correlation_func == 2 or correlation_func == 3:
self.linear = nn.Linear(input_size, hidden_size, bias=False)
if do_similarity:
self.diagonal = Parameter(torch.ones(1, 1, 1) / (hidden_size ** 0.5), requires_grad=False)
else:
self.diagonal = Parameter(torch.ones(1, 1, hidden_size), requires_grad=True)
if correlation_func == 4:
self.linear = nn.Linear(input_size, input_size, bias=False)
if correlation_func == 5:
self.linear = nn.Linear(input_size, hidden_size, bias=False)
示例4: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Linear [as 別名]
def __init__(self, rnn_type, input_size, node_fdim, hidden_size, depth, dropout):
super(MPNEncoder, self).__init__()
self.hidden_size = hidden_size
self.input_size = input_size
self.depth = depth
self.W_o = nn.Sequential(
nn.Linear(node_fdim + hidden_size, hidden_size),
nn.ReLU(),
nn.Dropout(dropout)
)
if rnn_type == 'GRU':
self.rnn = GRU(input_size, hidden_size, depth)
elif rnn_type == 'LSTM':
self.rnn = LSTM(input_size, hidden_size, depth)
else:
raise ValueError('unsupported rnn cell type ' + rnn_type)
示例5: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Linear [as 別名]
def __init__(self, config):
super(LanguageModel, self).__init__()
self.config = config
self.ntoken = ntoken = config.ntoken
self.ninp = ninp = config.ninp
self.nhid = nhid = config.nhid
self.nlayers = nlayers = config.nlayers
self.encoder = nn.Embedding(ntoken, ninp)
self.dropouti = nn.Dropout(config.dropouti) if config.dropouti > 0 else None
self.lstm = LSTM([ninp] + [nhid] * nlayers, bias=False, layernorm=True,
dropoutr=config.dropoutr, dropouth=config.dropouth, dropouto=config.dropouto)
self.projection = nn.Linear(nhid, ninp)
self.decoder = nn.Linear(ninp, ntoken)
self.decoder.weight = self.encoder.weight
self.init_weights()
示例6: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Linear [as 別名]
def __init__(self):
super(CW2_Net, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 3)
self.bnm1 = nn.BatchNorm2d(32, momentum=0.1)
self.conv2 = nn.Conv2d(32, 64, 3)
self.bnm2 = nn.BatchNorm2d(64, momentum=0.1)
self.conv3 = nn.Conv2d(64, 128, 3)
self.bnm3 = nn.BatchNorm2d(128, momentum=0.1)
self.conv4 = nn.Conv2d(128, 128, 3)
self.bnm4 = nn.BatchNorm2d(128, momentum=0.1)
self.fc1 = nn.Linear(3200, 256)
#self.dropout1 = nn.Dropout(p=0.35, inplace=False)
self.bnm5 = nn.BatchNorm1d(256, momentum=0.1)
self.fc2 = nn.Linear(256, 256)
self.bnm6 = nn.BatchNorm1d(256, momentum=0.1)
self.fc3 = nn.Linear(256, 10)
#self.dropout2 = nn.Dropout(p=0.35, inplace=False)
#self.dropout3 = nn.Dropout(p=0.35, inplace=False)
示例7: init_bert_weights
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Linear [as 別名]
def init_bert_weights(self, module):
""" Initialize the weights.
"""
if isinstance(module, (nn.Linear, nn.Embedding)):
# Slightly different from the TF version which uses truncated_normal for initialization
# cf https://github.com/pytorch/pytorch/pull/5617
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
elif isinstance(module, BertLayerNorm):
module.bias.data.normal_(mean=0.0, std=self.config.initializer_range)
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
if isinstance(module, nn.Linear) and module.bias is not None:
module.bias.data.zero_()
示例8: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Linear [as 別名]
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.fc1 = nn.Linear(9216, 128)
示例9: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Linear [as 別名]
def __init__(self, config):
super(BertSelfAttention, self).__init__()
if config.hidden_size % config.num_attention_heads != 0:
raise ValueError(
"The hidden size (%d) is not a multiple of the number of attention "
"heads (%d)" % (config.hidden_size, config.num_attention_heads))
self.num_attention_heads = config.num_attention_heads
self.attention_head_size = int(config.hidden_size / config.num_attention_heads)
self.all_head_size = self.num_attention_heads * self.attention_head_size
self.query = nn.Linear(config.hidden_size, self.all_head_size)
self.key = nn.Linear(config.hidden_size, self.all_head_size)
self.value = nn.Linear(config.hidden_size, self.all_head_size)
self.dropout = nn.Dropout(config.attention_probs_dropout_prob)
示例10: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Linear [as 別名]
def __init__(self, config):
super(BertForQuestionAnswering, self).__init__(config)
self.bert = BertModel(config)
self.qa_outputs = nn.Linear(config.hidden_size, 1)
self.apply(self.init_bert_weights)
示例11: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Linear [as 別名]
def __init__(self, h, d_model, dropout=0.1):
"""Take in model size and number of heads."""
super(MultiHeadedAttention, self).__init__()
assert d_model % h == 0
# We assume d_v always equals d_k
self.d_k = d_model // h
self.h = h
self.linears = clones(nn.Linear(d_model, d_model), 4)
self.attn = None
self.dropout = nn.Dropout(p=dropout)