def __init__(self, word_vec_size, hidden_size, word_mat, dropout_prob=0.1, num_layers=3, no_wordvec_layer=False): super(BOWModel, self).__init__() self.embs = ibp.Embedding.from_pretrained(word_mat) self.rotation = ibp.Linear(word_vec_size, hidden_size) self.sum_drop = ibp.Dropout(dropout_prob) if dropout_prob else None layers = [] for i in range(num_layers): layers.append(ibp.Linear(2 * hidden_size, 2 * hidden_size)) layers.append(ibp.Activation(F.relu)) if dropout_prob: layers.append(ibp.Dropout(dropout_prob)) layers.append(ibp.Linear(2 * hidden_size, len(EntailmentLabels))) layers.append(ibp.LogSoftmax(dim=1)) self.layers = nn.Sequential(*layers)
def get_feedforward_layers(num_layers, input_size, hidden_size, output_size): layers = [] for i in range(num_layers): layer_in_size = input_size if i == 0 else hidden_size layer_out_size = output_size if i == num_layers - 1 else hidden_size if dropout_prob: layers.append(ibp.Dropout(dropout_prob)) layers.append(ibp.Linear(layer_in_size, layer_out_size)) if i < num_layers - 1: layers.append(ibp.Activation(F.relu)) return layers
def __init__(self, word_vec_size, hidden_size, word_mat, pool='max', dropout=0.2, no_wordvec_layer=False): super(BOWModel, self).__init__() self.pool = pool self.no_wordvec_layer = no_wordvec_layer self.embs = ibp.Embedding.from_pretrained(word_mat) if no_wordvec_layer: self.linear_hidden = ibp.Linear(word_vec_size, hidden_size) else: self.linear_input = ibp.Linear(word_vec_size, hidden_size) self.linear_hidden = ibp.Linear(hidden_size, hidden_size) self.linear_output = ibp.Linear(hidden_size, 1) self.dropout = ibp.Dropout(dropout) if self.pool == 'attn': self.attn_pool = ibp.Linear(hidden_size, 1)
def __init__(self, device, num_vocabs, x_size, h_size, num_classes, dropout, cell_type='nary', pretrained_emb=None, no_wordvec_layer=False, perturbation=None): super(TreeLSTMDP, self).__init__() assert perturbation is not None # TODO: to implement more discrete perturbation space self.device = device self.perturbation = perturbation self.adv_attack = False self.out_x_vecs = None self.adv_attack_Ins = False self.out_ioux_c = [None, None] self.deltas = Perturbation.str2deltas(perturbation) self.deltas_p1 = [delta + 1 for delta in self.deltas] self.x_size = x_size self.h_size = h_size self.embedding = ibp.Embedding(num_vocabs, x_size) if pretrained_emb is not None: print('Using glove') self.embedding.weight.data.copy_(pretrained_emb) self.embedding.weight.requires_grad = True self.dropout = ibp.Dropout(dropout) self.linear = ibp.Linear(h_size, num_classes) cell = TreeLSTMCellDP if cell_type == 'nary' else None if cell is None: raise NotImplementedError self.no_wordvec_layer = no_wordvec_layer if no_wordvec_layer: self.cell = cell(x_size, h_size, self.deltas_p1, device) else: self.linear_input = ibp.Linear(x_size, h_size) self.cell = cell(h_size, h_size, self.deltas_p1, device)
def __init__(self, word_vec_size, hidden_size, word_mat, device, dropout=0.2, no_wordvec_layer=False): super(LSTMFinalStateModel, self).__init__() self.hidden_size = hidden_size self.no_wordvec_layer = no_wordvec_layer self.device = device self.embs = ibp.Embedding.from_pretrained(word_mat) if no_wordvec_layer: self.lstm = ibp.LSTM(word_vec_size, hidden_size, bidirectional=True) else: self.linear_input = ibp.Linear(word_vec_size, hidden_size) self.lstm = ibp.LSTM(hidden_size, hidden_size) self.dropout = ibp.Dropout(dropout) self.fc_hidden = ibp.Linear(hidden_size, hidden_size) self.fc_output = ibp.Linear(hidden_size, 1)
def __init__(self, word_vec_size, hidden_size, kernel_size, word_mat, pool='max', dropout=0.2, no_wordvec_layer=False, early_ibp=False, relu_wordvec=True, unfreeze_wordvec=False): super(CNNModel, self).__init__() cnn_padding = (kernel_size - 1) // 2 # preserves size self.pool = pool # Ablations self.no_wordvec_layer = no_wordvec_layer self.early_ibp = early_ibp self.relu_wordvec = relu_wordvec self.unfreeze_wordvec = False # End ablations self.embs = ibp.Embedding.from_pretrained( word_mat, freeze=not self.unfreeze_wordvec) if no_wordvec_layer: self.conv1 = ibp.Conv1d(word_vec_size, hidden_size, kernel_size, padding=cnn_padding) else: self.linear_input = ibp.Linear(word_vec_size, hidden_size) self.conv1 = ibp.Conv1d(hidden_size, hidden_size, kernel_size, padding=cnn_padding) if self.pool == 'attn': self.attn_pool = ibp.Linear(hidden_size, 1) self.dropout = ibp.Dropout(dropout) self.fc_hidden = ibp.Linear(hidden_size, hidden_size) self.fc_output = ibp.Linear(hidden_size, 1)