本文整理匯總了Python中torch.nn.Tanh方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.Tanh方法的具體用法?Python nn.Tanh怎麽用?Python nn.Tanh使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn
的用法示例。
在下文中一共展示了nn.Tanh方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Tanh [as 別名]
def __init__(self, input_size, n_channels, ngf, n_layers, activation='tanh'):
super(ImageDecoder, self).__init__()
ngf = ngf * (2 ** (n_layers - 2))
layers = [nn.ConvTranspose2d(input_size, ngf, 4, 1, 0, bias=False),
nn.BatchNorm2d(ngf),
nn.ReLU(True)]
for i in range(1, n_layers - 1):
layers += [nn.ConvTranspose2d(ngf, ngf // 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf // 2),
nn.ReLU(True)]
ngf = ngf // 2
layers += [nn.ConvTranspose2d(ngf, n_channels, 4, 2, 1, bias=False)]
if activation == 'tanh':
layers += [nn.Tanh()]
elif activation == 'sigmoid':
layers += [nn.Sigmoid()]
else:
raise NotImplementedError
self.main = nn.Sequential(*layers)
示例2: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Tanh [as 別名]
def __init__(self, input_dropout_p, rnn_cell, input_size, hidden_size, num_layers, output_dropout_p, bidirectional, variable_lengths):
super(EncoderGRUATTN, self).__init__(input_dropout_p=input_dropout_p,
rnn_cell=rnn_cell,
input_size=input_size,
hidden_size=hidden_size,
num_layers=num_layers,
output_dropout_p=output_dropout_p,
bidirectional=bidirectional)
self.variable_lengths = variable_lengths
self.nhid_attn = hidden_size
self.output_size = hidden_size*2 if bidirectional else hidden_size
# attention to combine selection hidden states
self.attn = nn.Sequential(
nn.Linear(2 * hidden_size, hidden_size),
nn.Tanh(),
nn.Linear(hidden_size, 1)
)
示例3: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Tanh [as 別名]
def __init__(self, config):
super().__init__()
self.config = config
self.relu = nn.ReLU(inplace=True)
self.deconv1 = nn.ConvTranspose2d(in_channels=self.config.g_input_size, out_channels=self.config.num_filt_g * 8, kernel_size=4, stride=1, padding=0, bias=False)
self.batch_norm1 = nn.BatchNorm2d(self.config.num_filt_g*8)
self.deconv2 = nn.ConvTranspose2d(in_channels=self.config.num_filt_g * 8, out_channels=self.config.num_filt_g * 4, kernel_size=4, stride=2, padding=1, bias=False)
self.batch_norm2 = nn.BatchNorm2d(self.config.num_filt_g*4)
self.deconv3 = nn.ConvTranspose2d(in_channels=self.config.num_filt_g * 4, out_channels=self.config.num_filt_g * 2, kernel_size=4, stride=2, padding=1, bias=False)
self.batch_norm3 = nn.BatchNorm2d(self.config.num_filt_g*2)
self.deconv4 = nn.ConvTranspose2d(in_channels=self.config.num_filt_g * 2, out_channels=self.config.num_filt_g , kernel_size=4, stride=2, padding=1, bias=False)
self.batch_norm4 = nn.BatchNorm2d(self.config.num_filt_g)
self.deconv5 = nn.ConvTranspose2d(in_channels=self.config.num_filt_g, out_channels=self.config.input_channels, kernel_size=4, stride=2, padding=1, bias=False)
self.out = nn.Tanh()
self.apply(weights_init)
示例4: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Tanh [as 別名]
def __init__(self, hidden_dim):
""" Implementation of customized LSTM for set2set """
super(Set2SetLSTM, self).__init__()
self.hidden_dim = hidden_dim
self.forget_gate = nn.Sequential(
*[nn.Linear(2 * self.hidden_dim, self.hidden_dim),
nn.Sigmoid()])
self.input_gate = nn.Sequential(
*[nn.Linear(2 * self.hidden_dim, self.hidden_dim),
nn.Sigmoid()])
self.output_gate = nn.Sequential(
*[nn.Linear(2 * self.hidden_dim, self.hidden_dim),
nn.Sigmoid()])
self.memory_gate = nn.Sequential(
*[nn.Linear(2 * self.hidden_dim, self.hidden_dim),
nn.Tanh()])
self._init_param()
示例5: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Tanh [as 別名]
def __init__(self, input_nc=3, output_nc=3, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=True, num_blocks=6):
super(ResnetGenerator, self).__init__()
if type(norm_layer) == functools.partial:
use_bias = norm_layer.func == nn.InstanceNorm2d
else:
use_bias = norm_layer == nn.InstanceNorm2d
res_model = [nn.ReflectionPad2d(3),
conv_norm_relu(input_nc, ngf * 1, 7, norm_layer=norm_layer, bias=use_bias),
conv_norm_relu(ngf * 1, ngf * 2, 3, 2, 1, norm_layer=norm_layer, bias=use_bias),
conv_norm_relu(ngf * 2, ngf * 4, 3, 2, 1, norm_layer=norm_layer, bias=use_bias)]
for i in range(num_blocks):
res_model += [ResidualBlock(ngf * 4, norm_layer, use_dropout, use_bias)]
res_model += [dconv_norm_relu(ngf * 4, ngf * 2, 3, 2, 1, 1, norm_layer=norm_layer, bias=use_bias),
dconv_norm_relu(ngf * 2, ngf * 1, 3, 2, 1, 1, norm_layer=norm_layer, bias=use_bias),
nn.ReflectionPad2d(3),
nn.Conv2d(ngf, output_nc, 7),
nn.Tanh()]
self.res_model = nn.Sequential(*res_model)
示例6: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Tanh [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()
示例7: nonlinearity
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Tanh [as 別名]
def nonlinearity(h_nonlin_name):
def Nonlinearity(nonlin_name):
if nonlin_name == 'relu':
m = nn.ReLU()
elif nonlin_name == 'tanh':
m = nn.Tanh()
elif nonlin_name == 'elu':
m = nn.ELU()
else:
raise ValueError
return m
return hpt.siso_pytorch_module_from_pytorch_layer_fn(
Nonlinearity, {'nonlin_name': h_nonlin_name})
示例8: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Tanh [as 別名]
def __init__(self, device, m=[24, 12]):
super(MnistAE, self).__init__()
self.m = m
self.encoder = nn.Sequential(
nn.Conv2d(1, self.m[0], 3, stride=1, padding=0),
nn.ReLU(True),
nn.MaxPool2d(2, stride=2, padding=0),
nn.Conv2d(self.m[0], self.m[1], 3, stride=1, padding=0),
nn.ReLU(True),
nn.MaxPool2d(2, stride=1, padding=0)
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(self.m[1], self.m[1], 5, stride=2, padding=0),
nn.ReLU(True),
nn.ConvTranspose2d(self.m[1], self.m[0], 4, stride=1, padding=0),
nn.ReLU(True),
nn.ConvTranspose2d(self.m[0], 1, 3, stride=1, padding=0),
nn.Tanh()
)
示例9: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Tanh [as 別名]
def __init__(self, dim, coverage=False, attn_type="dot"):
super(GlobalAttention, self).__init__()
self.dim = dim
self.attn_type = attn_type
assert (self.attn_type in ["dot", "general", "mlp"]), (
"Please select a valid attention type.")
if self.attn_type == "general":
self.linear_in = nn.Linear(dim, dim, bias=False)
elif self.attn_type == "mlp":
self.linear_context = nn.Linear(dim, dim, bias=False)
self.linear_query = nn.Linear(dim, dim, bias=True)
self.v = nn.Linear(dim, 1, bias=False)
# mlp wants it with bias
out_bias = self.attn_type == "mlp"
self.linear_out = nn.Linear(dim*2, dim, bias=out_bias)
self.sm = nn.Softmax(dim=-1)
self.tanh = nn.Tanh()
if coverage:
self.linear_cover = nn.Linear(1, dim, bias=False)
示例10: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Tanh [as 別名]
def __init__(self, state_dim, n_node, n_edge_types):
super(Propogator, self).__init__()
self.n_node = n_node
self.n_edge_types = n_edge_types
self.reset_gate = nn.Sequential(
nn.Linear(state_dim*3, state_dim),
nn.Sigmoid()
)
self.update_gate = nn.Sequential(
nn.Linear(state_dim*3, state_dim),
nn.Sigmoid()
)
self.tansform = nn.Sequential(
nn.Linear(state_dim*3, state_dim),
nn.Tanh()
)
示例11: define_module
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Tanh [as 別名]
def define_module(self):
ninput = self.z_dim + self.ef_dim
ngf = self.gf_dim
# TEXT.DIMENSION -> GAN.CONDITION_DIM
self.ca_net = CA_NET()
# -> ngf x 4 x 4
self.fc = nn.Sequential(
nn.Linear(ninput, ngf * 4 * 4, bias=False),
nn.BatchNorm1d(ngf * 4 * 4),
nn.ReLU(True))
# ngf x 4 x 4 -> ngf/2 x 8 x 8
self.upsample1 = upBlock(ngf, ngf // 2)
# -> ngf/4 x 16 x 16
self.upsample2 = upBlock(ngf // 2, ngf // 4)
# -> ngf/8 x 32 x 32
self.upsample3 = upBlock(ngf // 4, ngf // 8)
# -> ngf/16 x 64 x 64
self.upsample4 = upBlock(ngf // 8, ngf // 16)
# -> 3 x 64 x 64
self.img = nn.Sequential(
conv3x3(ngf // 16, 3),
nn.Tanh())
示例12: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Tanh [as 別名]
def __init__(self, num_nodes=50, ip_dim=1, op_dim=1, activation_type='relu', args=None):
super(FCNet, self).__init__()
self.args = args
if activation_type == 'relu':
self.activation = nn.ReLU()
elif activation_type == 'tanh':
self.activation = nn.Tanh()
else:
print("Activation Type not supported")
return
layer = Linear
self.fc_hidden = []
self.fc1 = layer(ip_dim, num_nodes)
self.bn1 = nn.BatchNorm1d(num_nodes)
for _ in np.arange(self.args.num_layers - 1):
self.fc_hidden.append(layer(num_nodes, num_nodes))
self.fc_hidden.append(nn.BatchNorm1d(num_nodes))
self.fc_hidden.append(self.activation)
self.features = nn.Sequential(*self.fc_hidden)
self.fc_out = layer(num_nodes, op_dim)
示例13: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Tanh [as 別名]
def __init__(self,
num_inputs,
num_hidden,
num_cond_inputs=None,
act='relu',
pre_exp_tanh=False):
super(MADE, self).__init__()
activations = {'relu': nn.ReLU, 'sigmoid': nn.Sigmoid, 'tanh': nn.Tanh}
act_func = activations[act]
input_mask = get_mask(
num_inputs, num_hidden, num_inputs, mask_type='input')
hidden_mask = get_mask(num_hidden, num_hidden, num_inputs)
output_mask = get_mask(
num_hidden, num_inputs * 2, num_inputs, mask_type='output')
self.joiner = nn.MaskedLinear(num_inputs, num_hidden, input_mask,
num_cond_inputs)
self.trunk = nn.Sequential(act_func(),
nn.MaskedLinear(num_hidden, num_hidden,
hidden_mask), act_func(),
nn.MaskedLinear(num_hidden, num_inputs * 2,
output_mask))
示例14: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Tanh [as 別名]
def __init__(self, config):
super(BertPooler, self).__init__()
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.activation = nn.Tanh()
示例15: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import Tanh [as 別名]
def __init__(self, input_size, hidden_size, depth):
super(LSTM, self).__init__()
self.hidden_size = hidden_size
self.input_size = input_size
self.depth = depth
self.W_i = nn.Sequential( nn.Linear(input_size + hidden_size, hidden_size), nn.Sigmoid() )
self.W_o = nn.Sequential( nn.Linear(input_size + hidden_size, hidden_size), nn.Sigmoid() )
self.W_f = nn.Sequential( nn.Linear(input_size + hidden_size, hidden_size), nn.Sigmoid() )
self.W = nn.Sequential( nn.Linear(input_size + hidden_size, hidden_size), nn.Tanh() )