def _define_parameters(self): with self.name_scope(): self.latent2hid = Dense(in_units=self.config.latent_dim, units=self.config.lstm_config.hidden_dim * 2) self.class2hid = Embedding( input_dim=self.config.num_classes, output_dim=self.config.lstm_config.hidden_dim * 2) self.embedding = Embedding( input_dim=self.config.output_dim, output_dim=self.config.lstm_config.hidden_dim) self.decoder = LSTM( self.config.lstm_config.hidden_dim, # // 2, self.config.lstm_config.n_layers, bidirectional=False, dropout=self.config.lstm_config.dropout, input_size=self.config.lstm_config.hidden_dim, layout='NTC') self.output_layer = Dense( in_units=self.config.lstm_config.hidden_dim, units=self.config.output_dim, flatten=False)
def __init__(self, word_vocab_size, word_vec_size, nature_vocab_size, nature_vec_size, hidden_dim, num_layers, tag2idx, drop_prob, **kwargs): super(LSTM_CRF, self).__init__(**kwargs) with self.name_scope(): self.drop_prob = drop_prob self.word_embdding = Embedding(word_vocab_size, word_vec_size) self.nature_embedding = Embedding(nature_vocab_size, nature_vec_size) self.lstm_layer = rnn.LSTM(hidden_dim, num_layers=num_layers, dropout=drop_prob, bidirectional=True) self.dense = Dense(len(tag2idx)) self.crf = CRF(tag2idx)
def __init__(self, config: EncoderConfig): super().__init__() self.config = config with self.name_scope(): self.class2hid = Embedding( input_dim=self.config.num_classes, output_dim=self.config.transformer_config.model_size) self.encoder_embedding = Embedding( input_dim=self.config.input_dim, output_dim=self.config.transformer_config.model_size) self.encoder = TransformerEncoder(self.config.transformer_config) self.latent_proj = Dense( in_units=self.config.transformer_config.model_size, units=self.config.latent_dim * 2)
def _define_parameters(self): with self.name_scope(): self.latent2hid = Dense( in_units=self.config.latent_dim, units=self.config.transformer_config.model_size) self.class2hid = Embedding( input_dim=self.config.num_classes, output_dim=self.config.transformer_config.model_size) self.embedding = Embedding( input_dim=self.config.output_dim, output_dim=self.config.transformer_config.model_size) self.decoder = TransformerDecoder(self.config.transformer_config) self.output_layer = Dense( in_units=self.config.transformer_config.model_size, units=self.config.output_dim, flatten=False)
def create_model(self) -> Sequential: embedding_size = 100 model = Sequential() with model.name_scope(): # input shape is (batch_size,), output shape is (batch_size, embedding_size) model.add( Embedding(input_dim=self.vocab_size, output_dim=embedding_size)) model.add(Dropout(0.2)) # layout : str, default 'TNC' # The format of input and output tensors. # T, N and C stand for sequence length, batch size, and feature dimensions respectively. # Change it to NTC so that the input shape can be (batch_size, sequence_length, embedding_size) model.add(LSTM(hidden_size=64, layout='NTC', bidirectional=True)) model.add(Dense(len(self.labels))) return model
def get_model(vocab_size, embedding_size, hidden_size, dropout_rate, classes=3): net = HybridSequential() with net.name_scope(): net.add(Embedding(vocab_size, embedding_size)) net.add(Dropout(args.dropout)) net.add( LSTM(hidden_size=hidden_size // 2, num_layers=1, layout='NTC', bidirectional=True, dropout=dropout_rate)) net.add(Dense(units=classes, flatten=False)) return net
#read vocab with open("/root/PycharmProjects/sigir/baseline/dual_lstm/vocab/word2id_file",'rb') as f: word2id = pickle.load(f) with open("/root/PycharmProjects/sigir/baseline/dual_lstm/vocab/id2word_file",'rb') as ff: id2word = pickle.load(ff) vocab_size = len(id2word) #gen batch and val_set data,label = build_input_data(sentences=raw_data,labels=raw_label,vocabulary=word2id) train_dataset = gluon.data.ArrayDataset(data[0:9000], label[0:9000]) data_iter = gluon.data.DataLoader(train_dataset, 512, shuffle=True) post_net = gluon.nn.Sequential() with post_net.name_scope(): post_net.add(Embedding(input_dim=vocab_size,output_dim=1000)) post_net.add(LSTM(hidden_size=1000,layout='NTC',num_layers=2,bidirectional = False)) post_net.initialize() resp_net = gluon.nn.Sequential() with resp_net.name_scope(): resp_net.add(Embedding(input_dim=vocab_size,output_dim=1000)) resp_net.add(LSTM(hidden_size=1000,layout='NTC',num_layers=2,bidirectional = False)) resp_net.initialize() mlp_net = gluon.nn.Sequential() with mlp_net.name_scope(): mlp_net.add(gluon.nn.Dense(2,flatten=True)) mlp_net.initialize()