Skip to content

Commit

Permalink
fix tiny errors
Browse files Browse the repository at this point in the history
  • Loading branch information
l11x0m7 committed May 7, 2018
1 parent 7c89cf3 commit f726884
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
29 changes: 10 additions & 19 deletions code/models.py
Expand Up @@ -3,9 +3,6 @@
import numpy as np

class SiameseNN(object):
"""
pairwise学习模型
"""
def __init__(self, config):
self.config = config
# 输入
Expand Down Expand Up @@ -102,9 +99,6 @@ def add_train_op(self, loss):


class SiameseCNN(object):
"""
pairwise学习模型
"""
def __init__(self, config):
self.config = config
# 输入
Expand All @@ -113,7 +107,7 @@ def __init__(self, config):
q_embed, a_embed = self.add_embeddings()
with tf.variable_scope('siamese') as scope:
self.q_trans = self.network(q_embed, reuse=False)
tf.get_variable_scope().reuse_variables()
scope.reuse_variables()
self.a_trans = self.network(a_embed, reuse=True)
# 损失和精确度
self.total_loss = self.add_loss_op(self.q_trans, self.a_trans)
Expand Down Expand Up @@ -163,7 +157,7 @@ def fc_layer(self, bottom, n_weight, name):
n_prev_weight = bottom.get_shape()[1]
initer = tf.truncated_normal_initializer(stddev=0.01)
W = tf.get_variable(name+'W', dtype=tf.float32, shape=[n_prev_weight, n_weight], initializer=initer)
b = tf.get_variable(name+'b', dtype=tf.float32, initializer=tf.constant(0.01, shape=[n_weight], dtype=tf.float32))
b = tf.get_variable(name+'b', dtype=tf.float32, initializer=tf.constant(0.0, shape=[n_weight], dtype=tf.float32))
fc = tf.nn.bias_add(tf.matmul(bottom, W), b)
return fc

Expand All @@ -173,10 +167,10 @@ def conv_layer(self, h, reuse=False):
h = tf.reshape(h, [-1, max_len, h.get_shape()[2], 1])
for i, filter_size in enumerate(self.config.filter_sizes):
with tf.variable_scope('filter{}'.format(filter_size)):
conv1_W = tf.get_variable('conv_W', shape=[filter_size, self.config.embedding_size, 1, self.config.num_filters], initializer=tf.truncated_normal_initializer(.0, .1))
conv1_b = tf.get_variable('conv_b', initializer=tf.constant(0.1, shape=[self.config.num_filters]))
conv1_W = tf.get_variable('conv_W', shape=[filter_size, self.config.embedding_size, 1, self.config.num_filters], initializer=tf.truncated_normal_initializer(.0, .01))
conv1_b = tf.get_variable('conv_b', initializer=tf.constant(0.0, shape=[self.config.num_filters]))
# pooling层的bias,Q和A分开
pool_b = tf.get_variable('pool_b', initializer=tf.constant(0.1, shape=[self.config.num_filters]))
pool_b = tf.get_variable('pool_b', initializer=tf.constant(0.0, shape=[self.config.num_filters]))
# 卷积
out = tf.nn.relu((tf.nn.conv2d(h, conv1_W, [1,1,1,1], padding='VALID')+conv1_b))
# 池化
Expand All @@ -185,10 +179,10 @@ def conv_layer(self, h, reuse=False):
pool.append(out)
# 加入正则项
if not reuse:
tf.add_to_collection('total_loss', 0.5*self.config.l2_reg_lambda*tf.nn.l2_loss(conv1_W))
tf.add_to_collection('total_loss', 0.5 * self.config.l2_reg_lambda * tf.nn.l2_loss(conv1_W))

total_channels = len(self.config.filter_sizes)*self.config.num_filters
real_pool = tf.reshape(tf.concat(pool, 3), [-1, total_channels])
total_channels = len(self.config.filter_sizes) * self.config.num_filters
real_pool = tf.reshape(tf.concat(pool, 3), [self.batch_size, total_channels])
return real_pool

# 损失节点
Expand Down Expand Up @@ -220,9 +214,6 @@ def add_train_op(self, loss):


class SiameseRNN(object):
"""
pairwise学习模型
"""
def __init__(self, config):
self.config = config
# 输入
Expand Down Expand Up @@ -272,7 +263,7 @@ def network(self, x):
inputs = tf.transpose(x, [1, 0, 2])
inputs = tf.reshape(inputs, [-1, self.config.embedding_size])
inputs = tf.split(inputs, sequence_length, 0)
# (batch_size, conv_size)
# (batch_size, rnn_output_size)
rnn1 = self.rnn_layer(inputs)
# (batch_size, hidden_size)
fc1 = self.fc_layer(rnn1, self.config.hidden_size, "fc1")
Expand Down Expand Up @@ -494,4 +485,4 @@ def add_train_op(self, loss):
self.global_step = tf.Variable(0, name='global_step', trainable=False)
opt = tf.train.AdamOptimizer(self.config.lr)
train_op = opt.minimize(loss, self.global_step)
return train_op
return train_op
8 changes: 7 additions & 1 deletion code/preprocess_wiki.ipynb
Expand Up @@ -74,6 +74,7 @@
}
],
"source": [
"# 分词、词干化处理\n",
"def segment(filename, use_lemma=True):\n",
" processed_qa = []\n",
" count = 0\n",
Expand All @@ -94,6 +95,7 @@
" print('Finished {}'.format(count))\n",
" return processed_qa\n",
"\n",
"# 构建词典\n",
"def build_vocab(corpus, topk=None):\n",
" vocab = Counter()\n",
" for line in corpus:\n",
Expand All @@ -110,6 +112,7 @@
" reverse_vocab = dict(zip(vocab.values(), vocab.keys()))\n",
" return vocab, reverse_vocab\n",
"\n",
"# 将每个词映射为词典中的id\n",
"def transform(corpus, word2id, unk_id=1):\n",
" transformed_corpus = []\n",
" for line in corpus:\n",
Expand All @@ -119,6 +122,7 @@
" transformed_corpus.append([qid, q, aid, a, int(label)])\n",
" return transformed_corpus\n",
"\n",
"# 得到pointwise形式的数据,即(Q, A, label)\n",
"def pointwise_data(corpus, keep_ids=False):\n",
" # (q, a, label)\n",
" pointwise_corpus = []\n",
Expand All @@ -130,6 +134,7 @@
" pointwise_corpus.append((q, a, label))\n",
" return pointwise_corpus\n",
"\n",
"# 得到pairwise形式的数据,即(Q, positive A, negative A)\n",
"def pairwise_data(corpus):\n",
" # (q, a_pos, a_neg), two answers must from the same q\n",
" # once a question contains no positive answers, we discard this sample.\n",
Expand All @@ -152,6 +157,7 @@
" real_pairwise_corpus.append((q, pos, neg))\n",
" return real_pairwise_corpus\n",
" \n",
"# 得到listwise形式的数据,即(Q, All answers related to this Q)\n",
"def listwise_data(corpus):\n",
" # (q, a_list)\n",
" listwise_corpus = dict()\n",
Expand All @@ -168,7 +174,7 @@
" real_listwise_corpus.append((q, alist))\n",
" return real_listwise_corpus\n",
"\n",
" \n",
"\n",
"train_processed_qa = segment('WikiQA-train.tsv')\n",
"val_processed_qa = segment('WikiQA-dev.tsv')\n",
"test_processed_qa = segment('WikiQA-test.tsv')\n",
Expand Down
12 changes: 6 additions & 6 deletions code/siamese.py
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import os
import sys
from copy import deepcopy
stdout = sys.stdout
reload(sys)
sys.stdout = stdout
Expand Down Expand Up @@ -61,15 +62,15 @@ def __init__(self, vocab_size, embeddings=None):
self.embedding_size = 100
if self.embeddings is not None:
self.embedding_size = embeddings.shape[1]
# 不同类型的filter,相当于1-gram,2-gram,3-gram和5-gram
# 不同类型的filter,对应不同的尺寸
self.filter_sizes = [1, 2, 3, 5, 7, 9]
# 隐层大小
self.hidden_size = 256
self.output_size = 128
# 每种filter的数量
self.num_filters = 128
self.l2_reg_lambda = 0.
self.keep_prob = 0.6
self.keep_prob = 0.8
# 学习率
self.lr = 0.001
# contrasive loss 中的 positive loss部分的权重
Expand Down Expand Up @@ -105,7 +106,6 @@ def __init__(self, vocab_size, embeddings=None):
self.output_size = 128
# 每种filter的数量
self.num_filters = 128
self.l2_reg_lambda = 0.
self.keep_prob = 0.6
# 学习率
self.lr = 0.001
Expand Down Expand Up @@ -142,7 +142,7 @@ def train(train_corpus, config, val_corpus, eval_train_corpus=None):
count += 1
if count % 10 == 0:
print('[epoch {}, batch {}]Loss:{}'.format(epoch, count, loss))
saver.save(sess,'models/siamese_nn/my_model', global_step=epoch)
saver.save(sess,'models/siamese_{}/my_model'.format(args.model.lower()), global_step=epoch)
if eval_train_corpus is not None:
train_res = evaluate(sess, model, eval_train_corpus, config)
print('[train] ' + train_res)
Expand Down Expand Up @@ -195,7 +195,7 @@ def test(corpus, config):
else:
model = SiameseRNN(config)
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint('models/qacnn'))
saver.restore(sess, tf.train.latest_checkpoint('models/siamese_{}'.format(args.model)))
print('[test] ' + evaluate(sess, model, corpus, config))


Expand Down Expand Up @@ -234,7 +234,7 @@ def main(args):
config.max_q_length = max_q_length
config.max_a_length = max_a_length
if args.train:
train(train_corpus, config, val_corpus, train_corpus)
train(deepcopy(train_corpus), config, val_corpus, deepcopy(train_corpus))
elif args.test:
test(test_corpus, config)

Expand Down

0 comments on commit f726884

Please sign in to comment.