Skip to content

Commit 8b30e2a

Browse files
authored
Book chap6 (#5321)
* init * Fix bug * rename test_filw * refine test
1 parent 5c1ed01 commit 8b30e2a

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import paddle.v2 as paddle
2+
import paddle.v2.framework.layers as layers
3+
import paddle.v2.framework.nets as nets
4+
import paddle.v2.framework.core as core
5+
import paddle.v2.framework.optimizer as optimizer
6+
7+
from paddle.v2.framework.framework import Program, g_program, g_init_program
8+
from paddle.v2.framework.executor import Executor
9+
10+
import numpy as np
11+
12+
13+
def convolution_net(input_dim, class_dim=2, emb_dim=32, hid_dim=32):
14+
data = layers.data(name="words", shape=[1], data_type="int64")
15+
label = layers.data(name="label", shape=[1], data_type="int64")
16+
17+
emb = layers.embedding(input=data, size=[input_dim, emb_dim])
18+
conv_3 = nets.sequence_conv_pool(
19+
input=emb,
20+
num_filters=hid_dim,
21+
filter_size=3,
22+
act="tanh",
23+
pool_type="sqrt")
24+
conv_4 = nets.sequence_conv_pool(
25+
input=emb,
26+
num_filters=hid_dim,
27+
filter_size=4,
28+
act="tanh",
29+
pool_type="sqrt")
30+
prediction = layers.fc(input=[conv_3, conv_4],
31+
size=class_dim,
32+
act="softmax")
33+
cost = layers.cross_entropy(input=prediction, label=label)
34+
avg_cost = layers.mean(x=cost)
35+
adam_optimizer = optimizer.AdamOptimizer(learning_rate=0.002)
36+
opts = adam_optimizer.minimize(avg_cost)
37+
acc = layers.accuracy(input=prediction, label=label)
38+
return avg_cost, acc
39+
40+
41+
def to_lodtensor(data, place):
42+
seq_lens = [len(seq) for seq in data]
43+
cur_len = 0
44+
lod = [cur_len]
45+
for l in seq_lens:
46+
cur_len += l
47+
lod.append(cur_len)
48+
flattened_data = np.concatenate(data, axis=0).astype("int64")
49+
flattened_data = flattened_data.reshape([len(flattened_data), 1])
50+
res = core.LoDTensor()
51+
res.set(flattened_data, place)
52+
res.set_lod([lod])
53+
return res
54+
55+
56+
def main():
57+
BATCH_SIZE = 100
58+
PASS_NUM = 5
59+
60+
word_dict = paddle.dataset.imdb.word_dict()
61+
dict_dim = len(word_dict)
62+
class_dim = 2
63+
64+
cost, acc = convolution_net(input_dim=dict_dim, class_dim=class_dim)
65+
66+
train_data = paddle.batch(
67+
paddle.reader.shuffle(
68+
paddle.dataset.imdb.train(word_dict), buf_size=1000),
69+
batch_size=BATCH_SIZE)
70+
place = core.CPUPlace()
71+
exe = Executor(place)
72+
73+
exe.run(g_init_program)
74+
75+
for pass_id in xrange(PASS_NUM):
76+
for data in train_data():
77+
tensor_words = to_lodtensor(map(lambda x: x[0], data), place)
78+
79+
label = np.array(map(lambda x: x[1], data)).astype("int64")
80+
label = label.reshape([BATCH_SIZE, 1])
81+
82+
tensor_label = core.LoDTensor()
83+
tensor_label.set(label, place)
84+
85+
outs = exe.run(g_program,
86+
feed={"words": tensor_words,
87+
"label": tensor_label},
88+
fetch_list=[cost, acc])
89+
cost_val = np.array(outs[0])
90+
acc_val = np.array(outs[1])
91+
92+
print("cost=" + str(cost_val) + " acc=" + str(acc_val))
93+
if cost_val < 1.0 and acc_val > 0.7:
94+
exit(0)
95+
exit(1)
96+
97+
98+
if __name__ == '__main__':
99+
main()

0 commit comments

Comments
 (0)