我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用torch.nn.LSTMCell()。
def __init__(self, dictionary, encoder_embed_dim=512, embed_dim=512, out_embed_dim=512, num_layers=1, dropout_in=0.1, dropout_out=0.1, attention=True): super().__init__() self.dictionary = dictionary self.dropout_in = dropout_in self.dropout_out = dropout_out num_embeddings = len(dictionary) padding_idx = dictionary.pad() self.embed_tokens = Embedding(num_embeddings, embed_dim, padding_idx) self.layers = nn.ModuleList([ LSTMCell(encoder_embed_dim + embed_dim if layer == 0 else embed_dim, embed_dim) for layer in range(num_layers) ]) self.attention = AttentionLayer(encoder_embed_dim, embed_dim) if embed_dim != out_embed_dim: self.additional_fc = Linear(embed_dim, out_embed_dim) self.fc_out = Linear(out_embed_dim, num_embeddings, dropout=dropout_out)
def __init__(self, num_inputs, action_space, small_net=False): """ Really I should be using inheritance for the small_net here """ super(ES, self).__init__() num_outputs = action_space.n self.small_net = small_net if self.small_net: self.linear1 = nn.Linear(num_inputs, 64) self.linear2 = nn.Linear(64, 64) self.actor_linear = nn.Linear(64, num_outputs) else: self.conv1 = nn.Conv2d(num_inputs, 32, 3, stride=2, padding=1) self.conv2 = nn.Conv2d(32, 32, 3, stride=2, padding=1) self.conv3 = nn.Conv2d(32, 32, 3, stride=2, padding=1) self.conv4 = nn.Conv2d(32, 32, 3, stride=2, padding=1) self.lstm = nn.LSTMCell(32*3*3, 256) self.actor_linear = nn.Linear(256, num_outputs) self.train()
def __init__(self, input_size, hidden_size, vocab_size, wordEmbed): super(PointerAttentionDecoder, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.vocab_size = vocab_size self.word_embed = wordEmbed #self.decoderRNN = LSTMCell(self.input_size, self.hidden_size) self.decoderRNN = LSTM(self.input_size, self.hidden_size, batch_first=True) #params for attention self.Wh = Linear(2 * self.hidden_size, 2*self. hidden_size) self.Ws = Linear(self.hidden_size, 2*self.hidden_size) self.w_c = Linear(1, 2*self.hidden_size) self.v = Linear(2*self.hidden_size, 1) # parameters for p_gen self.w_h = Linear(2 * self.hidden_size, 1) # double due to concat of BiDi encoder states self.w_s = Linear(self.hidden_size, 1) self.w_x = Linear(self.input_size, 1) #params for output proj self.V = Linear(self.hidden_size * 3, self.vocab_size) self.min_length = 40
def forward(self, input, mask=None, hx=None): batch_size = input.size(0) if self.batch_first else input.size(1) lstm = self.Cell is nn.LSTMCell if hx is None: num_directions = 2 if self.bidirectional else 1 hx = torch.autograd.Variable(input.data.new(self.num_layers * num_directions, batch_size, self.hidden_size).zero_()) if lstm: hx = (hx, hx) func = AutogradMaskedRNN(num_layers=self.num_layers, batch_first=self.batch_first, dropout=self.dropout, train=self.training, bidirectional=self.bidirectional, lstm=lstm) output, hidden = func(input, self.all_cells, hx, None if mask is None else mask.view(mask.size() + (1, ))) return output, hidden
def __init__(self, args): super(ACERCnnDisModel, self).__init__(args) # build model # 0. feature layers self.conv1 = nn.Conv2d(self.input_dims[0], 32, kernel_size=3, stride=2) # NOTE: for pkg="atari" self.rl1 = nn.ReLU() self.conv2 = nn.Conv2d(32, 32, kernel_size=3, stride=2, padding=1) self.rl2 = nn.ReLU() self.conv3 = nn.Conv2d(32, 32, kernel_size=3, stride=2, padding=1) self.rl3 = nn.ReLU() self.conv4 = nn.Conv2d(32, 32, kernel_size=3, stride=2, padding=1) self.rl4 = nn.ReLU() if self.enable_lstm: self.lstm = nn.LSTMCell(3*3*32, self.hidden_dim) # 1. actor: /pi_{/theta}(a_t | x_t) self.actor_5 = nn.Linear(self.hidden_dim, self.output_dims) self.actor_6 = nn.Softmax() # 2. critic: Q_{/theta_v}(x_t, a_t) self.critic_5 = nn.Linear(self.hidden_dim, self.output_dims) self._reset()
def __init__(self, args): super(ACERMlpDisModel, self).__init__(args) # build model # 0. feature layers self.fc1 = nn.Linear(self.input_dims[0] * self.input_dims[1], self.hidden_dim) self.rl1 = nn.ReLU() # lstm if self.enable_lstm: self.lstm = nn.LSTMCell(self.hidden_dim, self.hidden_dim) # 1. actor: /pi_{/theta}(a_t | x_t) self.actor_2 = nn.Linear(self.hidden_dim, self.output_dims) self.actor_3 = nn.Softmax() # 2. critic: Q_{/theta_v}(x_t, a_t) self.critic_2 = nn.Linear(self.hidden_dim, self.output_dims) self._reset()
def __init__(self, args): super(A3CCnnDisModel, self).__init__(args) # build model # 0. feature layers self.conv1 = nn.Conv2d(self.input_dims[0], 32, kernel_size=3, stride=2) # NOTE: for pkg="atari" self.rl1 = nn.ReLU() self.conv2 = nn.Conv2d(32, 32, kernel_size=3, stride=2, padding=1) self.rl2 = nn.ReLU() self.conv3 = nn.Conv2d(32, 32, kernel_size=3, stride=2, padding=1) self.rl3 = nn.ReLU() self.conv4 = nn.Conv2d(32, 32, kernel_size=3, stride=2, padding=1) self.rl4 = nn.ReLU() if self.enable_lstm: self.lstm = nn.LSTMCell(3*3*32, self.hidden_dim) # 1. policy output self.policy_5 = nn.Linear(self.hidden_dim, self.output_dims) self.policy_6 = nn.Softmax() # 2. value output self.value_5 = nn.Linear(self.hidden_dim, 1) self._reset()
def __init__(self, num_inputs, action_space): super(ActorCritic, self).__init__() self.conv1 = nn.Conv2d(num_inputs, 32, 3, stride=2, padding=1) self.conv2 = nn.Conv2d(32, 32, 3, stride=2, padding=1) self.conv3 = nn.Conv2d(32, 32, 3, stride=2, padding=1) self.conv4 = nn.Conv2d(32, 32, 3, stride=2, padding=1) self.lstm = nn.LSTMCell(32 * 3 * 3, 256) num_outputs = action_space.n self.critic_linear = nn.Linear(256, 1) self.actor_linear = nn.Linear(256, num_outputs) self.apply(weights_init) self.actor_linear.weight.data = normalized_columns_initializer( self.actor_linear.weight.data, 0.01) self.actor_linear.bias.data.fill_(0) self.critic_linear.weight.data = normalized_columns_initializer( self.critic_linear.weight.data, 1.0) self.critic_linear.bias.data.fill_(0) self.lstm.bias_ih.data.fill_(0) self.lstm.bias_hh.data.fill_(0) self.train()
def __init__(self, observation_space, action_space, hidden_size, sigma_init, no_noise): super(ActorCritic, self).__init__() self.no_noise = no_noise self.state_size = observation_space.shape[0] self.action_size = action_space.n self.relu = nn.ReLU(inplace=True) self.softmax = nn.Softmax(dim=1) self.fc1 = nn.Linear(self.state_size, hidden_size) self.lstm = nn.LSTMCell(hidden_size, hidden_size) if no_noise: self.fc_actor = nn.Linear(hidden_size, self.action_size) self.fc_critic = nn.Linear(hidden_size, 1) else: self.fc_actor = NoisyLinear(hidden_size, self.action_size, sigma_init=sigma_init) self.fc_critic = NoisyLinear(hidden_size, 1, sigma_init=sigma_init)
def run_lstm_fusion(self, use_cuda): def to_type(x): x = x.float() if use_cuda: x = x.cuda() return x def rand_v(a, b): return Variable(to_type(torch.randn(a, b))) input = rand_v(3, 10) hx = rand_v(3, 20) cx = rand_v(3, 20) module = to_type(nn.LSTMCell(10, 20)) # Just to allocate weights with correct sizes CompiledLSTMCell = torch.jit.compile(nderivs=0)(LSTMCell) z = CompiledLSTMCell(input, (hx, cx), *module.parameters()) with self.assertCompiled(CompiledLSTMCell): z2 = CompiledLSTMCell(input, (hx, cx), *module.parameters()) self.assertEqual(z, z2)
def __init__(self,T,A,B,z_size,N,dec_size,enc_size): super(DrawModel,self).__init__() self.T = T # self.batch_size = batch_size self.A = A self.B = B self.z_size = z_size self.N = N self.dec_size = dec_size self.enc_size = enc_size self.cs = [0] * T self.logsigmas,self.sigmas,self.mus = [0] * T,[0] * T,[0] * T self.encoder = nn.LSTMCell(2 * N * N + dec_size, enc_size) self.encoder_gru = nn.GRUCell(2 * N * N + dec_size, enc_size) self.mu_linear = nn.Linear(dec_size, z_size) self.sigma_linear = nn.Linear(dec_size, z_size) self.decoder = nn.LSTMCell(z_size,dec_size) self.decoder_gru = nn.GRUCell(z_size,dec_size) self.dec_linear = nn.Linear(dec_size,5) self.dec_w_linear = nn.Linear(dec_size,N*N) self.sigmoid = nn.Sigmoid()
def __init__(self, params): self.parent = super(Answerer, self); # input-output for current bot params['inVocabSize'] = params['aInVocab']; params['outVocabSize'] = params['aOutVocab']; self.parent.__init__(params); # number of attribute values numAttrs = sum([len(ii) for ii in self.props.values()]); # number of unique attributes numUniqAttr = len(self.props); # rnn inputSize rnnInputSize = numUniqAttr * self.imgFeatSize + self.embedSize; self.imgNet = nn.Embedding(numAttrs, self.imgFeatSize); self.rnn = nn.LSTMCell(rnnInputSize, self.hiddenSize); initializeWeights([self.rnn, self.imgNet], 'xavier'); # set offset self.listenOffset = params['qOutVocab']; # Embedding the image
def __init__(self, params): self.parent = super(Questioner, self); # input-output for current bot params['inVocabSize'] = params['qInVocab']; params['outVocabSize'] = params['qOutVocab']; self.parent.__init__(params); # always condition on task #self.rnn = nn.LSTMCell(2*self.embedSize, self.hiddenSize); self.rnn = nn.LSTMCell(self.embedSize, self.hiddenSize); # additional prediction network # start token included numPreds = sum([len(ii) for ii in self.props.values()]); # network for predicting self.predictRNN = nn.LSTMCell(self.embedSize, self.hiddenSize); self.predictNet = nn.Linear(self.hiddenSize, numPreds); initializeWeights([self.predictNet, self.predictRNN, self.rnn], 'xavier'); # setting offset self.taskOffset = params['aOutVocab'] + params['qOutVocab']; self.listenOffset = params['aOutVocab']; # make a guess the given image
def __init__(self, state_size, layer_sizes=[128, 128], dropout=0.0): super(LSTMDiscreteFeatures, self).__init__() if dropout != 0.0: raise Exception('Dropout not supported yet.') self.lstm = nn.LSTMCell(state_size, layer_sizes[0], bias=False)
def __init__(self, state_size, layer_sizes=[128, 128], dropout=0.0): super(LSTMContinuousFeatures, self).__init__() if dropout != 0.0: raise Exception('Dropout not supported yet.') self.lstm = nn.LSTMCell(state_size, layer_sizes[0], bias=False) self.lstm.weight_hh.data *= 0.1 self.lstm.weight_ih.data *= 0.1
def __init__(self, input_size, hidden_size, num_layers, num_classes): super(RNN_LSTM, self).__init__() self.hidden_size = hidden_size self.num_layers = num_layers self.num_classes = num_classes self.lstm1 = nn.LSTMCell(input_size, hidden_size) self.fc = nn.Linear(hidden_size, num_classes)
def test_LSTM_cell(self): # this is just a smoke test; these modules are implemented through # autograd so no Jacobian test is needed for bias in (True, False): input = Variable(torch.randn(3, 10)) hx = Variable(torch.randn(3, 20)) cx = Variable(torch.randn(3, 20)) lstm = nn.LSTMCell(10, 20, bias=bias) for i in range(6): hx, cx = lstm(input, (hx, cx)) (hx+cx).sum().backward()
def __init__(self, observation_space, non_rgb_rgb_state_size, action_space, hidden_size): super(ActorCritic, self).__init__() self.rgb_state_size = (6, 128, 128) self.action_size = 5 self.relu = nn.ReLU(inplace=True) self.softmax = nn.Softmax() # the archtecture is adapted from Sim2Real (Rusu et. al., 2016) self.conv1 = nn.Conv2d( self.rgb_state_size[0], 16, 8, stride=4, padding=1) self.conv2 = nn.Conv2d(16, 32, 5, stride=2) self.fc1 = nn.Linear(1152 + non_rgb_rgb_state_size, hidden_size) self.lstm = nn.LSTMCell(hidden_size, hidden_size) self.fc_actor1 = nn.Linear(hidden_size, self.action_size) self.fc_actor2 = nn.Linear(hidden_size, self.action_size) self.fc_actor3 = nn.Linear(hidden_size, self.action_size) self.fc_actor4 = nn.Linear(hidden_size, self.action_size) self.fc_actor5 = nn.Linear(hidden_size, self.action_size) self.fc_actor6 = nn.Linear(hidden_size, self.action_size) self.fc_critic = nn.Linear(hidden_size, 1) # Orthogonal weight initialisation for name, p in self.named_parameters(): if 'weight' in name: init.orthogonal(p) elif 'bias' in name: init.constant(p, 0)
def __init__(self, dictionary, embed_dim=512, num_layers=1, dropout_in=0.1, dropout_out=0.1): super().__init__() self.dictionary = dictionary self.dropout_in = dropout_in self.dropout_out = dropout_out num_embeddings = len(dictionary) padding_idx = dictionary.pad() self.embed_tokens = Embedding(num_embeddings, embed_dim, padding_idx) self.layers = nn.ModuleList([ LSTMCell(embed_dim, embed_dim) for layer in range(num_layers) ])
def LSTMCell(input_dim, hidden_dim, **kwargs): m = nn.LSTMCell(input_dim, hidden_dim, **kwargs) for name, param in m.named_parameters(): if 'weight' in name or 'bias' in name: param.data.uniform_(-0.1, 0.1) return m
def __init__(self, embed_size, state_size): super().__init__() self.net = nn.LSTMCell(embed_size, state_size) xavier_init(self)
def __init__(self, embed_size, state_size, out_size): super().__init__() self.net_lstm = nn.LSTMCell(embed_size, state_size) self.net_mlp = nn.Linear(state_size, out_size) self.softmax = nn.Softmax() xavier_init(self)
def __init__(self, size, tracker_size, predict): super(Tracker, self).__init__() self.rnn = nn.LSTMCell(3 * size, tracker_size) if predict: self.transition = nn.Linear(tracker_size, 4) self.state_size = tracker_size
def __init__(self, num_layers, input_size, rnn_size, dropout): super(StackedLSTM, self).__init__() self.dropout = nn.Dropout(dropout) self.num_layers = num_layers self.layers = nn.ModuleList() for i in range(num_layers): self.layers.append(nn.LSTMCell(input_size, rnn_size)) input_size = rnn_size
def __init__(self, num_glimpses: int=8, glimpse_h: int=8, glimpse_w: int=8, controller_out: int=128) -> None: super().__init__() self.num_glimpses = num_glimpses self.glimpse_h = glimpse_h self.glimpse_w = glimpse_w self.controller_out = controller_out # main modules of ARC self.controller = nn.LSTMCell(input_size=(glimpse_h * glimpse_w), hidden_size=self.controller_out) self.glimpser = nn.Linear(in_features=self.controller_out, out_features=3) # this will actually generate glimpses from images using the glimpse parameters. self.glimpse_window = GlimpseWindow(glimpse_h=self.glimpse_h, glimpse_w=self.glimpse_w)
def __init__(self, hidden_size): super(ActorCritic, self).__init__() self.state_size = STATE_SIZE[0] * STATE_SIZE[1] * STATE_SIZE[2] self.elu = nn.ELU(inplace=True) self.softmax = nn.Softmax() self.sigmoid = nn.Sigmoid() # Pass state into model body self.conv1 = nn.Conv2d(STATE_SIZE[0], 32, 4, stride=2) self.conv2 = nn.Conv2d(32, 32, 3) self.fc1 = nn.Linear(1152, hidden_size) # Pass previous action, reward and timestep directly into LSTM self.lstm = nn.LSTMCell(hidden_size + ACTION_SIZE + 2, hidden_size) self.fc_actor1 = nn.Linear(hidden_size, ACTION_SIZE) self.fc_critic1 = nn.Linear(hidden_size, ACTION_SIZE) self.fc_actor2 = nn.Linear(hidden_size, ACTION_SIZE) self.fc_critic2 = nn.Linear(hidden_size, ACTION_SIZE) self.fc_class = nn.Linear(hidden_size, 1) # Orthogonal weight initialisation for name, p in self.named_parameters(): if 'weight' in name: init.orthogonal(p) elif 'bias' in name: init.constant(p, 0) # Set LSTM forget gate bias to 1 for name, p in self.lstm.named_parameters(): if 'bias' in name: n = p.size(0) forget_start_idx, forget_end_idx = n // 4, n // 2 init.constant(p[forget_start_idx:forget_end_idx], 1)
def __init__(self, num_inputs, action_space): super(ActorCritic, self).__init__() self.linear1 = nn.Linear(num_inputs, 256) self.lstm = nn.LSTMCell(256, 256) num_outputs = action_space.n self.critic_linear = nn.Linear(256, 1) self.actor_linear = nn.Linear(256, num_outputs) self.apply(weights_init) self.train()
def test_LSTM_cell(self): # this is just a smoke test; these modules are implemented through # autograd so no Jacobian test is needed for bias in (True, False): input = Variable(torch.randn(3, 10)) hx = Variable(torch.randn(3, 20)) cx = Variable(torch.randn(3, 20)) lstm = nn.LSTMCell(10, 20, bias=bias) for i in range(6): hx, cx = lstm(input, (hx, cx)) (hx + cx).sum().backward()
def __init__(self, word_dim, hidden_dim, use_leaf_rnn, intra_attention, gumbel_temperature, bidirectional): super(BinaryTreeLSTM, self).__init__() self.word_dim = word_dim self.hidden_dim = hidden_dim self.use_leaf_rnn = use_leaf_rnn self.intra_attention = intra_attention self.gumbel_temperature = gumbel_temperature self.bidirectional = bidirectional assert not (self.bidirectional and not self.use_leaf_rnn) if use_leaf_rnn: self.leaf_rnn_cell = nn.LSTMCell( input_size=word_dim, hidden_size=hidden_dim) if bidirectional: self.leaf_rnn_cell_bw = nn.LSTMCell( input_size=word_dim, hidden_size=hidden_dim) else: self.word_linear = nn.Linear(in_features=word_dim, out_features=2 * hidden_dim) if self.bidirectional: self.treelstm_layer = BinaryTreeLSTMLayer(2 * hidden_dim) self.comp_query = nn.Parameter(torch.FloatTensor(2 * hidden_dim)) else: self.treelstm_layer = BinaryTreeLSTMLayer(hidden_dim) self.comp_query = nn.Parameter(torch.FloatTensor(hidden_dim)) self.reset_parameters()
def __init__(self, num_channel_input, num_output): super(ActorCriticNet, self).__init__() self.conv1 = nn.Conv2d(num_channel_input, 32, 3, stride=2, padding=1) self.conv2 = nn.Conv2d(32, 32, 3, stride=2, padding=1) self.conv3 = nn.Conv2d(32, 32, 3, stride=2, padding=1) self.conv4 = nn.Conv2d(32, 32, 3, stride=2, padding=1) self.lstm = nn.LSTMCell(32 * 3 * 3, 256) self.critic_fc = nn.Linear(256, 1) self.actor_fc = nn.Linear(256, num_output) self.softmax = nn.Softmax()
def __init__(self, input_size, hidden_size, num_layers=1, dropout=0, bias=True, rnn_cell=nn.LSTMCell, residual=False): super(StackedCell, self).__init__() self.dropout = nn.Dropout(dropout) self.num_layers = num_layers self.hidden_size = hidden_size self.residual = residual self.layers = nn.ModuleList() for _ in range(num_layers): self.layers.append(rnn_cell(input_size, hidden_size, bias=bias)) input_size = hidden_size
def __init__(self, input_size, hidden_size, attention_layer, num_layers=1, dropout=0, bias=True, rnn_cell=nn.LSTMCell, residual=False): super(StackedsAttentionCell, self).__init__(input_size, hidden_size, num_layers, dropout, bias, rnn_cell, residual) self.attention = attention_layer
def __init__(self, num_inputs, action_space): super(ActorCritic, self).__init__() self.linear1 = nn.Linear(num_inputs, 200) self.lstm = nn.LSTMCell(200, 128) num_outputs = action_space.shape[0] # Actor self.mu_linear = nn.Linear(128, num_outputs) self.sigma_sq_linear = nn.Linear(128, num_outputs) # Critic self.value_linear = nn.Linear(128, 1) # initialize weight self.apply(weights_init) self.mu_linear.weight.data = normalized_columns_initializer( self.mu_linear.weight.data, 0.01) self.sigma_sq_linear.weight.data = normalized_columns_initializer( self.sigma_sq_linear.weight.data, 0.01) self.mu_linear.bias.data.fill_(0) self.sigma_sq_linear.bias.data.fill_(0) self.value_linear.weight.data = normalized_columns_initializer( self.value_linear.weight.data, 1.0) self.value_linear.bias.data.fill_(0) self.lstm.bias_ih.data.fill_(0) self.lstm.bias_hh.data.fill_(0) self.train()
def __init__(self, observation_space, action_space, hidden_size): super(ActorCritic, self).__init__() self.state_size = observation_space.shape[0] self.action_size = action_space.n self.relu = nn.ReLU(inplace=True) self.softmax = nn.Softmax(dim=1) self.fc1 = nn.Linear(self.state_size, hidden_size) self.lstm = nn.LSTMCell(hidden_size, hidden_size) self.fc_actor = nn.Linear(hidden_size, self.action_size) self.fc_critic = nn.Linear(hidden_size, self.action_size)
def __init__(self, args, infer=False): ''' Initializer function params: args : Training arguments infer : Training or test time (True at test time) ''' super(HumanNodeRNN, self).__init__() self.args = args self.infer = infer # Store required sizes self.rnn_size = args.human_node_rnn_size self.output_size = args.human_node_output_size self.embedding_size = args.human_node_embedding_size self.input_size = args.human_node_input_size self.edge_rnn_size = args.human_human_edge_rnn_size # Linear layer to embed input self.encoder_linear = nn.Linear(self.input_size, self.embedding_size) # ReLU and Dropout layers self.relu = nn.ReLU() self.dropout = nn.Dropout(args.dropout) # Linear layer to embed edgeRNN hidden states self.edge_embed = nn.Linear(self.edge_rnn_size, self.embedding_size) # Linear layer to embed attention module output self.edge_attention_embed = nn.Linear(self.edge_rnn_size*2, self.embedding_size) # The LSTM cell self.cell = nn.LSTMCell(2*self.embedding_size, self.rnn_size) # Output linear layer self.output_linear = nn.Linear(self.rnn_size, self.output_size)
def __init__(self, args, infer=False): ''' Initializer function params: args : Training arguments infer : Training or test time (True at test time) ''' super(HumanHumanEdgeRNN, self).__init__() self.args = args self.infer = infer # Store required sizes self.rnn_size = args.human_human_edge_rnn_size self.embedding_size = args.human_human_edge_embedding_size self.input_size = args.human_human_edge_input_size # Linear layer to embed input self.encoder_linear = nn.Linear(self.input_size, self.embedding_size) # ReLU and Dropout layers self.relu = nn.ReLU() self.dropout = nn.Dropout(args.dropout) # The LSTM cell self.cell = nn.LSTMCell(self.embedding_size, self.rnn_size)
def test_lstm_fusion(self): input = Variable(torch.randn(3, 10).cuda()) hx = Variable(torch.randn(3, 20).cuda()) cx = Variable(torch.randn(3, 20).cuda()) module = nn.LSTMCell(10, 20).cuda() # Just to allocate weights with correct sizes def LSTMCell(input, hidden, w_ih, w_hh, b_ih=None, b_hh=None): hx, cx = hidden gates = F.linear(input, w_ih, b_ih) + F.linear(hx, w_hh, b_hh) ingate, forgetgate, cellgate, outgate = gates.chunk(4, 1) ingate = F.sigmoid(ingate) forgetgate = F.sigmoid(forgetgate) cellgate = F.tanh(cellgate) outgate = F.sigmoid(outgate) cy = (forgetgate * cx) + (ingate * cellgate) hy = outgate * F.tanh(cy) return hy, cy trace, _ = torch.jit.trace(LSTMCell, (input, (hx, cx)) + tuple(module.parameters())) torch._C._jit_pass_lint(trace) torch._C._jit_pass_onnx(trace) torch._C._jit_pass_lint(trace) torch._C._jit_pass_fuse(trace) torch._C._jit_pass_lint(trace) self.assertExpected(str(trace))
def test_traced_module(self): input = Variable(torch.randn(3, 10)) hx = Variable(torch.randn(3, 20)) cx = Variable(torch.randn(3, 20)) @torch.jit.compile(verify=True) class MyLSTMCell(nn.LSTMCell): pass lstm = MyLSTMCell(10, 20) out = lstm(input, (hx, cx)) out2 = lstm(input, (hx, cx)) self.assertEqual(out, out2)
def __init__(self, num_points = 2500): super(PointGenR2, self).__init__() self.decoder = nn.Sequential( nn.Linear(100, 256), nn.ReLU(), nn.Linear(256, 512), nn.ReLU(), nn.Linear(512, 1024), nn.ReLU(), nn.Linear(1024, 500 * 3), nn.Tanh(), ) self.lstmcell = nn.LSTMCell(input_size = 100, hidden_size= 100) self.encoder = nn.Sequential( PointNetfeat(num_points = 500), ) self.encoder2 = nn.Sequential( nn.BatchNorm1d(1024), nn.Linear(1024, 512), nn.BatchNorm1d(512), nn.ReLU(), nn.Linear(512, 100), )
def __init__(self, num_points = 2500): super(PointGenR3, self).__init__() def get_decoder(): return nn.Sequential( nn.Linear(200, 256), nn.ReLU(), nn.Linear(256, 512), nn.ReLU(), nn.Linear(512, 1024), nn.ReLU(), nn.Linear(1024, 500 * 3), nn.Tanh(), ) self.decoder = get_decoder() self.lstmcell = nn.LSTMCell(input_size = 100, hidden_size= 100) self.encoder = nn.Sequential( PointNetfeat(num_points = 500), ) self.encoder2 = nn.Sequential( nn.BatchNorm1d(1024), nn.Linear(1024, 512), nn.BatchNorm1d(512), nn.ReLU(), nn.Linear(512, 100), )
def __init__(self): super(Net, self).__init__() self.nhid=256 self.nhid0=256 self.rnn0 = nn.LSTMCell(256,256) #self.downsample=nn.Conv2d(3, 3, kernel_size=2,stride=2) self.conv1 = nn.Conv2d(3, 16, kernel_size=5,padding=2) self.conv11 = nn.Conv2d(16, 32, kernel_size=5,padding=2) self.convo1 = nn.Conv2d(32, 4, kernel_size=1) #self.conv12 = nn.Conv2d(32, 64, kernel_size=5,padding=2) # self.rnn0 = nn.RNNCell(1024,1024, nonlinearity='relu') self.conv21 = nn.Conv2d(3,8, kernel_size=5,padding=2) #self.convo1 = nn.Conv2d(32, 16, kernel_size=1) self.conv22 = nn.Conv2d(8, 16, kernel_size=5,padding=2) self.conv23 = nn.Conv2d(16, 16, kernel_size=3,padding=1) self.rnn1 = nn.LSTMCell(256,256) # self.convo3 = nn.Conv2d(32, 16, kernel_size=1) self.BN1 =nn.BatchNorm2d(8) self.BN2 =nn.BatchNorm2d(16) self.BN22 =nn.BatchNorm2d(16) self.BN3 =nn.BatchNorm2d(32) self.BN02 =nn.BatchNorm2d(16) self.BN03 =nn.BatchNorm2d(32) self.BN4=nn.BatchNorm2d(64) self.BN5 =nn.BatchNorm2d(128) self.BN6 =nn.BatchNorm1d(256) self.BN0 =nn.BatchNorm1d(1024) self.fc1 = nn.Linear(256, 1024) self.fc2 = nn.Linear(2, 256) self.fc3 = nn.Linear(256, 10) self.fc4 = nn.Linear(192, 256) self.fc5 = nn.Linear(512, 256) # self.fc1 = nn.Linear(400, 120) # self.BN3 =nn.BatchNorm1d(120) # self.fc2 = nn.Linear(120, 84) # self.BN4 =nn.BatchNorm1d(84) # self.fc3 = nn.Linear(84, 10)
def __init__(self, args, infer=False): ''' Initializer function params: args: Training arguments infer: Training or test time (true if test time) ''' super(SocialLSTM, self).__init__() self.args = args self.infer = infer if infer: # Test time self.seq_length = 1 else: # Training time self.seq_length = args.seq_length # Store required sizes self.rnn_size = args.rnn_size self.grid_size = args.grid_size self.embedding_size = args.embedding_size self.input_size = args.input_size self.output_size = args.output_size # The LSTM cell self.cell = nn.LSTMCell(2*self.embedding_size, self.rnn_size) # Linear layer to embed the input position self.input_embedding_layer = nn.Linear(self.input_size, self.embedding_size) # Linear layer to embed the social tensor self.tensor_embedding_layer = nn.Linear(self.grid_size*self.grid_size*self.rnn_size, self.embedding_size) # Linear layer to map the hidden state of LSTM to output self.output_layer = nn.Linear(self.rnn_size, self.output_size) # ReLU and dropout unit self.relu = nn.ReLU() self.dropout = nn.Dropout(args.dropout)