Skip to content

Commit

Permalink
Add codes for Phase 1 without data augmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
plkumjorn committed Mar 22, 2019
1 parent 79ed0a7 commit 4be2a49
Show file tree
Hide file tree
Showing 4 changed files with 829 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,17 @@ The locations of the result files are specified by config.\{zhang15_dbpedia, new

### How to train / test Phase 1

Pending
- Without data augmentation: an example
```bash
python3 train_reject.py \
--data dbpedia \
--unseen 0.5 \
--model vw \
--nepoch 3 \
--rgidx 1 \
--train 1
```


### How to train / test the traditional classifier in Phase 2

Expand Down
1 change: 1 addition & 0 deletions src_reject/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
parser.add_argument("--ns", type=int, default=2, required=False, help="negative samples: integer, the ratio of positive and negative samples, the higher the more negative samples")
parser.add_argument("--ni", type=int, default=2, required=False, help="negative increase: integer, the speed of increasing negative samples during training per epoch")
parser.add_argument("--sepoch", type=int, required=False, help="small epoch: integer, repeat training of each epoch for several times so that the ratio of posi/negative, learning rate both keep the same")
parser.add_argument("--nepoch", type=int, default = 5, required=False, help="number of epochs for training")
parser.add_argument("--rgidx", type=int, default=1, required=False, help="random group starting index: e.g. if 5, the training will start from the 5th random group, by default 1")
parser.add_argument("--train", type=int, required=False, help="train or not")
parser.add_argument("--gpu", type=float, default=1.0, required=False, help="gpu occupation percentage")
Expand Down
178 changes: 178 additions & 0 deletions src_reject/model_reject.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import os
import tensorflow as tf
import tensorlayer as tl
from tensorlayer.layers import \
Layer, \
InputLayer, Conv1d, MaxPool1d, \
Conv1dLayer, \
RNNLayer, DropoutLayer, DenseLayer, \
LambdaLayer, ReshapeLayer, ConcatLayer, \
Conv2d, MaxPool2d, FlattenLayer, \
DeConv2d, BatchNormLayer, EmbeddingInputlayer, \
Seq2Seq, retrieve_seq_length_op2, DynamicRNNLayer, \
retrieve_seq_length_op

import numpy as np
import logging

import config

class Model4Reject():

def __init__(
self,
model_name,
start_learning_rate,
decay_rate,
decay_steps,
main_class,
seen_classes,
unseen_classes,
word_embedding_dim=config.word_embedding_dim,
max_length=config.max_length
):
self.model_name = model_name
self.start_learning_rate = start_learning_rate
self.decay_rate = decay_rate
self.decay_steps = decay_steps
self.word_embedding_dim = word_embedding_dim
self.max_length = max_length
self.main_class = main_class
self.seen_classes = seen_classes
self.unseen_classes = unseen_classes
self.threshold = 0.5

self.__create_placeholders__()
self.__create_model__()
self.__create_loss__()
self.__create_training_op__()

def __create_placeholders__(self):
# the placeholder for inputs
self.encode_seqs = tf.placeholder(dtype=tf.float32, shape=[None, self.max_length, self.word_embedding_dim], name="encode_seqs")
self.label_logits = tf.placeholder(dtype=tf.float32, shape=[None, 1], name="label_logits")

# self.encode_seqs_anc = tf.placeholder(dtype=tf.float32, shape=[None, self.max_length, self.word_embedding_dim], name="encode_seqs_anc")
# self.encode_seqs_pos = tf.placeholder(dtype=tf.float32, shape=[None, self.max_length, self.word_embedding_dim], name="encode_seqs_pos")
# self.encode_seqs_neg = tf.placeholder(dtype=tf.float32, shape=[None, self.max_length, self.word_embedding_dim], name="encode_seqs_neg")
# pass

def __create_model__(self):
# train_net
# self.train_net = self.__get_network__(self.model_name, self.encode_seqs, reuse=False, is_train=True)
self.train_net, _ = self.__get_network__(self.model_name, self.encode_seqs, reuse=False, is_train=True)
# _, self.cnn_anchor = self.__get_network__(self.model_name, self.encode_seqs_anc, reuse=True, is_train=True)
# _, self.cnn_pos = self.__get_network__(self.model_name, self.encode_seqs_pos, reuse=True, is_train=True)
# _, self.cnn_neg = self.__get_network__(self.model_name, self.encode_seqs_neg, reuse=True, is_train=True)

# test_net
# self.test_net = self.__get_network__(self.model_name, self.encode_seqs, reuse=True, is_train=False)
self.test_net, _ = self.__get_network__(self.model_name, self.encode_seqs, reuse=True, is_train=False)

# pass

def __get_network__(self, model_name, encode_seqs, reuse=False, is_train=True):
# the architecture of networks
with tf.variable_scope(model_name, reuse=reuse):
# tl.layers.set_name_reuse(reuse)
net_in = InputLayer(
inputs=encode_seqs,
name="in_word_embed"
)

filter_length = [3, 4, 5]
n_filter = 200
net_cnn_list = list()
for fsz in filter_length:
net_cnn = Conv1d(
net_in,
n_filter=n_filter,
filter_size=fsz,
stride=1,
act=tf.nn.relu,
name="cnn%d" % fsz
)
net_cnn.outputs = tf.reduce_max(net_cnn.outputs, axis=1, name="global_maxpool%d" % fsz)
net_cnn_list.append(net_cnn)

net_cnn = ConcatLayer(net_cnn_list, concat_dim=-1)
net_fc = DenseLayer(
net_cnn,
n_units=300,
act=tf.nn.relu,
name="fc_1"
)

net_fc = DenseLayer(
net_fc,
n_units=1,
act=tf.nn.sigmoid,
name="fc_2"
)
return net_fc, net_cnn
# return net_fc
# pass

def __create_loss__(self):
# loss function
# train_loss
train_predicted_logits = self.train_net.outputs
self.cross_entropy_loss = tl.cost.binary_cross_entropy(
output=train_predicted_logits,
target=self.label_logits,
)

# anchor_output = self.cnn_anchor.outputs
# positive_output = self.cnn_pos.outputs
# negative_output = self.cnn_neg.outputs

# d_pos = tf.reduce_sum(tf.square(anchor_output - positive_output), 1)
# d_neg = tf.reduce_sum(tf.square(anchor_output - negative_output), 1)

# triplet_loss = tf.maximum(0., 0.1 + d_pos - d_neg)
# self.triplet_loss = tf.reduce_mean(triplet_loss)

self.train_loss = self.cross_entropy_loss
# self.train_loss = self.cross_entropy_loss + self.triplet_loss


# test_loss if necessary
test_predicted_logits = self.test_net.outputs
self.test_loss = tl.cost.binary_cross_entropy(
output=test_predicted_logits,
target=self.label_logits,
)
# pass

def __create_training_op__(self):
# learning rate operators
self.global_step = tf.placeholder(
dtype=tf.int32,
shape=[],
name="global_step"
)

self.learning_rate = tf.train.exponential_decay(
learning_rate=self.start_learning_rate,
global_step=self.global_step,
decay_steps=self.decay_steps,
decay_rate=self.decay_rate,
staircase=True,
name="learning_rate"
)

# optim operators
self.optim = tf.train.AdamOptimizer(self.learning_rate, beta1=0.5) \
.minimize(self.train_loss, var_list=self.train_net.all_params)
# self.optim = tf.train.AdamOptimizer(self.learning_rate, beta1=0.5) \
# .minimize(self.train_loss, var_list=self.train_net.all_params + self.cnn_anchor.all_params + self.cnn_pos.all_params + self.cnn_neg.all_params)
# pass

if __name__ == "__main__":
model = Reject_Model(
model_name="cnn_binary_classification",
start_learning_rate=0.001,
decay_rate=0.8,
decay_steps=1000
)
pass
Loading

0 comments on commit 4be2a49

Please sign in to comment.