提交 0388d1cb 编写于 作者: F fengjiayi

some update

上级 fab6287e
...@@ -50,7 +50,7 @@ feeder = fluid.DataFeeder(feed_list=[data, label], place=fluid.CPUPlace()) ...@@ -50,7 +50,7 @@ feeder = fluid.DataFeeder(feed_list=[data, label], place=fluid.CPUPlace())
BATCH_SIZE = 128 BATCH_SIZE = 128
train_reader = paddle.batch( train_reader = paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
paddle.dataset.imdb.train(word_dict), buf_size=10000), paddle.dataset.imdb.train(word_dict), buf_size=25000),
batch_size=BATCH_SIZE) batch_size=BATCH_SIZE)
test_reader = paddle.batch( test_reader = paddle.batch(
......
...@@ -27,9 +27,6 @@ emb_dim = 128 ...@@ -27,9 +27,6 @@ emb_dim = 128
# hidden dim # hidden dim
hid_dim = 128 hid_dim = 128
# hidden dim2
hid_dim2 = 96
# class num # class num
class_dim = 2 class_dim = 2
...@@ -42,13 +39,9 @@ def build_program(is_train): ...@@ -42,13 +39,9 @@ def build_program(is_train):
filenames=TRAIN_FILES if is_train else TEST_FILES, filenames=TRAIN_FILES if is_train else TEST_FILES,
shapes=[[-1, 1], [-1, 1]], shapes=[[-1, 1], [-1, 1]],
lod_levels=[1, 0], lod_levels=[1, 0],
dtypes=['int64', 'int64'], dtypes=['int64', 'int64'])
thread_num=1)
if is_train: file_obj = fluid.layers.io.double_buffer(file_obj_handle)
file_obj = fluid.layers.io.shuffle(file_obj_handle, buffer_size=1000)
else:
file_obj = file_obj_handle
file_obj = fluid.layers.io.double_buffer(file_obj)
with fluid.unique_name.guard(): with fluid.unique_name.guard():
...@@ -56,22 +49,24 @@ def build_program(is_train): ...@@ -56,22 +49,24 @@ def build_program(is_train):
emb = fluid.layers.embedding(input=data, size=[DICT_DIM, emb_dim]) emb = fluid.layers.embedding(input=data, size=[DICT_DIM, emb_dim])
# sequence conv with window size = 3
win_size = 3
conv_3 = fluid.nets.sequence_conv_pool( conv_3 = fluid.nets.sequence_conv_pool(
input=emb, input=emb,
num_filters=hid_dim, num_filters=hid_dim,
filter_size=win_size, filter_size=3,
act="tanh", act="tanh",
pool_type="max") pool_type="sqrt")
# fc layer after conv conv_4 = fluid.nets.sequence_conv_pool(
fc_1 = fluid.layers.fc(input=[conv_3], size=hid_dim2) input=emb,
num_filters=hid_dim,
filter_size=4,
act="tanh",
pool_type="sqrt")
# probability of each class prediction = fluid.layers.fc(input=[conv_3, conv_4],
prediction = fluid.layers.fc(input=[fc_1],
size=class_dim, size=class_dim,
act="softmax") act="softmax")
# cross entropy loss # cross entropy loss
cost = fluid.layers.cross_entropy(input=prediction, label=label) cost = fluid.layers.cross_entropy(input=prediction, label=label)
...@@ -117,11 +112,9 @@ def main(): ...@@ -117,11 +112,9 @@ def main():
try: try:
batch_id = 0 batch_id = 0
while True: while True:
result = map(numpy.array, loss, acc = map(numpy.array,
train_exe.run(fetch_list=fetch_var_list train_exe.run(fetch_list=fetch_var_list))
if batch_id % 10 == 0 else [])) print 'Train epoch', epoch_id, 'batch', batch_id, 'loss:', loss, 'acc:', acc
if len(result) != 0:
print 'Train loss: ', result
batch_id += 1 batch_id += 1
except fluid.core.EOFException: except fluid.core.EOFException:
print 'End of epoch', epoch_id print 'End of epoch', epoch_id
...@@ -138,7 +131,7 @@ def main(): ...@@ -138,7 +131,7 @@ def main():
acc.append(acc_np[0]) acc.append(acc_np[0])
except: except:
test_args['file'].reset() test_args['file'].reset()
print 'TEST: ', numpy.mean(loss), numpy.mean(acc) print 'Test loss:', numpy.mean(loss), 'acc:', numpy.mean(acc)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册