Exemplo n.º 1
0
 def predict(self, left, right):
     """
     Forward network
     """
     # embedding layer
     emb_layer = layers.EmbeddingLayer(self.dict_size, self.emb_dim, "emb")
     left_emb = emb_layer.ops(left)
     right_emb = emb_layer.ops(right)
     # Presentation context
     lstm_layer = layers.DynamicLSTMLayer(self.lstm_dim, "lstm")
     left_lstm = lstm_layer.ops(left_emb)
     right_lstm = lstm_layer.ops(right_emb)
     last_layer = layers.SequenceLastStepLayer()
     left_last = last_layer.ops(left_lstm)
     right_last = last_layer.ops(right_lstm)
     # matching layer
     if self.task_mode == "pairwise":
         relu_layer = layers.FCLayer(self.hidden_dim, "relu", "relu")
         left_relu = relu_layer.ops(left_last)
         right_relu = relu_layer.ops(right_last)
         cos_sim_layer = layers.CosSimLayer()
         pred = cos_sim_layer.ops(left_relu, right_relu)
         return left_relu, pred
     else:
         concat_layer = layers.ConcatLayer(1)
         concat = concat_layer.ops([left_last, right_last])
         relu_layer = layers.FCLayer(self.hidden_dim, "relu", "relu")
         concat_fc = relu_layer.ops(concat)
         softmax_layer = layers.FCLayer(2, "softmax", "cos_sim")
         pred = softmax_layer.ops(concat_fc)
         return left_last, pred
Exemplo n.º 2
0
 def predict(self, left, right):
     """
     Forward network
     """
     # embedding layer
     emb_layer = layers.EmbeddingLayer(self.dict_size, self.emb_dim, "emb")
     left_emb = emb_layer.ops(left)
     right_emb = emb_layer.ops(right)
     # Presentation context
     gru_layer = layers.DynamicGRULayer(self.gru_dim, "gru")
     left_gru = gru_layer.ops(left_emb)
     right_gru = gru_layer.ops(right_emb)
     last_layer = layers.SequenceLastStepLayer()
     left_last = last_layer.ops(left_gru)
     right_last = last_layer.ops(right_gru)
     tanh_layer = layers.FCLayer(self.hidden_dim, "tanh", "tanh")
     left_tanh = tanh_layer.ops(left_last)
     right_tanh = tanh_layer.ops(right_last)
     # matching layer
     if self.task_mode == "pairwise":
         cos_sim_layer = layers.CosSimLayer()
         pred = cos_sim_layer.ops(left_tanh, right_tanh)
     else:
         concat_layer = layers.ConcatLayer(1)
         concat = concat_layer.ops([left_tanh, right_tanh])
         softmax_layer = layers.FCLayer(2, "softmax", "cos_sim")
         pred = softmax_layer.ops(concat)
     return left_tanh, pred
Exemplo n.º 3
0
 def predict(self, left, right):
     """
     Forward network
     """
     # embedding layer
     emb_layer = layers.EmbeddingLayer(self.dict_size, self.emb_dim, "emb")
     left_emb = emb_layer.ops(left)
     right_emb = emb_layer.ops(right)
     # Presentation context
     pool_layer = layers.SequencePoolLayer("sum")
     left_pool = pool_layer.ops(left_emb)
     right_pool = pool_layer.ops(right_emb)
     softsign_layer = layers.SoftsignLayer()
     left_soft = softsign_layer.ops(left_pool)
     right_soft = softsign_layer.ops(right_pool)
     bow_layer = layers.FCLayer(self.bow_dim, None, "tanh")
     left_bow = bow_layer.ops(left_soft)
     right_bow = bow_layer.ops(right_soft)
     # matching layer
     if self.task_mode == "pairwise":
         cos_sim_layer = layers.CosSimLayer()
         pred = cos_sim_layer.ops(left_bow, right_bow)
     else:
         concat_layer = layers.ConcatLayer(1)
         concat = concat_layer.ops([left_bow, right_bow])
         softmax_layer = layers.FCLayer(2, "softmax", "cos_sim")
         pred = softmax_layer.ops(concat)
     return left_bow, pred
Exemplo n.º 4
0
 def predict(self, left, right):
     """
     Forward network
     """
     # embedding layer
     emb_layer = layers.EmbeddingLayer(self.dict_size, self.emb_dim, "emb")
     left_emb = emb_layer.ops(left)
     right_emb = emb_layer.ops(right)
     # Presentation context
     cnn_layer = layers.SequenceConvPoolLayer(
         self.filter_size, self.num_filters, "conv")
     left_cnn = cnn_layer.ops(left_emb)
     right_cnn = cnn_layer.ops(right_emb)
     # matching layer
     if self.task_mode == "pairwise":
         relu_layer = layers.FCLayer(self.hidden_dim, "relu", "relu")
         left_relu = relu_layer.ops(left_cnn)
         right_relu = relu_layer.ops(right_cnn)
         cos_sim_layer = layers.CosSimLayer()
         pred = cos_sim_layer.ops(left_relu, right_relu)
         return left_relu, pred
     else:
         concat_layer = layers.ConcatLayer(1)
         concat = concat_layer.ops([left_cnn, right_cnn])
         relu_layer = layers.FCLayer(self.hidden_dim, "relu", "relu")
         concat_fc = relu_layer.ops(concat)
         softmax_layer = layers.FCLayer(2, "softmax", "cos_sim")
         pred = softmax_layer.ops(concat_fc)
         return left_cnn, pred