Python torch.nn.Embedding() Examples
The following are 30
code examples of torch.nn.Embedding().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch.nn
, or try the search function
.
Example #1
Source File: BiLSTM.py From pytorch_NER_BiLSTM_CNN_CRF with Apache License 2.0 | 7 votes |
def __init__(self, **kwargs): super(BiLSTM, self).__init__() for k in kwargs: self.__setattr__(k, kwargs[k]) V = self.embed_num D = self.embed_dim C = self.label_num paddingId = self.paddingId self.embed = nn.Embedding(V, D, padding_idx=paddingId) if self.pretrained_embed: self.embed.weight.data.copy_(self.pretrained_weight) else: init_embedding(self.embed.weight) self.dropout_embed = nn.Dropout(self.dropout_emb) self.dropout = nn.Dropout(self.dropout) self.bilstm = nn.LSTM(input_size=D, hidden_size=self.lstm_hiddens, num_layers=self.lstm_layers, bidirectional=True, batch_first=True, bias=True) self.linear = nn.Linear(in_features=self.lstm_hiddens * 2, out_features=C, bias=True) init_linear(self.linear)
Example #2
Source File: model.py From TaskBot with GNU General Public License v3.0 | 6 votes |
def __init__(self, input_size, embed_size, hidden_size, n_layers=1, dropout=0.5, batch_first=False, bidirectional=False): """ :param input_size: 词典大小 :param embed_size: word2vec嵌入维度 :param hidden_size: encoder rnn 隐藏态维度 :param n_layers: rnn层数 :param dropout: dropout rate """ super(Encoder, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.embed_size = embed_size self.embed = nn.Embedding(input_size, embed_size) self.gru = nn.GRU(embed_size, hidden_size, n_layers, dropout=dropout, bidirectional=bidirectional, batch_first=batch_first)
Example #3
Source File: text_cnn.py From TaskBot with GNU General Public License v3.0 | 6 votes |
def __init__(self, param: dict): super().__init__(param) ci = 1 # input chanel size kernel_num = param['kernel_num'] # output chanel size kernel_size = param['kernel_size'] vocab_size = param['vocab_size'] embed_dim = param['embed_dim'] dropout = param['dropout'] class_num = param['class_num'] self.param = param self.embed = nn.Embedding(vocab_size, embed_dim, padding_idx=1) self.conv11 = nn.Conv2d(ci, kernel_num, (kernel_size[0], embed_dim)) self.conv12 = nn.Conv2d(ci, kernel_num, (kernel_size[1], embed_dim)) self.conv13 = nn.Conv2d(ci, kernel_num, (kernel_size[2], embed_dim)) self.dropout = nn.Dropout(dropout) self.fc1 = nn.Linear(len(kernel_size) * kernel_num, class_num)
Example #4
Source File: util.py From End-to-end-ASR-Pytorch with MIT License | 6 votes |
def init_weights(module): # Exceptions if type(module) == nn.Embedding: module.weight.data.normal_(0, 1) else: for p in module.parameters(): data = p.data if data.dim() == 1: # bias data.zero_() elif data.dim() == 2: # linear weight n = data.size(1) stdv = 1. / math.sqrt(n) data.normal_(0, stdv) elif data.dim() in [3, 4]: # conv weight n = data.size(1) for k in data.size()[2:]: n *= k stdv = 1. / math.sqrt(n) data.normal_(0, stdv) else: raise NotImplementedError
Example #5
Source File: cnn_attention.py From TaskBot with GNU General Public License v3.0 | 6 votes |
def __init__(self, args): super().__init__() self.args = args vocab_size = args["vocab_size"] class_num = args["class_num"] kernel_num = args["kernel_num"] kernel_size = args["kernel_size"] embed_dim = args["embed_dim"] self.embed = nn.Embedding(vocab_size, embed_dim, padding_idx=0) self.conv_11 = nn.Conv2d(1, kernel_num, (kernel_size[0], embed_dim), padding=((kernel_size[0] - 1) / 2, 0)) self.conv_12 = nn.Conv2d(1, kernel_num, (kernel_size[1], embed_dim), padding=((kernel_size[1] - 1) / 2, 0)) self.conv_13 = nn.Conv2d(1, kernel_num, (kernel_size[1], embed_dim), padding=((kernel_size[2] - 1) / 2, 0)) self.att_1 = nn.Linear()
Example #6
Source File: model.py From VSE-C with MIT License | 6 votes |
def __init__(self, vocab_size, word_dim, embed_size, use_abs=False, glove_path='data/glove.pkl'): super(EncoderTextDeepCNN, self).__init__() self.use_abs = use_abs self.embed_size = embed_size # word embedding self.embed = nn.Embedding(vocab_size, word_dim-300, padding_idx=0) _, embed_weight = pickle.load(open(glove_path, 'rb')) self.glove = Variable(torch.cuda.FloatTensor(embed_weight), requires_grad=False) channel_num = embed_size self.conv1 = nn.Conv1d(word_dim, embed_size, 2, stride=2) # [batch_size, dim, 30] self.conv2 = nn.Conv1d(embed_size, embed_size, 4, stride=2) # [batch_size, dim, 14] self.conv3 = nn.Conv1d(embed_size, embed_size, 5, stride=2) # [batch_size, dim, 5] self.conv4 = nn.Conv1d(embed_size, channel_num, 5) self.drop = nn.Dropout(p=0.5) self.relu = nn.ReLU() # self.mlp = nn.Linear(embed_size, embed_size) self.init_weights()
Example #7
Source File: model.py From VSE-C with MIT License | 6 votes |
def __init__(self, vocab_size, word_dim, embed_size, use_abs=False, glove_path='data/glove.pkl'): super(EncoderTextCNN, self).__init__() self.use_abs = use_abs self.embed_size = embed_size # word embedding self.embed = nn.Embedding(vocab_size, word_dim-300, padding_idx=0) # 0 for <pad> _, embed_weight = pickle.load(open(glove_path, 'rb')) self.glove = Variable(torch.cuda.FloatTensor(embed_weight), requires_grad=False) channel_num = embed_size // 4 self.conv2 = nn.Conv1d(word_dim, channel_num, 2) self.conv3 = nn.Conv1d(word_dim, channel_num, 3) self.conv4 = nn.Conv1d(word_dim, channel_num, 4) self.conv5 = nn.Conv1d(word_dim, channel_num, 5) self.drop = nn.Dropout(p=0.5) self.relu = nn.ReLU() # self.mlp = nn.Linear(embed_size, embed_size) self.init_weights()
Example #8
Source File: model.py From VSE-C with MIT License | 6 votes |
def __init__(self, vocab_size, word_dim, embed_size, num_layers, pooling='last', use_abs=False, bid=False, glove_path='data/glove.pkl'): super(EncoderTextGRU, self).__init__() self.use_abs = use_abs self.embed_size = embed_size self.combiner = Combiner(pooling, embed_size) # word embedding self.word_dim = word_dim if word_dim > 300: self.embed = nn.Embedding(vocab_size, word_dim-300) _, embed_weight = pickle.load(open(glove_path, 'rb')) self.glove = Variable(torch.cuda.FloatTensor(embed_weight), requires_grad=False) # caption embedding self.rnn = nn.GRU(word_dim, embed_size//(2 if bid else 1), num_layers, batch_first=True, bidirectional=bid) self.init_weights()
Example #9
Source File: Embed.py From pytorch_NER_BiLSTM_CNN_CRF with Apache License 2.0 | 6 votes |
def _nn_embed(self, embed_dict, words_dict): """ :param embed_dict: :param words_dict: """ print("loading pre_train embedding by nn.Embedding for out of vocabulary.") embed = nn.Embedding(int(self.words_count), int(self.dim)) init.xavier_uniform_(embed.weight.data) embeddings = np.array(embed.weight.data) for word in words_dict: if word in embed_dict: embeddings[words_dict[word]] = np.array([float(i) for i in embed_dict[word]], dtype='float32') self.exact_count += 1 elif word.lower() in embed_dict: embeddings[words_dict[word]] = np.array([float(i) for i in embed_dict[word.lower()]], dtype='float32') self.fuzzy_count += 1 else: self.oov_count += 1 embeddings[self.padID] = 0 final_embed = torch.from_numpy(embeddings).float() return final_embed
Example #10
Source File: model.py From subword-qac with MIT License | 6 votes |
def __init__(self, config): super(LanguageModel, self).__init__() self.config = config self.ntoken = ntoken = config.ntoken self.ninp = ninp = config.ninp self.nhid = nhid = config.nhid self.nlayers = nlayers = config.nlayers self.encoder = nn.Embedding(ntoken, ninp) self.dropouti = nn.Dropout(config.dropouti) if config.dropouti > 0 else None self.lstm = LSTM([ninp] + [nhid] * nlayers, bias=False, layernorm=True, dropoutr=config.dropoutr, dropouth=config.dropouth, dropouto=config.dropouto) self.projection = nn.Linear(nhid, ninp) self.decoder = nn.Linear(ninp, ntoken) self.decoder.weight = self.encoder.weight self.init_weights()
Example #11
Source File: decoder.py From TaskBot with GNU General Public License v3.0 | 6 votes |
def __init__(self, hidden_size, embed_size, output_size, n_layers=1, dropout=0.1): """ Args: hidden_size: GRU hidden_size embed_size: embedding size output_size: outputs vocab size n_layers: GRU layers dropout: dropout ratio, """ super(AttnDecoder, self).__init__() # Define parameters self.hidden_size = hidden_size self.embed_size = embed_size self.output_size = output_size self.n_layers = n_layers self.dropout = dropout # Define layers self.embedding = nn.Embedding(output_size, embed_size) self.dropout_layer = nn.Dropout(dropout) self.attn = Attn('concat', hidden_size) self.gru = nn.GRU(hidden_size + embed_size, hidden_size, n_layers, dropout=dropout) self.out = nn.Linear(hidden_size, output_size)
Example #12
Source File: encoder.py From TaskBot with GNU General Public License v3.0 | 6 votes |
def __init__(self, input_size, embed_size, hidden_size, n_layers=1, dropout=0.3): """initialize encoder Args: input_size: <int>, encoder vocab size embed_size: <int>, encoder embed size hidden_size: <int>, GRU hidden state size n_layers: <int>, GRU layers dropout: <float>, dropout rate Notes: default batch_first, bidirectional=True """ super().__init__() self.input_size = input_size self.hidden_size = hidden_size self.embed_size = embed_size self.n_layers = n_layers self.dropout = self.dropout self.embedding = nn.Embedding(input_size, embed_size) self.gru = nn.GRU(embed_size, hidden_size, n_layers, bidirectional=True, dropout=dropout)
Example #13
Source File: modules.py From BAMnet with Apache License 2.0 | 6 votes |
def __init__(self, vocab_size, embed_size, hidden_size, dropout=None, \ bidirectional=False, shared_embed=None, init_word_embed=None, rnn_type='lstm', use_cuda=True): super(EncoderRNN, self).__init__() if not rnn_type in ('lstm', 'gru'): raise RuntimeError('rnn_type is expected to be lstm or gru, got {}'.format(rnn_type)) if bidirectional: print('[ Using bidirectional {} encoder ]'.format(rnn_type)) else: print('[ Using {} encoder ]'.format(rnn_type)) if bidirectional and hidden_size % 2 != 0: raise RuntimeError('hidden_size is expected to be even in the bidirectional mode!') self.dropout = dropout self.rnn_type = rnn_type self.use_cuda = use_cuda self.hidden_size = hidden_size // 2 if bidirectional else hidden_size self.num_directions = 2 if bidirectional else 1 self.embed = shared_embed if shared_embed is not None else nn.Embedding(vocab_size, embed_size, padding_idx=0) model = nn.LSTM if rnn_type == 'lstm' else nn.GRU self.model = model(embed_size, self.hidden_size, 1, batch_first=True, bidirectional=bidirectional) if shared_embed is None: self.init_weights(init_word_embed)
Example #14
Source File: ner_model.py From Doc2EDAG with MIT License | 6 votes |
def __init__(self, config): super(NERModel, self).__init__() self.config = config # Word Embedding, Word Local Position Embedding self.token_embedding = NERTokenEmbedding( config.vocab_size, config.hidden_size, max_sent_len=config.max_sent_len, dropout=config.dropout ) # Multi-layer Transformer Layers to Incorporate Contextual Information self.token_encoder = transformer.make_transformer_encoder( config.num_tf_layers, config.hidden_size, ff_size=config.ff_size, dropout=config.dropout ) if self.config.use_crf_layer: self.crf_layer = CRFLayer(config.hidden_size, self.config.num_entity_labels) else: # Token Label Classification self.classifier = nn.Linear(config.hidden_size, self.config.num_entity_labels)
Example #15
Source File: rnn.py From slot-filling with MIT License | 6 votes |
def __init__(self, vocab_size, label_size, mode='elman', bidirectional=False, cuda=False, is_training=True): super(SlotFilling, self).__init__() self.is_training = is_training embedding_dim = 100 hidden_size = 75 self.embedding = nn.Embedding(vocab_size, embedding_dim) if mode == 'lstm': self.rnn = nn.LSTM(input_size=embedding_dim, hidden_size=hidden_size, bidirectional=bidirectional, batch_first=True) else: self.rnn = RNN(input_size=embedding_dim, hidden_size=hidden_size, mode=mode, cuda=cuda, bidirectional=bidirectional, batch_first=True) if bidirectional: self.fc = nn.Linear(2*hidden_size, label_size) else: self.fc = nn.Linear(hidden_size, label_size)
Example #16
Source File: att_fasttext.py From TaskBot with GNU General Public License v3.0 | 5 votes |
def __init__(self, param: dict): super().__init__(param) embed_dim = param['embed_dim'] vocab_size = param['vocab_size'] class_num = param['class_num'] self.embed = nn.Embedding(vocab_size, embed_dim) self.fc = nn.Linear(embed_dim, class_num) self.score_w = nn.Parameter(torch.randn(param['embed_dim'])) self.score_b = nn.Parameter(torch.randn(1))
Example #17
Source File: semi_sup_net.py From SEDST with MIT License | 5 votes |
def __init__(self, input_size, embed_size, hidden_size, n_layers, dropout): super(Encoder, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.embed_size = embed_size self.n_layers = n_layers self.dropout = dropout self.embedding = nn.Embedding(input_size, embed_size) self.gru = nn.GRU(embed_size, hidden_size, n_layers, dropout=self.dropout, bidirectional=True)
Example #18
Source File: att_rcnn.py From TaskBot with GNU General Public License v3.0 | 5 votes |
def __init__(self, param, *args, **kwargs): super().__init__(param, *args, **kwargs) self.lookup = nn.Embedding(param["vocab_size"], param["embed_dim"]) self.conv11 = nn.Conv2d(1, 4, kernel_size=(3, param["embed_dim"]), padding=(1, 0)) self.conv12 = nn.Conv2d(1, 4, kernel_size=(5, param["embed_dim"]), padding=(2, 0)) self.rnn = nn.GRU(param["embed_dim"], param["hidden_dim"], batch_first=True) self.fc = nn.Linear(param["hidden_dim"], param["class_num"])
Example #19
Source File: transformer.py From Doc2EDAG with MIT License | 5 votes |
def __init__(self, d_model, vocab): super(Embeddings, self).__init__() self.lut = nn.Embedding(vocab, d_model) self.d_model = d_model
Example #20
Source File: semi_sup_net.py From SEDST with MIT License | 5 votes |
def __init__(self, input_size, embed_size, hidden_size, n_layers, dropout): super().__init__() self.input_size = input_size self.hidden_size = hidden_size self.embed_size = embed_size self.n_layers = n_layers self.dropout = dropout self.embedding = nn.Embedding(input_size, embed_size) self.gru = nn.GRU(embed_size, hidden_size, n_layers, dropout=self.dropout, bidirectional=True)
Example #21
Source File: lstm_attention.py From TaskBot with GNU General Public License v3.0 | 5 votes |
def __init__(self, args): self.args = args super(LSTMAttention, self).__init__() self.hidden_dim = args["rnn_dim"] self.word_embeddings = nn.Embedding(args["vocab_size"], args["embed_dim"]) # self.bidirectional = True self.dropout = nn.Dropout(0.2) self.bilstm = nn.LSTM(args["embed_dim"], self.hidden_dim, batch_first=True, num_layers=1, bidirectional=False) self.hidden2label = nn.Linear(self.hidden_dim, args["class_num"])
Example #22
Source File: ner_model.py From Doc2EDAG with MIT License | 5 votes |
def __init__(self, vocab_size, hidden_size, max_sent_len=256, dropout=0.1): super(NERTokenEmbedding, self).__init__() self.token_embedding = nn.Embedding(vocab_size, hidden_size) self.pos_embedding = nn.Embedding(max_sent_len, hidden_size) self.layer_norm = transformer.LayerNorm(hidden_size) self.dropout = nn.Dropout(dropout)
Example #23
Source File: fast_text.py From TaskBot with GNU General Public License v3.0 | 5 votes |
def __init__(self, param: dict): super().__init__(param) embed_dim = param['embed_dim'] vocab_size = param['vocab_size'] class_num = param['class_num'] self.embed = nn.Embedding(vocab_size, embed_dim) self.fc = nn.Linear(embed_dim, class_num) self.dropout = nn.Dropout(param["dropout"])
Example #24
Source File: cheby_net.py From LanczosNetwork with MIT License | 5 votes |
def __init__(self, config): super(ChebyNet, self).__init__() self.config = config self.input_dim = config.model.input_dim self.hidden_dim = config.model.hidden_dim self.output_dim = config.model.output_dim self.num_layer = config.model.num_layer self.polynomial_order = config.model.polynomial_order self.num_atom = config.dataset.num_atom self.num_edgetype = config.dataset.num_bond_type self.dropout = config.model.dropout if hasattr(config.model, 'dropout') else 0.0 dim_list = [self.input_dim] + self.hidden_dim + [self.output_dim] self.filter = nn.ModuleList([ nn.Linear(dim_list[tt] * (self.polynomial_order + self.num_edgetype + 1), dim_list[tt + 1]) for tt in range(self.num_layer) ] + [nn.Linear(dim_list[-2], dim_list[-1])]) self.embedding = nn.Embedding(self.num_atom, self.input_dim) # attention self.att_func = nn.Sequential(*[nn.Linear(dim_list[-2], 1), nn.Sigmoid()]) if config.model.loss == 'CrossEntropy': self.loss_func = torch.nn.CrossEntropyLoss() elif config.model.loss == 'MSE': self.loss_func = torch.nn.MSELoss() elif config.model.loss == 'L1': self.loss_func = torch.nn.L1Loss() else: raise ValueError("Non-supported loss function!") self._init_param()
Example #25
Source File: model.py From TaskBot with GNU General Public License v3.0 | 5 votes |
def __init__(self, hidden_size, output_size, dropout, max_length): super().__init__() self.hidden_size = hidden_size self.output_size = output_size self.max_length = max_length self.embedding = nn.Embedding(self.output_size, self.hidden_size) self.attn = nn.Linear(self.hidden_size * 2, self.max_length) self.attn_combine = nn.Linear(self.hidden_size * 2, self.hidden_size) self.dropout = nn.Dropout(dropout) self.gru = nn.GRU(self.hidden_size, self.hidden_size) self.out = nn.Linear(self.hidden_size, self.output_size)
Example #26
Source File: model.py From TaskBot with GNU General Public License v3.0 | 5 votes |
def __init__(self, input_size, hidden_size): super(Encoder, self).__init__() self.hidden_size = hidden_size self.embedding = nn.Embedding(input_size, hidden_size) self.gru = nn.GRU(hidden_size, hidden_size)
Example #27
Source File: tutorial.py From TaskBot with GNU General Public License v3.0 | 5 votes |
def __init__(self, hidden_size, output_size, dropout_p=0.1, max_length=MAX_LENGTH): super(AttnDecoderRNN, self).__init__() self.hidden_size = hidden_size self.output_size = output_size self.dropout_p = dropout_p self.max_length = max_length self.embedding = nn.Embedding(self.output_size, self.hidden_size) self.attn = nn.Linear(self.hidden_size * 2, self.max_length) self.attn_combine = nn.Linear(self.hidden_size * 2, self.hidden_size) self.dropout = nn.Dropout(self.dropout_p) self.gru = nn.GRU(self.hidden_size, self.hidden_size) self.out = nn.Linear(self.hidden_size, self.output_size)
Example #28
Source File: tutorial.py From TaskBot with GNU General Public License v3.0 | 5 votes |
def __init__(self, input_size, hidden_size): super(EncoderRNN, self).__init__() self.hidden_size = hidden_size self.embedding = nn.Embedding(input_size, hidden_size) self.gru = nn.GRU(hidden_size, hidden_size)
Example #29
Source File: norms.py From JEM with Apache License 2.0 | 5 votes |
def __init__(self, num_features, num_classes): super().__init__() self.num_features = num_features self.num_classes = num_classes self.embed = nn.Embedding(num_classes, num_features * 2) self.embed.weight.data.zero_() self.init = False
Example #30
Source File: norms.py From JEM with Apache License 2.0 | 5 votes |
def __init__(self, num_features, num_classes, bias=True): super().__init__() self.num_features = num_features self.bias = bias self.instance_norm = nn.InstanceNorm2d(num_features, affine=False, track_running_stats=False) if bias: self.embed = nn.Embedding(num_classes, num_features * 3) self.embed.weight.data[:, :2 * num_features].normal_(1, 0.02) # Initialise scale at N(1, 0.02) self.embed.weight.data[:, 2 * num_features:].zero_() # Initialise bias at 0 else: self.embed = nn.Embedding(num_classes, 2 * num_features) self.embed.weight.data.normal_(1, 0.02)