Esempio n. 1
0
 def _preprocess(self,
                 Xw,
                 Xw_len,
                 Xc,
                 Xc_len,
                 scope="preprocess_layers",
                 reuse=False):
     with tf.variable_scope(scope, reuse=reuse):
         Xw_embedded, size_w = embedded(Xw,
                                        self.embeddings_w[0],
                                        self.embeddings_w[1],
                                        self.config.wv_config["train_w"],
                                        scope="embedded_w")
         Xc_embedded, size_c = embedded(Xc,
                                        self.embeddings_c[0],
                                        self.embeddings_c[1],
                                        self.config.wv_config["train_c"],
                                        scope="embedded_c")
         batch_size, seq_len = tf.shape(Xw)[0], tf.shape(Xw)[1]
         Xc_embedded = tf.reshape(Xc_embedded,
                                  shape=[batch_size * seq_len, -1, size_c])
         Xc_len = tf.reshape(Xc_len, shape=[
             batch_size * seq_len,
         ])
         Xc_embedded, size_c = bi_gru(Xc_embedded, Xc_len,
                                      (self.config.char_dim, ), 2,
                                      self.initializer, 1.0, "bi_gru_c2w")
         Xc_embedded = tf.reshape(Xc_embedded,
                                  shape=[batch_size, seq_len, size_c])
         X_embedded = tf.concat([Xw_embedded, Xc_embedded], axis=-1)
         out_w, out_w_size = bi_gru(X_embedded, Xw_len,
                                    (self.config.bi_dim, ), 1,
                                    self.initializer, 1.0, "bi_gru__wc")
         return out_w, out_w_size
Esempio n. 2
0
 def _encode(self, Xw, Xw_l, Xc, Xc_l, scope="encode_layers", reuse=False):
     with tf.variable_scope(scope, reuse=reuse):
         Xw_embedded, size_w = embedded(Xw,
                                        self.embeddings_w[0],
                                        self.embeddings_w[1],
                                        self.config.wv_config["train_w"],
                                        scope="embedded_w")
         Xc_embedded, size_c = embedded(Xc,
                                        self.embeddings_c[0],
                                        self.embeddings_c[1],
                                        self.config.wv_config["train_c"],
                                        scope="embedded_c")
         batch_size = tf.shape(Xw)[0]
         # char
         v0, v0_size = attention_han(Xc_embedded, self.config.un_dim,
                                     self.initializer, "attention_han_c")
         v1, v1_size = bi_gru(Xc_embedded, Xc_l, (self.config.bi_dim, ), 2,
                              self.initializer, 1.0, "bi_gru_c")
         char_v = tf.reshape(tf.concat([v0, v1], axis=-1),
                             [batch_size, v0_size + v1_size])
         # word
         v0, v0_size = attention_han(Xw_embedded, self.config.un_dim,
                                     self.initializer, "attention_han_w")
         v1, v1_size = bi_gru(Xw_embedded, Xw_l, (self.config.bi_dim, ), 2,
                              self.initializer, 1.0, "bi_gru_w")
         word_v = tf.reshape(tf.concat([v0, v1], axis=-1),
                             [batch_size, v0_size + v1_size])
         # phrase
         Xp_embedded, size_p = conv_with_max_pool(Xw_embedded, (2, 3, 4, 5),
                                                  size_w // 4, False,
                                                  tf.nn.selu,
                                                  self.initializer,
                                                  "conv_w2p")
         v0, v0_size = attention_han(Xp_embedded, self.config.un_dim,
                                     self.initializer, "attention_han_p")
         v1, v1_size = bi_gru(Xp_embedded, Xw_l, (self.config.bi_dim, ), 2,
                              self.initializer, 1.0, "bi_gru_p")
         phrase_v = tf.reshape(tf.concat([v0, v1], axis=-1),
                               [batch_size, v0_size + v1_size])
         return char_v, word_v, phrase_v