Exemplo n.º 1
0
 def forward(self, x):
     y = F.dropout(F.relu(self.linears[0](x)), self.training)
     for layer in self.linears[1:-1]:
         y = F.relu(layer(y))
         y = F.dropout(y, self.training)
     y = F.log_softmax(self.linears[-1](y))
     return y
Exemplo n.º 2
0
 def forward(self, x):
     x = F.relu(self.fc1(x))
     x = F.relu(self.fc_1(x))
     x = F.relu(self.fc_2(x))
     x = F.relu(self.fc_3(x))
     y = self.fc2(x)
     return y
Exemplo n.º 3
0
	def forward(self, v, u, d):
		"""
		@param v [batch_size, embedding_size] matrix to push
		@param u [batch_size,] vector of pop signals in (0, 1)
		@param d [batch_size,] vector of push signals in (0, 1)
		@return [batch_size, embedding_size] read matrix
		"""

		# update V, which is of size [t, bach_size, embedding_size]
		v = v.view(1, self.batch_size, self.embedding_size)
		self.V = torch.cat([self.V, v], 0) if len(self.V.data) != 0 else v

		# TODO initialize queue to fixed size

		# update s, which is of size [t, batch_size]
		old_t = self.s.size(0) if self.s.size() else 0
		s = Variable(torch.FloatTensor(old_t + 1, self.batch_size))
		w = u
		for i in xrange(old_t):
			s_ = F.relu(self.s[i,:] - w)
			w = F.relu(w - self.s[i,:])
			s[i,:] = s_
			# if len(torch.nonzero(w.data)) == 0: break
			# TODO does this if work properly now?
		s[old_t,:] = d
		self.s = s

		# calculate r, which is of size [batch_size, embedding_size]
		r = Variable(torch.zeros([self.batch_size, self.embedding_size]))
		for i in xrange(old_t + 1):
			used = torch.sum(self.s[:i,:], 0) if i > 0 else self.zero
			coeffs = torch.min(self.s[i,:], F.relu(1 - used))
			# reformating coeffs into a matrix that can be multiplied element-wise
			r += coeffs.view(self.batch_size, 1).repeat(1, self.embedding_size) * self.V[i,:,:]
		return r
Exemplo n.º 4
0
    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = self.bn_conv1(x)
        x = self.pool(F.relu(self.conv2(x)))
        x = self.bn_conv2(x)

        x = F.relu(self.conv3(x))
        x = self.bn_conv3(x)
        x = self.pool(F.relu(self.conv4(x)))
        x = self.bn_conv4(x)
        
        '''
        x = self.cpdfc1(x)
        x = self.bn_2(x)
        x = self.cpdfc2(x)
        x = self.bn_2(x)
        x = self.cpdfc3(x)
        x = self.bn_2(x)
        x = F.relu(self.cpdfc4(x))
        '''
        x = F.relu(self.conv2fc1(x))
        x = self.bn_4(x)
        x = self.conv2fc2(x)
        x = self.bn_5(x)

        x = x.view(-1, self.num_classes)  # Flatten! <---
        return x
Exemplo n.º 5
0
    def forward(self, x, y, y_mask):
        """Input shapes:
            x = batch * len1 * h
            y = batch * len2 * h
            y_mask = batch * len2
        Output shapes:
            matched_seq = batch * len1 * h
        """
        # Project vectors
        if self.linear:
            x_proj = self.linear(x.view(-1, x.size(2))).view(x.size())
            x_proj = F.relu(x_proj)
            y_proj = self.linear(y.view(-1, y.size(2))).view(y.size())
            y_proj = F.relu(y_proj)
        else:
            x_proj = x
            y_proj = y

        # Compute scores
        scores = x_proj.bmm(y_proj.transpose(2, 1))

        # Mask padding
        y_mask = y_mask.unsqueeze(1).expand(scores.size())
        scores.data.masked_fill_(y_mask.data, -float('inf'))

        # Normalize with softmax
        alpha_flat = F.softmax(scores.view(-1, y.size(1)))
        alpha = alpha_flat.view(-1, x.size(1), y.size(1))

        # Take weighted average
        matched_seq = alpha.bmm(y)
        return matched_seq
Exemplo n.º 6
0
    def forward(self, doc_hiddens, question_hiddens):
        '''

        :param doc_hiddens: batch * input_len * in_channels
        :param question_hiddens: batch * input_len * in_channels
        :return: batch * output_size
        '''
        # Concatenate all available relations
        num_doc_objects = doc_hiddens.size(1)
        num_question_objects = question_hiddens.size(1)
        num_total_objects = num_doc_objects * num_question_objects
        doc = doc_hiddens.unsqueeze(1)
        doc = doc.repeat(1, num_question_objects, 1, 1)
        q = question_hiddens.unsqueeze(2).repeat(1, 1, num_doc_objects, 1)
        relations = torch.cat([doc, q], 3)
        #print('relations       :', relations.size())
        x_r = relations.view(-1,doc.size(3)+q.size(3))
        #print('x_r:',x_r.size())
        #res1 = x_r.clone()
        #print(self.g_fc1)
        x_r = F.relu((self.g_fc1(x_r)))# + res1
        x_r = self.ln1.forward(x_r)
        #res2 = x_r.clone()
        #x_r = F.relu((self.g_fc2(x_r)))# + res2
        #x_r = self.ln2.forward(x_r)
        #res3 = x_r.clone()
        #x_r = F.relu((self.g_fc3(x_r))) + res3

        x_r = F.relu((self.g_fc4(x_r)))
        x_r = self.ln4.forward(x_r)

        x_g = x_r.view(relations.size(0), relations.size(1) * relations.size(2), -1)
        x_g = x_g.sum(1).squeeze(1)/num_total_objects

        return x_g
Exemplo n.º 7
0
 def forward(self, x):
     x = F.relu(F.max_pool2d(self.conv1(x), 2))
     x = F.relu(F.max_pool2d(self.conv2(x), 2))
     x = x.view(-1, 320)
     x = F.relu(self.fc1(x))
     x = self.fc2(x)
     return x
Exemplo n.º 8
0
 def forward(self, x):
     out = F.relu(self.bn1(x))
     shortcut = self.shortcut(out) if hasattr(self, 'shortcut') else x
     out = self.conv1(out)
     out = self.conv2(F.relu(self.bn2(out)))
     out += shortcut
     return out
 def forward(self, x):
     x = self.fc1(x).view(-1, self.channels, self.rows, self.rows)
     x = F.relu(self.batch_norm1(x))
     x = F.relu(self.batch_norm2(self.conv1(x)))
     x = F.relu(self.batch_norm3(self.conv2(x)))
     x = F.relu(self.batch_norm4(self.conv3(x)))
     return F.tanh(self.conv4(x))
 def forward(self, x):
     x = x.view(-1, 784)  # Flatten the data (n, 1, 28, 28)-> (n, 784)
     x = F.relu(self.l1(x))
     x = F.relu(self.l2(x))
     x = F.relu(self.l3(x))
     x = F.relu(self.l4(x))
     return self.l5(x)
Exemplo n.º 11
0
 def forward(self, x):
     out = F.relu(self.bn1(self.conv1(x)))
     out = F.relu(self.bn2(self.conv2(out)))
     out = self.bn3(self.conv3(out))
     out += self.shortcut(x)
     out = F.relu(out)
     return out
Exemplo n.º 12
0
    def forward(self, x_left, x_right):
        x_left = F.relu(x_left)
        x_left = self.conv_left(x_left)
        x_left = self.bn_left(x_left)

        x_right = F.relu(x_right)
        x_right = self.conv_right(x_right)
        x_right = self.bn_right(x_right)

        x_comb_iter_0_left = self.comb_iter_0_left(x_right)
        x_comb_iter_0_right = self.comb_iter_0_right(x_left)
        x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right

        x_comb_iter_1_left = self.comb_iter_1_left(x_left)
        x_comb_iter_1_right = self.comb_iter_1_right(x_right)
        x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right

        x_comb_iter_2_left = self.comb_iter_2_left(x_right)
        x_comb_iter_2_right = self.comb_iter_2_right(x_left)
        x_comb_iter_2 = x_comb_iter_2_left + x_comb_iter_2_right

        x_comb_iter_3_left = self.comb_iter_3_left(x_comb_iter_0)
        x_comb_iter_3 = x_comb_iter_3_left + x_comb_iter_1

        x_comb_iter_4_left = self.comb_iter_4_left(x_comb_iter_0)
        x_comb_iter_4_right = self.comb_iter_4_right(x_right)
        x_comb_iter_4 = x_comb_iter_4_left + x_comb_iter_4_right

        return torch.cat([x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1)
Exemplo n.º 13
0
 def adpW(self,x):
     '''
        calculate the pairwise_att of everypair of inputs
        output_size: (x.size(0),x.size(1)/2)
     '''
     x = x.detach()
     x = self.adp_metric_embedding1(x)
     x = self.adp_metric_embedding1_bn(x)
     x = F.relu(x)
     x = self.adp_metric_embedding2(x)
     x = self.adp_metric_embedding2_bn(x)
     x = F.relu(x)
     x = self.adp_metric_embedding3(x)
     x = self.adp_metric_embedding3_bn(x)
     x = F.relu(x)
     pairwise_att = F.sigmoid(self.adp_metric_embedding4(x))
     # x = self.adp_metric_embedding2_bn(x)
     diag_matrix1 = []
     diag_matrix2 = []
     for i in range(x.size(0)):
         diag_matrix1.append(torch.diag(pairwise_att[i, :x.size(1)/2]))
     for i in range(x.size(0)):
         diag_matrix2.append(torch.diag(pairwise_att[i, x.size(1)/2:]))
     pairwise_att1 = torch.stack(diag_matrix1)
     pairwise_att2 = torch.stack(diag_matrix1)
     return pairwise_att1, pairwise_att2
Exemplo n.º 14
0
    def forward(self, x_left, x_right):
        x_left = F.relu(x_left)

        x_left_0 = self.pool_left_0(x_left)
        x_left_0 = self.conv_left_0(x_left_0)

        x_left_1 = self.pool_left_1(x_left) # TODO: strange padding + stride operation
        x_left_1 = self.conv_left_1(x_left_1)

        x_left = torch.cat([x_left_0, x_left_1], 1)
        x_left = self.bn_left(x_left)

        x_right = F.relu(x_right)
        x_right = self.conv_right(x_right)
        x_right = self.bn_right(x_right)

        x_comb_iter_0_left = self.comb_iter_0_left(x_right)
        x_comb_iter_0_right = self.comb_iter_0_right(x_left)
        x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right

        x_comb_iter_1_left = self.comb_iter_1_left(x_left)
        x_comb_iter_1_right = self.comb_iter_1_right(x_left)
        x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right

        x_comb_iter_2_left = self.comb_iter_2_left(x_right)
        x_comb_iter_2 = x_comb_iter_2_left + x_left

        x_comb_iter_3_left = self.comb_iter_3_left(x_left) # TODO: those two avgPool look similar
        x_comb_iter_3_right = self.comb_iter_3_right(x_left)
        x_comb_iter_3 = x_comb_iter_3_left + x_comb_iter_3_right

        x_comb_iter_4_left = self.comb_iter_4_left(x_right)
        x_comb_iter_4 = x_comb_iter_4_left + x_right

        return torch.cat([x_left, x_comb_iter_0, x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1)
Exemplo n.º 15
0
    def forward(self, x_left, x_right):
        x_left = F.relu(x_left)
        x_left = self.conv_left(x_left)
        x_left = self.bn_left(x_left)

        x_right = F.relu(x_right)
        x_right = self.conv_right(x_right)
        x_right = self.bn_right(x_right)

        x_comb_iter_0_left = self.comb_iter_0_left(x_right)
        x_comb_iter_0_right = self.comb_iter_0_right(x_left)
        x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right

        x_comb_iter_1_left = self.comb_iter_1_left(x_left)
        x_comb_iter_1_right = self.comb_iter_1_right(x_right)
        x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right

        x_comb_iter_2_left = self.comb_iter_2_left(x_right)
        x_comb_iter_2 = x_comb_iter_2_left + x_left

        x_comb_iter_3_left = self.comb_iter_3_left(x_left) # TODO: those two avgPool look similar
        x_comb_iter_3_right = self.comb_iter_3_right(x_left)
        x_comb_iter_3 = x_comb_iter_3_left + x_comb_iter_3_right

        x_comb_iter_4_left = self.comb_iter_4_left(x_right)
        x_comb_iter_4 = x_comb_iter_4_left + x_right

        return torch.cat([x_left, x_comb_iter_0, x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1)
    def forward(self, x):
        # print("aaaaa")
        x_no_static = self.embed_no_static(x)
        # x_no_static = self.dropout(x_no_static)
        x_static = self.embed_static(x)
        # fix the embedding
        # x_static = Variable(x_static.data)
        # x_static = self.dropout(x_static)
        x = torch.stack([x_static, x_no_static], 1)
        # x = x.unsqueeze(1) # (N,Ci,W,D)
        x = self.dropout(x)
        if self.args.batch_normalizations is True:
            x = [F.relu(self.convs1_bn(conv(x))).squeeze(3) for conv in self.convs1] #[(N,Co,W), ...]*len(Ks)
            x = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in x] #[(N,Co), ...]*len(Ks)
        else:
            x = [F.relu(conv(x)).squeeze(3) for conv in self.convs1] #[(N,Co,W), ...]*len(Ks)
            x = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in x]  # [(N,Co), ...]*len(Ks)
        x = torch.cat(x, 1)
        '''
        x1 = self.conv_and_pool(x,self.conv13) #(N,Co)
        x2 = self.conv_and_pool(x,self.conv14) #(N,Co)
        x3 = self.conv_and_pool(x,self.conv15) #(N,Co)
        x = torch.cat((x1, x2, x3), 1) # (N,len(Ks)*Co)
        '''
        x = self.dropout(x)  # (N,len(Ks)*Co)

        if self.args.batch_normalizations is True:
            x = self.fc1(x)
            logit = self.fc2(F.relu(x))
        else:
            x = self.fc1(x)
            logit = self.fc2(F.relu(x))
        return logit
Exemplo n.º 17
0
 def forward(self, x):
     x = F.relu(self.fc1(x))
     x = F.relu(self.fc11(x))
     x = F.relu(self.fc2(x))
     x = F.relu(self.fc3(x))
     x = F.tanh(self.out(x))
     return x
Exemplo n.º 18
0
	def forward(self, sequence, graph):
		"""
		Apply self-attention to the sequence, ignores
		the graph
		"""
		sequence = sequence.squeeze(1)	
		
		#get the dimension
		n, d = sequence.size()
		
		#project the sequence into key, value, and query sequences
		keySeq = f.relu(self.keyProj(sequence))
		valueSeq = f.relu(self.valueProj(sequence))
		querySeq = f.relu(self.queryProj(sequence))
		
		#combine query with each key
		#a_ijh = softmax( (q_ih^T k_jh) / sqrt(d) )
		#the result is, row i is the importance of the sequence for key i
		importance = f.softmax(t.matmul(querySeq, keySeq.permute(1,0)) * math.sqrt(d),0).permute(1,0)

		#apply the importance weights to the value sequence
		attention = t.matmul(valueSeq.permute(1,0), importance).permute(1,0)
	
		#sum the sequence for a complete representation
		final = t.sum(attention, 0)
		
		return attention.unsqueeze(1), final
    def forward(self, x):
        """
        In the forward function we accept a Variable of input data and we must return
        a Variable of output data. We can use Modules defined in the constructor as
        well as arbitrary operators on Variables.
        """

        x = self.first_conv_layer(x)
        x = self.second_conv_layer(x)
        x = self.third_conv_layer(x)
        x = self.fourth_conv_layer(x)
        x = self.fifth_conv_layer(x)
        x = self.last_conv_layer(x)
               
        # auxiliary classifier branch (for 9 colors)
        x = x.view(BATCH_SIZE, 4 * 4 * 10)
        x_aux = F.relu(self.fc_aux(x))
        class_out = nn.functional.softmax(x_aux)
        
        #print 'class_out =', class_out
        
        x_disc = F.relu(self.fc_disc(x))
        disc_out = nn.functional.sigmoid(x_disc)
        
        #print 'disc_out =', disc_out

        return disc_out, class_out
Exemplo n.º 20
0
 def forward(self, x):
     x = F.relu(self.linear1(x))
     x = F.dropout(x, 0.8)
     x = F.relu(self.linear2(x))
     x = F.dropout(x, 0.8)
     x = F.log_softmax(self.linear3(x))
     return x
 def forward(self, x):
     x = x.float()
     x = func.relu(self.fc1(x))
     x = func.relu(self.fc2(x))
     x = func.relu(self.fc3(x))
     x = self.fc4(x).double()
     return x
Exemplo n.º 22
0
 def forward(self, x):
     out = F.relu(F.max_pool2d(self.conv1(x), 2))
     out = F.relu(F.max_pool2d(self.conv2(out), 2))
     out = out.view(-1, 320)
     out = F.relu(self.fc1(out))
     out = self.fc2(out)
     return F.log_softmax(out, dim=1)
 def forward(self, x):
     x_no_static = self.embed_no_static(x)
     # x_no_static = self.dropout(x_no_static)
     x_static = self.embed_static(x)
     # fix the embedding
     x_static = Variable(x_static.data)
     # x_static = self.dropout(x_static)
     x = torch.stack([x_static, x_no_static], 1)
     one_layer = x  # (N,W,D) #  torch.Size([64, 43, 300])
     # print("one_layer {}".format(one_layer.size()))
     # one_layer = self.dropout(one_layer)
     # one_layer = one_layer.unsqueeze(1)  # (N,Ci,W,D)  #  torch.Size([64, 1, 43, 300])
     # one layer
     one_layer = [torch.transpose(F.relu(conv(one_layer)).squeeze(3), 1, 2).unsqueeze(1) for conv in self.convs1] # torch.Size([64, 100, 36])
     # one_layer = [F.relu(conv(one_layer)).squeeze(3).unsqueeze(1) for conv in self.convs1] # torch.Size([64, 100, 36])
     # print(one_layer[0].size())
     # print(one_layer[1].size())
     # two layer
     two_layer = [F.relu(conv(one_layer)).squeeze(3) for (conv, one_layer) in zip(self.convs2, one_layer)]
     # print("two_layer {}".format(two_layer[0].size()))
     # print("two_layer {}".format(two_layer[1].size()))
     # pooling
     output = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in two_layer]   #  torch.Size([64, 100]) torch.Size([64, 100])
     output = torch.cat(output, 1)  # torch.Size([64, 300])
     # dropout
     output = self.dropout(output)
     # linear
     output = self.fc1(output)
     logit = self.fc2(F.relu(output))
     return logit
Exemplo n.º 24
0
 def forward(self, x):
     x1_1 = F.relu(self.cpm1(x), inplace=True)
     x1_2 = F.relu(self.cpm2(x), inplace=True)
     x2_1 = F.relu(self.cpm3(x1_2), inplace=True)
     x2_2 = F.relu(self.cpm4(x1_2), inplace=True)
     x3_1 = F.relu(self.cpm5(x2_2), inplace=True)
     return torch.cat([x1_1, x2_1, x3_1] , 1)
Exemplo n.º 25
0
 def forward(self, x):
     out = F.relu(self.bn1(x))
     shortcut = self.shortcut(out)
     out = self.conv1(out)
     out = self.conv2(F.relu(self.bn2(out)))
     out += shortcut
     return out
Exemplo n.º 26
0
 def forward(self, word_inputs, feature_inputs, word_seq_lengths, char_inputs, char_seq_lengths, char_seq_recover):
     """
         input:
             word_inputs: (batch_size, sent_len)
             word_seq_lengths: list of batch_size, (batch_size,1)
             char_inputs: (batch_size*sent_len, word_length)
             char_seq_lengths: list of whole batch_size for char, (batch_size*sent_len, 1)
             char_seq_recover: variable which records the char order information, used to recover char order
         output:
             Variable(batch_size, sent_len, hidden_dim)
     """
     word_represent = self.wordrep(word_inputs,feature_inputs, word_seq_lengths, char_inputs, char_seq_lengths, char_seq_recover)
     ## word_embs (batch_size, seq_len, embed_size)
     if self.word_feature_extractor == "CNN":
         word_in = F.tanh(self.word2cnn(word_represent)).transpose(2,1).contiguous()
         for idx in range(self.cnn_layer):
             if idx == 0:
                 cnn_feature = F.relu(self.cnn_list[idx](word_in))
             else:
                 cnn_feature = F.relu(self.cnn_list[idx](cnn_feature))
             cnn_feature = self.cnn_drop_list[idx](cnn_feature)
             cnn_feature = self.cnn_batchnorm_list[idx](cnn_feature)
         feature_out = cnn_feature.transpose(2,1).contiguous()
     else:
         packed_words = pack_padded_sequence(word_represent, word_seq_lengths.cpu().numpy(), True)
         hidden = None
         lstm_out, hidden = self.lstm(packed_words, hidden)
         lstm_out, _ = pad_packed_sequence(lstm_out)
         ## lstm_out (seq_len, seq_len, hidden_size)
         feature_out = self.droplstm(lstm_out.transpose(1,0))
     ## feature_out (batch_size, seq_len, hidden_size)
     outputs = self.hidden2tag(feature_out)
     return outputs
Exemplo n.º 27
0
 def forward(self, x):
     x = F.relu(self.conv1(x))
     x = F.relu(self.conv2(x))
     before_pool = x
     if self.pooling:
         x = self.pool(x)
     return x, before_pool
Exemplo n.º 28
0
    def forward(self, x):
        # flatten input
        if len(x.size()) > 2:
            x = x.view(-1, int(np.prod(x.size()[1:])))
        # corrupt input
        x = self.input_corrupt(x)
        corrupted = x
        # encode
        for layer in self.encode_layers:
            x = layer(x)
            x = F.relu(x)
        # decode
        if self.tied_weights:
            for i, (layer, bias) in enumerate(self.decode_params):
                x = F.linear(x, weight=layer.weight.t(), bias=bias)
                if i == len(self.decode_params)-1:
                    x = self.visible_act(x)
                else:
                    x = F.relu(x)
        else:
            for i, layer in enumerate(self.decode_layers):
                x = layer(x)
                if i == len(self.decode_layers)-1:
                    x = self.visible_act(x)
                else:
                    x = F.relu(x)

        return x, corrupted
Exemplo n.º 29
0
    def forward(self, x):
        x = F.relu(self.bn1(self.conv1(x)), True)
        x = F.max_pool2d(self.conv2(x), 2)
        x = self.conv3(x)
        x = self.conv4(x)

        previous = x

        outputs = []
        for i in range(self.num_modules):
            hg = self._modules['m' + str(i)](previous)

            ll = hg
            ll = self._modules['top_m_' + str(i)](ll)

            ll = F.relu(self._modules['bn_end' + str(i)]
                        (self._modules['conv_last' + str(i)](ll)), True)

            # Predict heatmaps
            tmp_out = self._modules['l' + str(i)](ll)
            outputs.append(tmp_out)

            if i < self.num_modules - 1:
                ll = self._modules['bl' + str(i)](ll)
                tmp_out_ = self._modules['al' + str(i)](tmp_out)
                previous = previous + ll + tmp_out_

        return outputs
Exemplo n.º 30
0
 def forward(self, x):
     out = F.relu(self.bn1(self.conv1(x)))
     out = F.relu(self.bn2(self.conv2(out)))
     out = self.bn3(self.conv3(out))
     out += self.downsample(x)
     out = F.relu(out)
     return out
Exemplo n.º 31
0
 def forward(self, x):
     out = F.relu(self.bn1(self.conv1(x)))
     out = self.bn2(self.conv2(out))
     out += self.shortcut(x)
     out = F.relu(out)
     return out 
 def forward(self, x):
     x = x.view(-1, 84 * 84 * 3)
     x = F.relu((self.fc1(x)))
     x = F.relu(self.fc2(x))
     x = self.fc3(x)
     return F.log_softmax(x)
Exemplo n.º 33
0
                            lr=lr,
                            momentum=0.9,
                            nesterov=True)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=sch)

for epoch in range(epochs):
    attack_model.train()
    print("On Epoch, ", epoch)

    for x1, x2, m1, m2 in train_dl:

        # Forward pass
        y_pred = attack_model(x1, x2, m1, m2)
        #y_pred_above_0 = Functional.relu(y_pred)
        y_6 = 6 - y_pred
        y_less = Functional.relu(y_6)
        yp = 6 - y_less

        # Compute loss
        loss = -1 * torch.sum(yp)

        # Zero gradients, backward pass, update weights
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        print("running avg: ", (-1 * loss / bs))

        # Keep weights below barrier
        clip_params(attack_model, e)

# Save the trained model
Exemplo n.º 34
0
    def forward(self, obs: torch.Tensor) -> torch.Tensor:
        x = F.relu(self.linear1(obs))
        x = F.relu(self.linear2(x))
        x = torch.tanh(self.linear3(x))

        return x
 def forward(self, x):
     out = self.conv1(F.relu(self.bn1(x)))
     out = F.avg_pool2d(out, 2)
     return out
 def forward(self, x):
     out = self.conv1(F.relu(self.bn1(x)))
     out = torch.cat((x, out), 1)
     return out
Exemplo n.º 37
0
    def forward(self, sentence_1, sentence_2):
        matrix_x, size = DynamicPool.cal_similar_matrix(sentence_1, sentence_2)
        matrix_x.view(size, -1, 56, 56)
        matrix_x = F.relu(self.conv1(matrix_x))
        # 暂时用不到的动态pooling
        # origin_size1 = len(matrix_x[0][0])
        # origin_size2 = len(matrix_x[0][0][0])
        # # 填充卷积输出
        # # print(matrix_x)
        # while origin_size1 < self.target_pool[0]:
        #     matrix_x = torch.cat([matrix_x, matrix_x[:, :, :origin_size1, :]], dim=2)
        #     if len(matrix_x[0][0]) >= self.target_pool[0]:
        #         break
        #
        # while origin_size2 < self.target_pool[1]:
        #     matrix_x = torch.cat([matrix_x, matrix_x[:, :, :, :origin_size2]], dim=3)
        #     if len(matrix_x[0][0][0]) >= self.target_pool[1]:
        #         break
        #
        # dynamic_pool_size1 = len(matrix_x[0][0])
        # dynamic_pool_size2 = len(matrix_x[0][0][0])
        # get_index = DynamicPool(self.target_pool[0], self.target_pool[1], dynamic_pool_size1, dynamic_pool_size2)
        # index, pool_size = get_index.d_pool_index()
        # m, n, high_judge, weight_judge = get_index.cal(index)
        # stride = pool_size[0]
        # stride1 = pool_size[1]
        #
        # matrix_x1 = matrix_x[:, :, :m, :n]
        # matrix_x1 = F.max_pool2d(matrix_x1, (stride, stride1))
        #
        # if high_judge > 0:
        #     matrix_x2 = matrix_x[:, :, m:, :n]
        #     matrix_x2 = F.max_pool2d(matrix_x2, (stride + 1, stride1))
        # if weight_judge > 0:
        #     matrix_x3 = matrix_x[:, :, :m, n:]
        #     matrix_x3 = F.max_pool2d(matrix_x3, (stride, stride1 + 1))
        # if high_judge > 0 and weight_judge > 0:
        #     matrix_x4 = matrix_x[:, :, m:, n:]
        #     matrix_x4 = F.max_pool2d(matrix_x4, (stride + 1, stride1 + 1))
        #
        # if high_judge == 0 and weight_judge == 0:
        #     matrix_x = matrix_x1
        # elif high_judge > 0 and weight_judge == 0:
        #     matrix_x = torch.cat([matrix_x1, matrix_x2], dim=2)
        # elif high_judge == 0 and weight_judge > 0:
        #     matrix_x = torch.cat([matrix_x1, matrix_x3], dim=3)
        # else:
        #     matrix_x_1 = torch.cat([matrix_x1, matrix_x2], dim=2)
        #     matrix_x_2 = torch.cat([matrix_x3, matrix_x4], dim=2)
        #     matrix_x = torch.cat([matrix_x_1, matrix_x_2], dim=3)

        matrix_x = F.max_pool2d(matrix_x, (3, 3))
        need_matrix = matrix_x[:, 0, :, :].view(size, 1, CONV_TARGET,
                                                CONV_TARGET)
        mat2_1 = self.conv2_1(need_matrix)
        need_matrix = matrix_x[:, 1, :, :].view(size, 1, CONV_TARGET,
                                                CONV_TARGET)
        mat2_2 = self.conv2_1(need_matrix)
        need_matrix = matrix_x[:, 2, :, :].view(size, 1, CONV_TARGET,
                                                CONV_TARGET)
        mat2_3 = self.conv2_1(need_matrix)

        reshape_matrix = mat2_1 + mat2_2 + mat2_3

        reshape_matrix = self.dropout(reshape_matrix)
        reshape_matrix = F.max_pool2d(reshape_matrix, (2, 2))
        reshape_matrix = reshape_matrix.view(
            -1, self.num_flat_features(reshape_matrix))
        reshape_matrix = F.tanh(self.fc1(reshape_matrix))
        reshape_matrix = F.sigmoid(self.fc2(reshape_matrix))
        # matrix_x = self.fc3(matrix_x)
        reshape_matrix = reshape_matrix
        return reshape_matrix
Exemplo n.º 38
0
 def forward(self, x):
     x = self.conv(x)
     x = self.bn(x)
     return F.relu(x, inplace=True)
Exemplo n.º 39
0
 def forward(self, x):
     x = F.relu(self.conv(x), inplace=True)
     x = F.relu(self.deconv(x), inplace=True)
     return x
Exemplo n.º 40
0
 def forward(self, x):
     x = self.convs(x)  #convolutions maxpull pass
     x = F.relu(
         self.fc1(x))  #pass the convolution through fully connected linear
     x = self.fc2(x)  # bc this is our output layer. No activation here.
     return F.softmax(x, dim=1)
    def generate(self,
                 tensor_score,
                 tensor_metadata,
                 constraints_location,
                 temperature=1.):
        self.eval()
        tensor_score = to_cuda_variable(tensor_score)

        num_voices, chorale_length, num_metadatas = tensor_metadata.size()

        # generated chorale
        gen_chorale = self.dataset.empty_score_tensor(chorale_length)

        m = to_cuda_variable(tensor_metadata[None, :, :, :])
        m = self.embed_metadata(m, tensor_score[None, :, :],
                                constraints_location=constraints_location[None, :, :])

        output_constraints, activations_constraints = self.output_lstm_constraints(m)

        hidden = init_hidden_lstm(
            num_layers=self.num_layers,
            batch_size=1,
            lstm_hidden_size=self.num_lstm_generation_units
        )

        print(tensor_score)
        # 1 bar of start symbols
        # todo check
        for tick_index in range(self.dataset.num_voices
                                * 4
                                * self.dataset.subdivision - 1):
            voice_index = tick_index % self.dataset.num_voices
            # notes
            time_slice = gen_chorale[voice_index, 0]
            time_slice = torch.from_numpy(np.array([time_slice]))[None, :]
            note = self.note_embeddings[voice_index](
                to_cuda_variable(time_slice)
            )
            time_slice = note
            # concat with first metadata
            # todo wrong
            time_slice_cat = torch.cat(
                (time_slice, output_constraints[:,
                             tick_index + 1: tick_index + 2, :]
                 ), 2)

            (output_gen, hidden), activations_generation = lstm_with_activations(
                lstm_list=self.lstm_generation,
                input=time_slice_cat,
                hidden=hidden
            )
            h, c = hidden
            hidden = h[:, 0, :, :], c[:, 0, :, :]
        # generation:
        for tick_index in range(-1, chorale_length * num_voices - 1):
            voice_index = tick_index % num_voices
            time_index = (tick_index - voice_index) // num_voices
            next_voice_index = (tick_index + 1) % num_voices
            next_time_index = (tick_index + 1 - next_voice_index) // num_voices

            if tick_index == -1:
                last_start_symbol = 0  # gen_chorale[-1, 0]
                last_start_symbol = torch.from_numpy(np.array([last_start_symbol]))[None, :]
                time_slice = self.note_embeddings[-1](
                    to_cuda_variable(last_start_symbol)
                )
            else:
                time_slice = gen_chorale[voice_index, time_index]
                time_slice = torch.from_numpy(np.array([time_slice]))[None, :]
                note = self.note_embeddings[voice_index](
                    to_cuda_variable(time_slice)
                )
                time_slice = note

            time_slice_cat = torch.cat(
                (time_slice, output_constraints[:,
                             tick_index + 1: tick_index + 2, :]
                 ), 2
            )

            (output_gen, hidden), activations_generation = lstm_with_activations(
                lstm_list=self.lstm_generation,
                input=time_slice_cat, hidden=hidden)
            h, c = hidden
            hidden = h[:, 0, :, :], c[:, 0, :, :]

            weights = F.relu(self.linear_1(output_gen[:, 0, :]), inplace=True)
            weights = self.linear_ouput_notes[next_voice_index](weights)

            # compute predictions
            # temperature
            weights = weights * temperature
            preds = F.softmax(weights)

            # first batch element
            preds = to_numpy(preds[0])
            new_pitch_index = np.random.choice(np.arange(
                self.num_notes_per_voice[next_voice_index]
            ), p=preds)
            # new_pitch_index = np.argmax(preds)

            gen_chorale[next_voice_index, next_time_index] = int(new_pitch_index)

        # # show original chorale
        #score_original = self.dataset.tensor_to_score(
        #    tensor_score.cpu())
        #score_original.show()
        print(gen_chorale)
        score = self.dataset.tensor_to_score(gen_chorale)
        return score, gen_chorale, tensor_metadata
Exemplo n.º 42
0
 def stage2_pre(self, image):
     x = self.pool1_stage2(F.relu(self.conv1_stage2(image)))
     x = self.pool2_stage2(F.relu(self.conv2_stage2(x)))
     x = self.pool3_stage2(F.relu(self.conv3_stage2(x)))
     return x
 def forward(self, state, action):
     """Build a critic (value) network that maps (state, action) pairs -> Q-values."""
     xs = F.relu(self.fcs1(state))
     x = torch.cat((xs, action), dim=1)
     x = F.relu(self.fc2(x))
     return self.fc3(x)
Exemplo n.º 44
0
    def forward(self, image):
        """
        Forward propagation.

        :param image: images, a tensor of dimensions (N, 3, 300, 300)
        :return: lower-level feature maps conv4_3 and conv7
        """
        out = F.relu(self.conv1_1(image))  # (N, 64, 300, 300)
        out = F.relu(self.conv1_2(out))  # (N, 64, 300, 300)
        out = self.pool1(out)  # (N, 64, 150, 150)

        out = F.relu(self.conv2_1(out))  # (N, 128, 150, 150)
        out = F.relu(self.conv2_2(out))  # (N, 128, 150, 150)
        out = self.pool2(out)  # (N, 128, 75, 75)

        out = F.relu(self.conv3_1(out))  # (N, 256, 75, 75)
        out = F.relu(self.conv3_2(out))  # (N, 256, 75, 75)
        out = F.relu(self.conv3_3(out))  # (N, 256, 75, 75)
        out = self.pool3(out)  # (N, 256, 38, 38), it would have been 37 if not for ceil_mode = True

        out = F.relu(self.conv4_1(out))  # (N, 512, 38, 38)
        out = F.relu(self.conv4_2(out))  # (N, 512, 38, 38)
        out = F.relu(self.conv4_3(out))  # (N, 512, 38, 38)
        conv4_3_feats = out
        # g1 = out  # (N, 512, 38, 38)
        out = self.pool4(out)  # (N, 512, 19, 19)

        out = F.relu(self.conv5_1(out))  # (N, 512, 19, 19)
        out = F.relu(self.conv5_2(out))  # (N, 512, 19, 19)
        out = F.relu(self.conv5_3(out))  # (N, 512, 19, 19)
        out = self.pool5(out)  # (N, 512, 19, 19), pool5 does not reduce dimensions

        out = F.relu(self.conv6(out))  # (N, 1024, 19, 19)

        conv7_feats = F.relu(self.conv7(out))
        # g2 = F.relu(self.conv7(out))  # (N, 1024, 19, 19)


        # Lower-level feature maps
        return conv4_3_feats, conv7_feats
Exemplo n.º 45
0
	def forward(self, x):
		return self.w_2(self.dropout(F.relu(self.w_1(x))))
    def forward_inpaint(self, score_tensor: Variable, metadata_tensor: Variable, constraints_loc, start_tick, end_tick):
        batch_size, num_voices, chorale_length = score_tensor.size()
        sequence_length = num_voices * chorale_length
        gen_chorale = self.dataset.empty_score_tensor(chorale_length).unsqueeze(0)
        gen_chorale = gen_chorale.repeat(batch_size, 1, 1)
        gen_chorale[:, :, :start_tick] = score_tensor[:, :, :start_tick]
        gen_chorale[:, :, end_tick:] = score_tensor[:, :, end_tick:]
        # === embed as wrapped sequence ===
        # --- chorale
        x = self.embed_tensor_score(score_tensor)

        # --- metadata
        m = self.embed_metadata(
            metadata_tensor,
            score_tensor,
            constraints_location=constraints_loc
        )
        # === LSTM on constraints ===
        output_constraints, activations_constraint = self.output_lstm_constraints(m)
        hidden = init_hidden_lstm(
            num_layers=self.num_layers,
            batch_size=batch_size,
            lstm_hidden_size=self.num_lstm_generation_units
        )
        offset_seq = torch.cat(
            [to_cuda_variable(torch.zeros(batch_size, 1, self.note_embedding_dim)),
             x[:, :sequence_length - 1, :]
             ], 1)

        # todo dropout only on offset_seq?
        offset_seq = self.drop_input(offset_seq)
        input_seq = torch.cat([offset_seq, output_constraints], 2)
        past_input = input_seq[:, :start_tick, :]
        (_, hidden), activations_gen = lstm_with_activations(
            lstm_list=self.lstm_generation,
            input=past_input,
            hidden=hidden)
        h, c = hidden
        hidden = h[:, 0, :, :], c[:, 0, :, :]

        final_weights = [[] for i in range(num_voices)]
        for tick_index in range(start_tick-1, end_tick-1):
            voice_index = tick_index % num_voices
            time_index = (tick_index - voice_index) // num_voices
            next_voice_index = (tick_index + 1) % num_voices
            next_time_index = (tick_index + 1 - next_voice_index) // num_voices

            if tick_index == -1:
                last_start_symbol = 0  # gen_chorale[-1, 0]
                last_start_symbol = torch.from_numpy(np.array([last_start_symbol]))[None, :]
                time_slice = self.note_embeddings[-1](
                    to_cuda_variable(last_start_symbol)
                )
            else:
                time_slice = gen_chorale[:, voice_index:voice_index+1, time_index]
                # time_slice = torch.from_numpy(np.array([time_slice]))[None, :]
                note = self.note_embeddings[voice_index](
                    to_cuda_variable(time_slice)
                )
                time_slice = note

            time_slice_cat = torch.cat((time_slice, output_constraints[:, tick_index+1:tick_index+2, :]), 2)

            (output_gen, hidden), activations_generation = lstm_with_activations(
                lstm_list=self.lstm_generation,
                input=time_slice_cat, hidden=hidden)
            h, c = hidden
            hidden = h[:, 0, :, :], c[:, 0, :, :]

            weights = F.relu(self.linear_1(output_gen[:, 0, :]), inplace=True)
            weights = self.linear_ouput_notes[next_voice_index](weights)

            # compute predictions
            # temperature
            weights = weights
            preds = F.softmax(weights)
            final_weights[voice_index].append(weights.unsqueeze(1))

            # first batch element
            preds = to_numpy(preds[0])
            new_pitch_index = np.argmax(preds)

            gen_chorale[:, next_voice_index, next_time_index] = int(new_pitch_index)
        for i in range(num_voices):
            final_weights[i] = torch.cat(final_weights[i], 1)
        return final_weights, gen_chorale
Exemplo n.º 47
0
 def forward(self, x):
     x = x.view(-1, 28 * 28)
     x = F.relu(self.fc1(x))
     x = F.relu(self.fc2(x))
     x = self.fc3(x)
     return x
 def forward(self, state):
     """Build an actor (policy) network that maps states -> actions."""
     x = F.relu(self.fc1(state))
     x = F.relu(self.fc2(x))
     return torch.tanh(self.fc3(x))
Exemplo n.º 49
0
 def forward(self, c5):
     p6 = self.p6(c5)
     p7 = self.p7(F.relu(p6))
     return [p6, p7]
Exemplo n.º 50
0
 def forward(self, X, A_hat):  ### 1-layer GCN architecture
     X = torch.mm(X, self.weight)
     if self.bias is not None:
         X = (X + self.bias)
     X = F.relu(torch.mm(A_hat, X))
     return X
 def encode(self, x: Variable) -> (Variable, Variable):
     h1 = F.relu(self.fc1(x))             # h1 is 400-dimensional vector
     return self.fc21(h1), self.fc22(h1)  # this returns two 20-dimensional vectors
Exemplo n.º 52
0
    def forward(self, x):
        x = F.relu(self.conv1_bn(self.conv1(x)))
        x = F.relu(self.conv2_bn(self.conv2(x)))
        x = F.max_pool2d(x, (2, 2))
        x = self.do1(x)

        x = F.relu(self.conv3_bn(self.conv3(x)))
        x = F.relu(self.conv4_bn(self.conv4(x)))
        x = F.max_pool2d(x, (2, 2))
        x = self.do2(x)

        x = F.relu(self.conv5_bn(self.conv5(x)))
        x = self.do3(x)
        x = F.relu(self.conv6_bn(self.conv6(x)))
        x = self.do4(x)
        x = F.relu(self.conv7_bn(self.conv7(x)))
        x = self.do5(x)
        x = F.relu(self.conv8_bn(self.conv8(x)))
        x = F.max_pool2d(x, (2, 2))
        x = self.do6(x)

        x = F.relu(self.conv9_bn(self.conv9(x)))
        x = F.relu(self.conv10_bn(self.conv10(x)))
        x = F.max_pool2d(x, (1, 2))
        x = self.do7(x)

        x = F.relu(self.conv11_bn(self.conv11(x)))
        x = F.relu(self.conv12_bn(self.conv12(x)))
        x = F.max_pool2d(x, (1, 2))
        x = self.do8(x)

        x = F.relu(self.conv13_bn(self.conv13(x)))
        x = self.do9(x)
        x = F.relu(self.conv14_bn(self.conv14(x)))
        x = self.do10(x)

        x = self.conv15_bn(self.conv15(x))
        size_to_pool = x.size()
        x = F.avg_pool2d(x, (size_to_pool[2], size_to_pool[3]))
        x = x.view(-1, 41)
        return x
Exemplo n.º 53
0
 def encode(self, x):
     h = F.relu(self.fc1(x))
     mu = self.fc_encoder_mu(h)
     logvar = self.fc_encoder_logvar(h)
     return mu, logvar
 def decode(self, z: Variable) -> Variable:
     h3 = F.relu(self.fc3(z))             # z can be used to generate new samples
     return torch.sigmoid(self.fc4(h3))       # remember that z ~ N(mu, var)
def G(z):
    h = nn.relu(z @ Wzh + bzh.repeat(z.size(0), 1))
    X = nn.sigmoid(h @ Whx + bhx.repeat(h.size(0), 1))
    return X
Exemplo n.º 56
0
 def forward(self, z):
     h = F.relu(self.fc_decoder(z))
     x_reconst = self.softmax(self.fc2(h))
     return x_reconst
Exemplo n.º 57
0
 def forward(self, x):
     x = F.relu(self.fc1(x))
     x = F.relu(self.fc2(x))
     return self.fc3(x)
def D(X):
    h = nn.relu(X @ Wxh + bxh.repeat(X.size(0), 1))
    y = nn.sigmoid(h @ Why + bhy.repeat(h.size(0), 1))
    return y
Exemplo n.º 59
0
    def forward(self, x):
        x = F.relu(self.bn1(self.lin1(x)))
        x = F.relu(self.bn2(self.lin2(x)))
        x = F.relu(self.bn3(self.lin3(x)))

        return torch.sigmoid(self.head(x))
 def forward(self, x):
     out = F.relu(self.bn1(self.conv1(x)))
     out = F.relu(self.bn2(self.conv2(out)))
     out = self.bn3(self.conv3(out))
     out = out + self.shortcut(x) if self.stride==1 else out
     return out