Beispiel #1
0
 def __init__(self, in_dim):
     super(CrissCrossAttention, self).__init__()
     # Q, K, V 矩阵
     self.query_conv = nn.Conv2d(in_channels=in_dim,
                                 out_channels=in_dim // 8,
                                 kernel_size=1)
     self.key_conv = nn.Conv2d(in_channels=in_dim,
                               out_channels=in_dim // 8,
                               kernel_size=1)
     self.value_conv = nn.Conv2d(in_channels=in_dim,
                                 out_channels=in_dim,
                                 kernel_size=1)
     # softmax操作
     self.softmax = Softmax(dim=3)
     self.INF = INF
     self.gamma = nn.Parameter(torch.zeros(1))
Beispiel #2
0
    def __init__(self, config, vis):
        super(Attention, self).__init__()
        self.vis = vis
        self.num_attention_heads = config.transformer["num_heads"]
        self.attention_head_size = int(config.hidden_size / self.num_attention_heads)
        self.all_head_size = self.num_attention_heads * self.attention_head_size

        self.query = Linear(config.hidden_size, self.all_head_size)
        self.key = Linear(config.hidden_size, self.all_head_size)
        self.value = Linear(config.hidden_size, self.all_head_size)

        self.out = Linear(config.hidden_size, config.hidden_size)
        self.attn_dropout = Dropout(config.transformer["attention_dropout_rate"])
        self.proj_dropout = Dropout(config.transformer["attention_dropout_rate"])

        self.softmax = Softmax(dim=-1)
Beispiel #3
0
    def __init__(self, in_dim):
        super(PAM_Module, self).__init__()
        self.chanel_in = in_dim

        self.query_conv = Conv2d(in_channels=in_dim,
                                 out_channels=in_dim // 8,
                                 kernel_size=1)
        self.key_conv = Conv2d(in_channels=in_dim,
                               out_channels=in_dim // 8,
                               kernel_size=1)
        self.value_conv = Conv2d(in_channels=in_dim,
                                 out_channels=in_dim,
                                 kernel_size=1)
        self.gamma = Parameter(torch.zeros(1))

        self.softmax = Softmax(dim=-1)
Beispiel #4
0
    def __init__(self, token_embedder, agenda_dim, decoder_dim, encoder_dim,
                 attn_dim, no_insert_delete_attn, num_layers):
        super(AttentionDecoderCell, self).__init__()

        input_dim = token_embedder.embed_dim
        self.num_layers = num_layers

        # see definition of `x_augment` in `forward` method
        # we augment the input to each RNN layer with 3 attention contexts + the agenda
        augment_dim = encoder_dim + input_dim + input_dim + agenda_dim

        self.rnn_cells = []
        for layer in range(num_layers):
            in_dim = input_dim if layer == 0 else decoder_dim  # first layer takes word vectors
            out_dim = decoder_dim
            rnn_cell = LSTMCell(in_dim + augment_dim, out_dim)
            self.add_module('decoder_layer_{}'.format(layer), rnn_cell)
            self.rnn_cells.append(rnn_cell)

        # see definition of `z` in `forward` method
        # to predict words, we condition on the hidden state h + 3 attention contexts
        z_dim = decoder_dim + encoder_dim + 2 * input_dim
        if no_insert_delete_attn:
            z_dim = decoder_dim + encoder_dim

        self.vocab_projection_pos = Linear(
            z_dim, input_dim
        )  # TODO(kelvin): these big params may need regularization
        self.vocab_projection_neg = Linear(z_dim, input_dim)
        self.relu = torch.nn.ReLU()

        self.h0 = Parameter(torch.zeros(decoder_dim))
        self.c0 = Parameter(torch.zeros(decoder_dim))
        self.vocab_softmax = Softmax()

        self.source_attention = Attention(encoder_dim, decoder_dim, attn_dim)
        if not no_insert_delete_attn:
            self.insert_attention = Attention(input_dim, decoder_dim, attn_dim)
            self.delete_attention = Attention(input_dim, decoder_dim, attn_dim)
        else:
            self.insert_attention = DummyAttention(input_dim, decoder_dim,
                                                   attn_dim)
            self.delete_attention = DummyAttention(input_dim, decoder_dim,
                                                   attn_dim)

        self.token_embedder = token_embedder
        self.no_insert_delete_attn = no_insert_delete_attn
Beispiel #5
0
    def __init__(self,
                 nb_classes,
                 verbose=False,
                 build=True,
                 batch_size=64,
                 nb_filters=32,
                 use_residual=True,
                 use_bottleneck=True,
                 depth=6,
                 kernel_size=41,
                 nb_epochs=1500,
                 stride=1,
                 activation='linear'):
        # self.output_directory = output_directory
        self.nb_filters = nb_filters
        self.use_residual = use_residual
        self.use_bottleneck = use_bottleneck
        self.depth = depth
        self.kernel_size = kernel_size - 1
        self.callbacks = None
        self.batch_size = batch_size
        self.bottleneck_size = 32
        self.nb_epochs = nb_epochs
        self.depth = depth
        self.nb_classes = nb_classes

        self.stride = stride

        super().__init__()

        net = [
            InceptionBlockWithResidual.InceptionBlockWithResidual(
                1, 32, 8, 18),
            inceptionModule.InceptionModule(32, 64, 1, 24, 32, 32, True),
            InceptionBlockWithResidual.InceptionBlockWithResidual(
                32, 32, 20, 12),
            inceptionModule.InceptionModule(32, 32, 1, 8, 32, 32, True)
        ]

        #net = [InceptionBlockWithResidual.InceptionBlockWithResidual(1,32),
        #                     inceptionModule.InceptionModule(32,64,1,41,32,32,True),
        #                    InceptionBlockWithResidual.InceptionBlockWithResidual(32,32),
        #                   inceptionModule.InceptionModule(32,32,1,41,32,32,True)]

        self.net = Sequential(*net)

        self.output_layer = Sequential(Linear(32, nb_classes), Softmax(1))
Beispiel #6
0
    def __init__(self,
                 num_of_layers,
                 hidden_size,
                 dropout=0.0,
                 inner_activation=None):
        super(AttendedState, self).__init__()
        self.__input_size = hidden_size
        self.__output_size = hidden_size
        self.mlp = MultiLayerPerceptron(num_of_layers=num_of_layers,
                                        init_size=hidden_size,
                                        out_size=hidden_size,
                                        dropout=dropout,
                                        inner_activation=inner_activation,
                                        outer_activation=inner_activation)

        self.attention = Linear(hidden_size, 1)
        self.at_softmax = Softmax()
Beispiel #7
0
    def LeNet(self):
        model = getattr(models, 'GoogLeNet')(pretrained=self.pretrained)
        conv = model.conv1[0]
        classifier = model.fc

        model.conv1[0] = Conv2d(self.in_channels,
                                conv.out_channels,
                                kernel_size=conv.kernel_size,
                                stride=conv.stride,
                                padding=conv.padding,
                                bias=conv.bias)
        # todo inherit og 1st layer weights
        model.fc = Linear(in_features=classifier.in_features,
                          out_features=self.out_classes,
                          bias=True)

        return Sequential(model, Softmax(1)) if self.soft_max else model
Beispiel #8
0
    def __init__(self, vocab_size, embedding_dim):
        """Initializes model layers.

        Args:
            vocab_size (int): Number of tokens in corpus. This is used to init embeddings.
            embedding_dim (int): Dimension of embedding vector.
        """
        super().__init__()

        self._embedding_dim = embedding_dim

        self.encoder_in = Encoder(vocab_size, embedding_dim)
        self.encoder_out = Encoder(vocab_size, embedding_dim)

        self.linear = Linear(embedding_dim, embedding_dim, bias=False)
        self.similarity = CosineSimilarity(dim=2)
        self.softmax = Softmax(dim=2)
Beispiel #9
0
def parse_activation(name):
    if name in relu_names:
        return ReLU()
    if name in leaky_relu_names:
        return LeakyReLU()
    if name in sigmoid_names:
        return Sigmoid()
    if name in log_sigmoid_name:
        return LogSigmoid()
    if name in p_relu_names:
        return PReLU()
    if name in tanh_names:
        return Tanh()
    if name in softmax_names:
        return Softmax(dim=1)
    if name in log_softmax_names:
        return LogSoftmax(dim=1)
Beispiel #10
0
    def __init__(self,
                 in_channels: int,
                 num_classes: int,
                 class_type: str = "single"):
        super().__init__()
        self.avgpool = AdaptiveAvgPool2d(1)
        self.fc = Linear(in_channels, num_classes)

        if class_type == "single":
            self.softmax = Softmax(dim=1)
        elif class_type == "multi":
            self.softmax = Sigmoid()
        else:
            raise ValueError(
                "unknown class_type given of {}".format(class_type))

        self.initialize()
def sample(model, text_field, prompt="", max_len=50, temp=1.0, k=0, p=1):
    assert (k == 0 or p == 1), "Cannot combine top-k and top-p sampling"

    hidden_size = model._modules['rnn'].hidden_size
    n_layers = model._modules['rnn'].num_layers

    h_t_prev = torch.zeros(n_layers, 1, hidden_size)
    c_t_prev = torch.zeros(n_layers, 1, hidden_size)
    w_t = text_field.process([text_field.tokenize(prompt.lower())])

    sampling_criterion = Softmax(dim=1)
    words = []

    for i in range(max_len):
        s_t, h_t, c_t = model(w_t, h_t_prev, c_t_prev)
        w_t_next = sampling_criterion(s_t[-1] / temp)

        if k != 0:
            # Top-k Sampling
            topk_vals, topk_indices = torch.topk(w_t_next, k)
            topk_vals /= topk_vals.sum(dim=1)
            prob_dist = Categorical(topk_vals)
            w_t = topk_indices.view(-1)[prob_dist.sample()].unsqueeze(dim=0)
        elif p > 0:
            # Top-p Sampling
            sorted_wts, sorted_indices = torch.sort(w_t_next, descending=True)
            cumulative_probs = torch.cumsum(input=sorted_wts, dim=1).squeeze()
            sorted_indices_to_remove = cumulative_probs > p
            sorted_indices_to_remove[1:] = sorted_indices_to_remove[:-1].clone()
            sorted_indices_to_remove[0] = 0
            indices_to_remove = sorted_indices[sorted_indices_to_remove.unsqueeze(0)]
            w_t_next.squeeze()[indices_to_remove] = 0
            w_t_next /= w_t_next.sum()
            prob_dist = Categorical(w_t_next)
            w_t = prob_dist.sample().unsqueeze(0)
        else:
            # Vanilla and Temperature-Scaled Sampling
            prob_dist = Categorical(w_t_next)
            w_t = prob_dist.sample().unsqueeze(dim=0)

        words.append(w_t)
        h_t_prev = h_t
        c_t_prev = c_t

    decoded_string = prompt + " " + reverseNumeralize(torch.cat(words).squeeze(), text_field)
    return decoded_string
def special_mario_downsampling(num_scales, scales, image, token_list):
    """
    Special Downsampling Method designed for Super Mario Bros. Token based levels.

    num_scales : number of scales the image is scaled down to.
    scales : downsampling scales. Should be an array tuples (scale_x, scale_y) of length num_scales.
    image : Original level to be scaled down. Expects a torch tensor.
    token_list : list of tokens appearing in the image in order of channels from image.
    """

    scaled_list = []
    for sc in range(num_scales):
        scale_v = scales[sc][0]
        scale_h = scales[sc][1]

        # Initial downscaling of one-hot level tensor is normal bilinear scaling
        bil_scaled = interpolate(image, (int(image.shape[-2] * scale_v), int(image.shape[-1] * scale_h)),
                                 mode='bilinear', align_corners=False)

        # Init output level
        img_scaled = torch.zeros_like(bil_scaled)

        for x in range(bil_scaled.shape[-2]):
            for y in range(bil_scaled.shape[-1]):
                curr_h = 0
                curr_tokens = [tok for tok in token_list if bil_scaled[:, token_list.index(tok), x, y] > 0]
                for h in range(len(HIERARCHY)):  # find out which hierarchy group we're in
                    for token in HIERARCHY[h].keys():
                        if token in curr_tokens:
                            curr_h = h

                for t in range(bil_scaled.shape[-3]):
                    if not (token_list[t] in HIERARCHY[curr_h].keys()):
                        # if this token is not on the correct hierarchy group, set to 0
                        img_scaled[:, t, x, y] = 0
                    else:
                        # if it is, keep original value
                        img_scaled[:, t, x, y] = bil_scaled[:, t, x, y]

                # Adjust level to look more like the generator output through a Softmax function.
                img_scaled[:, :, x, y] = Softmax(dim=1)(30*img_scaled[:, :, x, y])

        scaled_list.append(img_scaled)

    scaled_list.reverse()
    return scaled_list
Beispiel #13
0
    def __init__(self,in_dim, device):
        '''
        Parameters
        ----------
        in_dim : int
            channels of input
     	device: torch.device

        '''
        super(CC_module, self).__init__()
        self.query_conv = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//8, kernel_size=1)
        self.key_conv = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//8, kernel_size=1)
        self.value_conv = nn.Conv2d(in_channels=in_dim, out_channels=in_dim, kernel_size=1)
        self.softmax = Softmax(dim=3)
        self.INF = INF
        self.gamma = nn.Parameter(torch.zeros(1)).to(device)
        self.device = device
Beispiel #14
0
    def get_possible_answers(question, context, n_ans):
        p = Softmax(dim=0)
        inputs = tokenizer_fast(question,
                                context,
                                truncation=True,
                                padding=True,
                                max_length=500,
                                return_tensors="pt")
        input_ids = inputs["input_ids"].tolist()[0]
        context_ids = np.array(input_ids)[np.array(inputs['token_type_ids'][0],
                                                   dtype=bool)]
        text_tokens = tokenizer_fast.convert_ids_to_tokens(input_ids)
        outputs = model(**inputs)
        answer_start_scores = outputs.start_logits[0][torch.tensor(
            inputs['token_type_ids'][0], dtype=bool)][:-1]
        answer_end_scores = outputs.end_logits[0][torch.tensor(
            inputs['token_type_ids'][0], dtype=bool)][:-1]
        total_scores = np.array([[
            float(answer_start_scores[i] + answer_end_scores[j])
            for j in range(len(answer_start_scores))
        ] for i in range(len(answer_start_scores))])
        possibles = []
        possible_scores = []
        i = 0
        while (len(possibles) < n_ans) & (i < total_scores.shape[0]):
            row, col = np.unravel_index(np.argsort(total_scores.ravel()),
                                        total_scores.shape)
            row, col = row[::-1], col[::-1]

            start = row[i]
            end = col[i]

            if (end >= start) & (tokenizer_fast.convert_ids_to_tokens(context_ids[start:(start+1)])[0][0:2] != "##")&\
                (tokenizer_fast.convert_ids_to_tokens(context_ids[(end+1):(end+2)])[0][0:2] != "##"):
                score = torch.tensor(total_scores)[start, end]
                answer = tokenizer_fast.convert_tokens_to_string(
                    tokenizer_fast.convert_ids_to_tokens(
                        context_ids[start:(end + 1)]))
                if (len(answer) <= 100) & (1 -
                                           ("(" in answer) & ~(")" in answer)
                                           ) & (~(answer in possibles)):
                    possibles.append(answer)
                    possible_scores.append(score)
            i += 1
        possible_scores_prob = p(torch.tensor(possible_scores))
        return possibles, possible_scores_prob
Beispiel #15
0
    def __init__(self, n_inputs):
        super(MLP, self).__init__()

        # input to first hidden layer
        self.hidden1 = Linear(n_inputs, 10)
        kaiming_uniform_(self.hidden1.weight, nonlinearity='relu')
        self.act1 = ReLU()

        # second hidden layer
        self.hidden2 = Linear(10, 8)
        kaiming_uniform_(self.hidden2.weight, nonlinearity='relu')
        self.act2 = ReLU()

        # third hidden layer and output
        self.hidden3 = Linear(8, 3)
        xavier_uniform_(self.hidden3.weight)
        self.act3 = Softmax(dim=1)
def visualize(pov,pred,label,prev_label,next_label):
    y = Softmax(0)(pred.squeeze().detach())
    width = 0.8



    x = np.arange(len(aclist))
    # y = np.random.sample(30)

    ## plotting
    plt.rcdefaults()
    # similar to tight_layout, but works better!
    fig, (ax1, ax2) = plt.subplots(1, 2, constrained_layout=True)



    rects1 = ax2.barh(x, y,height=width/4,label='logits')

    y_ = np.zeros(30)
    y_[label[0]] = 1
    rects0 = ax2.barh(x-width/4, y_,label='label',height=width/4)


    if prev_label != None:
        y_ = np.zeros(30)
        y_[prev_label[0]] = 1
        ax2.barh(x+width / 4, y_,label='prev_label',height=width/4)


    if next_label != None:
        y_ = np.zeros(30)
        y_[next_label[0]] = 1
        ax2.barh(x-width/2, y_,label='next_label',height=width/4)



    plt.xlim(0, 1)

    ax1.imshow(pov.squeeze() / 255)
    ax1.axis('off')

    ax2.set_yticks(x)
    ax2.set_yticklabels(aclist, fontsize=3)
    ax2.tick_params(axis='x',labelsize=3,tickdir='in')
    ax2.set_xlabel('softmaxed logits',fontsize = 3)
Beispiel #17
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 key_channels,
                 psp_size=(1, 4, 8, 16),
                 bn=False,
                 res=True,
                 v_conv_kernel=1):

        super(NLPM, self).__init__()

        self.in_channels = in_channels
        self.out_channels = out_channels
        self.key_channels = key_channels
        self.query_channels = key_channels
        self.psp_size = psp_size
        self.bn = bn
        self.res = res

        self.query_conv = nn.Conv2d(in_channels,
                                    key_channels,
                                    kernel_size=1,
                                    bias=False)

        self.key_conv = nn.Conv2d(in_channels,
                                  key_channels,
                                  kernel_size=1,
                                  bias=False)
        self.key_psp = PSPModule(sizes=psp_size)

        self.value_conv = nn.Conv2d(in_channels,
                                    out_channels,
                                    kernel_size=v_conv_kernel,
                                    padding=v_conv_kernel // 2,
                                    bias=False)
        self.value_psp = PSPModule(sizes=psp_size)

        if bn:
            self.query_bn = nn.BatchNorm2d(key_channels)
            self.key_bn = nn.BatchNorm2d(key_channels)

        print('Initing NLPM')

        self.softmax = Softmax(dim=-1)
        self.alpha = nn.Parameter(torch.zeros(1))
def valid_model(device, model, dataloader, criterion):

    best_IOU = 0.0
    step_message = "val_loss = {:6.4f}, val_IOU = {:6.4f} --- {:5.2f} ms/batch"
    best_result_message = "-" * 30 + "\n" + \
                    "Best IOU : {:<6.4f}" + "\n" + \
                    "-" * 30

    # container for IOU
    # we only concern for road
    iou_record = np.zeros(2, dtype=int)
    running_loss = 0
    since = time.time()

    for i, datas in enumerate(dataloader):
        images, labels = datas
        images = images.to(device)
        labels = labels.to(device)
        # forward pass
        outputs = model(images)

        outputs = outputs.permute(0, 2, 3, 1).reshape(-1, 2)
        labels = labels.permute(0, 2, 3, 1).reshape(-1, 2)
        outputs = Softmax(dim=1)(outputs)
        loss = criterion(outputs, labels)

        pred = (outputs > 0.5)[:, 1].type(torch.uint8)
        labels = (labels > 0.5)[:, 1].type(torch.uint8)
        # pred correctly
        iou_record[0] += torch.sum((pred == 0) & (labels == 0)).item()
        # all gt pixel belongs to road and all pred pixel belongs to road
        iou_record[1] += torch.sum(labels == 0).item() + torch.sum(
            pred == 0).item()

        running_loss += loss.item()

    time_eplased = time.time()
    iou = iou_record[0] / (iou_record[1] - iou_record[0])
    print(step_message.format(running_loss, iou, (time_eplased - since) * 100))

    iou = iou_record[0] / (iou_record[1] - iou_record[0])
    if iou > best_IOU:
        best_IOU = iou
        # best_model_wts = copy.deepcopy(model.state_dict())
        print(best_result_message.format(best_IOU))
Beispiel #19
0
 def __init__(self, pretrained=True):
     super().__init__()
     self.conv1 = Conv2d(3, 10, kernel_size=3)
     self.prelu1 = PReLU(10)
     self.pool1 = MaxPool2d(2, 2, ceil_mode=True)
     self.conv2 = Conv2d(10, 16, kernel_size=3)
     self.prelu2 = PReLU(16)
     self.conv3 = Conv2d(16, 32, kernel_size=3)
     self.prelu3 = PReLU(32)
     self.conv4_1 = Conv2d(32, 2, kernel_size=1)
     self.softmax4_1 = Softmax(dim=1)
     self.conv4_2 = Conv2d(32, 4, kernel_size=1)
     self.training = False
     if pretrained:
         state_dict_path = os.path.join(os.path.dirname(__file__),
                                        'pnet.pt')
         state_dict = torch.load(state_dict_path)
         self.load_state_dict(state_dict)
Beispiel #20
0
 def __init__(self,
              n_dim: int,
              e_dim: int,
              p_dim: int,
              c_dim: int,
              dropout=0.,
              use_cuda=False):
     super(MolGATMesPassing, self).__init__()
     self.linear = Linear(n_dim + p_dim + n_dim, c_dim, bias=True)
     self.linear_e = Linear(n_dim + p_dim + n_dim, e_dim, bias=True)
     self.relu1 = LeakyReLU()
     self.relu2 = LeakyReLU()
     self.relu_e = LeakyReLU()
     self.attention = Linear(e_dim + p_dim, 1, bias=True)
     self.softmax = Softmax(dim=1)
     self.elu = ELU()
     self.dropout = Dropout(p=dropout)
     self.use_cuda = use_cuda
    def inference(self, image, path=True):
        img = self.load_image(image, path)

        self.model.eval()
        print('predicting')

        with torch.no_grad():
            image_tensor = transforms.ToTensor()(img).unsqueeze_(0).to(
                self.device)
            output = self.model(image_tensor)
            softmax_output = Softmax(dim=1)(output)
            confidence, predicted_index = torch.max(softmax_output, 1)
            predicted = self.index2label[int(predicted_index[0])]

        if confidence < .5:
            predicted = 'unknown firearm'

        return float(confidence), predicted
Beispiel #22
0
    def VGG(self, name='vgg16'):
        model = getattr(models, name)(pretrained=self.pretrained)
        conv = model.features[0]
        classifier = model.classifier[-1]

        model.features[0] = Conv2d(self.in_channels,
                                   conv.out_channels,
                                   kernel_size=conv.kernel_size,
                                   stride=conv.stride,
                                   padding=conv.padding)
        model.features[0].bias.data = conv.bias
        model.features[0].weight.data = conv.weight.mean(1).unsqueeze(
            1)  # inherit og 1st layer weights
        model.classifier[-1] = Linear(in_features=classifier.in_features,
                                      out_features=self.out_classes,
                                      bias=True)

        return Sequential(model, Softmax(1)) if self.soft_max else model
Beispiel #23
0
    def __init__(self, in_dim, dropout=False):
        # def __init__(self, in_dim, dropout=True):   #20191217_2
        super(PAM_Module, self).__init__()
        self.channel_in = in_dim

        t = int(abs(math.log(self.channel_in, 2) + 1) / 2)
        self.k = t if t % 2 else t + 1

        self.query_conv = Conv1d(in_channels=1,
                                 out_channels=1,
                                 kernel_size=self.k,
                                 padding=int(self.k / 2),
                                 bias=False)
        self.key_conv = Conv1d(in_channels=1,
                               out_channels=1,
                               kernel_size=self.k,
                               padding=int(self.k / 2),
                               bias=False)
        # self.value_conv = Conv1d(in_channels=1, out_channels=1, kernel_size=self.k, padding=int(self.k/2), bias=False)

        # self.query_conv = Conv2d(in_channels=in_dim, out_channels=in_dim // 8, kernel_size=1)
        # self.key_conv = Conv2d(in_channels=in_dim, out_channels=in_dim // 8, kernel_size=1)
        self.value_conv = Conv2d(in_channels=in_dim,
                                 out_channels=in_dim,
                                 kernel_size=1)  #20191212_3
        # self.res_conv = Conv2d(in_channels=in_dim, out_channels=in_dim, kernel_size=1) #20191215_2

        self.gamma = Parameter(torch.zeros(1))
        # self.gamma = Parameter(torch.zeros(1)) 20191212_2

        self.softmax = Softmax(dim=-1)
        self.bn = nn.BatchNorm2d(in_dim)

        if dropout:
            # self.dropout = nn.Dropout(0.3) # 20191215_1
            self.dropout = nn.Dropout(0.1)  # 20191216_3

        if dropout:
            self.is_dropout = True
        else:
            self.is_dropout = False

        # self.scale = torch.sqrt(torch.FloatTensor([in_dim // 8]))   #20191212_4
        self._init_param()
Beispiel #24
0
    def __init__(self, output_dim=10, dropout=0.5):
        super(MLP, self).__init__()
        #These are encoder layers
        self.encoders = []
        #Layer Sizes
        self.ls = [784, 1000, 500, 250, 250, 250]
        n_layers = len(ls)  #6
        for i in xrange(n_layers - 1):
            self.encoders[i] = Sequential()
            self.encoders[i].add_module('l_{}'.format(i),
                                        Linear(self.ls[i], self.ls[i + 1]))
            #*** eps, momentum are default nonzero, torch imp sets both to 0
            #*** affine default to true giving learnable params, torch imp sets to false
            #self.encoders[i].add_module('bn_{}'.format(i), BatchNorm1d(self.ls[i+1], ))
            self.encoders[i].add_module(
                'bn_{}'.format(i), BatchNorm1d(self.ls[i + 1], 0.0, 0.0,
                                               False))
            self.encoders[i].add_module('relu_{}'.format(i), ReLU())

        self.encoders[n_layers] = Sequential()
        self.encoders[n_layers].add_module(
            'l_n', Linear(self.ls[n_layers], output_dim))
        #*** eps, momentum are default nonzero, torch imp doesn't set to 0 in last layer
        #*** affine default to true giving learnable params, torch imp  leaves it as true
        #self.encoders[n_layers].add_module('bn_n', BatchNorm1d(output_dim))
        self.encoders[n_layers].add_module(
            'bn_n', BatchNorm1d(output_dim, 0.0, 0.0, False))
        self.encoders[n_layers].add_module('relu_{}', ReLU())

        #***This may have to be it's own sequential...unsure
        # TODO: Use LogSoftmax because we have to use Negative Log Likelihood
        self.encoders[n_layers].add_module('softmax', Softmax())

        #***Need decoder layers here
        self.decoders = []  #Should also be 6

        #***Do something unique for last layer, then can loop through
        self.decoders[n_layers - 1] = Sequential()
        #self.decoders[n_layers-1].add_module(
        #self.decoders[n_layers-1].add_module(

        #4,3,2,1
        for i in xrange(n_layers - 2, 0, -1):
            self.decoders[i] = Sequential()
Beispiel #25
0
    def __init__(self, token_embedder, hidden_dim, input_dim, agenda_dim,
                 num_layers):
        super(MultilayeredDecoderCell, self).__init__()
        self.linear = Linear(hidden_dim, input_dim)
        self.h0 = Parameter(torch.zeros(hidden_dim))
        self.c0 = Parameter(torch.zeros(hidden_dim))
        self.softmax = Softmax()
        self.token_embedder = token_embedder
        self.num_layers = num_layers

        self.rnn_cells = []
        for layer in range(num_layers):
            in_dim = (
                input_dim + agenda_dim
            ) if layer == 0 else hidden_dim  # inputs to first layer are word vectors
            out_dim = hidden_dim
            rnn_cell = LSTMCell(in_dim, out_dim)
            self.add_module('decoder_layer_{}'.format(layer), rnn_cell)
            self.rnn_cells.append(rnn_cell)
    def __init__(self, n_channels):
        super(CNN, self).__init__()
        self.hidden1 = Conv2d(n_channels, 32, (3, 3))
        kaiming_uniform_(self.hidden1.weight, nonlinearity='relu')
        self.act1 = ReLU()
        self.pool1 = MaxPool2d((2, 2), stride=(2, 2))

        self.hidden2 = Conv2d(32, 32, (3, 3))
        kaiming_uniform_(self.hidden2.weight, nonlinearity='relu')
        self.act2 = ReLU()
        self.pool2 = MaxPool2d((2, 2), stride=(2, 2))

        self.hidden3 = Linear(5 * 5 * 32, 100)
        kaiming_uniform_(self.hidden3.weight, nonlinearity='relu')
        self.act3 = ReLU()

        self.hidden4 = Linear(100, 10)
        xavier_uniform_(self.hidden4.weight)
        self.act4 = Softmax(dim=1)
Beispiel #27
0
 def __init__(self, args):
     super(AttentionMILFeatures, self).__init__()
     width_fe = is_in_args(args, 'width_fe', 64)
     self.feature_extractor = Sequential(
         Linear(args.feature_depth, width_fe), ReLU(),
         Dropout(p=args.dropout), Linear(width_fe, width_fe), ReLU(),
         Dropout(p=args.dropout))
     self.weight_extractor = Sequential(
         Linear(width_fe, int(width_fe / 2)),
         Tanh(),
         Linear(int(width_fe / 2), 1),
         Softmax(dim=-2)  # Softmax sur toutes les tuiles. somme à 1.
     )
     self.classifier = Sequential(
         Linear(width_fe, int(width_fe / 2)),
         ReLU(),  # Added 25/09
         Dropout(p=args.dropout),  # Added 25/09
         Linear(int(width_fe / 2), 1),  # Added 25/09
         Sigmoid())
Beispiel #28
0
 def __GenerateSequence(self, input_nodes, hidden_nodes, output_nodes):
     """
     * Generate layers for neural network.
     """
     self.__inputsize = input_nodes
     self.__outputsize = output_nodes
     layers = []
     for num, hidden_dim in enumerate(hidden_nodes):
         if num == 0:
             layers.append(Linear(input_nodes, hidden_dim))
         else:
             layers.append(Linear(hidden_nodes[num - 1], hidden_dim))
     layers.append(Linear(hidden_dim, output_nodes))
     sequence = []
     for layer in layers:
         sequence.append(layer)
         sequence.append(Sigmoid())
     sequence.append(Softmax())
     return sequence
def main(batch_size, data_full_path, dtype, num_batches, pdf_name):
    sm = Softmax(dim=0)
    data_manager = DataManager(data_full_path=data_full_path,
                               data_set_cls=AttackedDataset)
    dl = data_manager.get_data_loader(batch_size=batch_size)
    with PdfPages(pdf_name) as pdf:
        for iter_num, (inputs, labels, init_outputs, perturbed_outputs) \
                in enumerate(dl, 0):
            if iter_num >= num_batches:
                break
            n_cols = int(np.sqrt(batch_size))
            n_rows = int(np.ceil(batch_size / n_cols))
            # make grid, plots
            gs = plt.GridSpec(n_rows, n_cols)
            for evt, img_tnsr in enumerate(inputs):
                # color codes
                # * green if prediction is correct and adv fails
                # * red if prediction is wrong and adv doesn't flip
                # * blue if prediction is correct and adv flips
                # * black if prediction is wrong and adv doesn't flip
                adv_swp = torch.argmax(init_outputs[evt]) == \
                    torch.argmax(perturbed_outputs[evt])
                init_pred_correct = torch.argmax(init_outputs[evt]) == \
                    labels[evt]
                color = 'k'
                if init_pred_correct and adv_swp:
                    color = 'g'
                if init_pred_correct and (not adv_swp):
                    color = 'b'
                if (not init_pred_correct) and (not adv_swp):
                    color = 'r'
                titlestr = data_manager.label_names[labels[evt]] + '\n' + \
                    str(sm(init_outputs[evt]).numpy()) + ' atk-> ' + \
                    str(sm(perturbed_outputs[evt]).numpy())
                ax = plt.subplot(gs[evt])
                ax.axis('on')
                ax.xaxis.set_major_locator(plt.NullLocator())
                ax.yaxis.set_major_locator(plt.NullLocator())
                _ = ax.imshow(
                    np.moveaxis(img_tnsr.numpy().astype(dtype), 0, -1))
                plt.title(titlestr, fontsize=6, color=color)
                # plt.tight_layout()
            pdf.savefig()
Beispiel #30
0
    def __init__(self, ):
        super(GCN, self).__init__()
        self.size = 128
        self.in_features = 600
        self.l1 = GraphLinearEmbedding(self.in_features,
                                       self.size,
                                       with_bn=False,
                                       with_act_func=True)
        # self.dropout1 = Dropout(p = 0.5)

        self.gc1 = GraphConvolution(self.size, self.size*4, 4, with_bn = False, \
         with_act_func = True)
        # self.dropout2 = Dropout(p = 0.5)

        self.gc2 = GraphConvolution(self.size*4, self.size*8, 4, with_bn = False, \
         with_act_func = True)
        # self.dropout3 = Dropout(p = 0.5)
        # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~```
        self.gc6 = GraphConvolution(self.size*8, self.size*4, 4, with_bn = False, \
         with_act_func = True)

        self.gc7 = GraphConvolution(self.size*4, self.size, 4, with_bn = False, \
         with_act_func = True)
        # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        self.l3 = GraphLinearEmbedding(self.size, self.size * 4, with_bn=False)

        self.gc3 = GraphConvolution(self.size*4, self.size*8, 4, with_bn = False, \
         with_act_func = True)

        self.gc4 = GraphConvolution(self.size*8, self.size*4, 4, with_bn = False, \
         with_act_func = True)

        self.gc5 = GraphConvolution(self.size*4, self.size, 4, with_bn = False, \
         with_act_func = True)

        self.l2 = GraphLinearEmbedding(self.size,
                                       5,
                                       with_bn=False,
                                       with_act_func=True)

        self.softmax = Softmax(-1)

        self.criterion = torch.nn.CrossEntropyLoss()