|
| 1 | +import sys |
| 2 | +import math |
| 3 | +import gzip |
| 4 | + |
| 5 | +from paddle.v2.layer import parse_network |
| 6 | +import paddle.v2 as paddle |
| 7 | + |
| 8 | +__all__ = ["fc_net", "convolution_net"] |
| 9 | + |
| 10 | + |
| 11 | +def fc_net(dict_dim, |
| 12 | + class_num, |
| 13 | + emb_dim=28, |
| 14 | + hidden_layer_sizes=[28, 8], |
| 15 | + is_infer=False): |
| 16 | + """ |
| 17 | + define the topology of the dnn network |
| 18 | +
|
| 19 | + :param dict_dim: size of word dictionary |
| 20 | + :type input_dim: int |
| 21 | + :params class_num: number of instance class |
| 22 | + :type class_num: int |
| 23 | + :params emb_dim: embedding vector dimension |
| 24 | + :type emb_dim: int |
| 25 | + """ |
| 26 | + |
| 27 | + # define the input layers |
| 28 | + data = paddle.layer.data("word", |
| 29 | + paddle.data_type.integer_value_sequence(dict_dim)) |
| 30 | + if not is_infer: |
| 31 | + lbl = paddle.layer.data("label", |
| 32 | + paddle.data_type.integer_value(class_num)) |
| 33 | + |
| 34 | + # define the embedding layer |
| 35 | + emb = paddle.layer.embedding(input=data, size=emb_dim) |
| 36 | + # max pooling to reduce the input sequence into a vector (non-sequence) |
| 37 | + seq_pool = paddle.layer.pooling( |
| 38 | + input=emb, pooling_type=paddle.pooling.Max()) |
| 39 | + |
| 40 | + for idx, hidden_size in enumerate(hidden_layer_sizes): |
| 41 | + hidden_init_std = 1.0 / math.sqrt(hidden_size) |
| 42 | + hidden = paddle.layer.fc( |
| 43 | + input=hidden if idx else seq_pool, |
| 44 | + size=hidden_size, |
| 45 | + act=paddle.activation.Tanh(), |
| 46 | + param_attr=paddle.attr.Param(initial_std=hidden_init_std)) |
| 47 | + |
| 48 | + prob = paddle.layer.fc( |
| 49 | + input=hidden, |
| 50 | + size=class_num, |
| 51 | + act=paddle.activation.Softmax(), |
| 52 | + param_attr=paddle.attr.Param(initial_std=1.0 / math.sqrt(class_num))) |
| 53 | + |
| 54 | + if is_infer: |
| 55 | + return prob |
| 56 | + else: |
| 57 | + return paddle.layer.classification_cost( |
| 58 | + input=prob, label=lbl), prob, lbl |
| 59 | + |
| 60 | + |
| 61 | +def convolution_net(dict_dim, class_dim=2, emb_dim=28, hid_dim=128): |
| 62 | + """ |
| 63 | + cnn network definition |
| 64 | +
|
| 65 | + :param dict_dim: size of word dictionary |
| 66 | + :type input_dim: int |
| 67 | + :params class_dim: number of instance class |
| 68 | + :type class_dim: int |
| 69 | + :params emb_dim: embedding vector dimension |
| 70 | + :type emb_dim: int |
| 71 | + :params hid_dim: number of same size convolution kernels |
| 72 | + :type hid_dim: int |
| 73 | + """ |
| 74 | + |
| 75 | + # input layers |
| 76 | + data = paddle.layer.data("word", |
| 77 | + paddle.data_type.integer_value_sequence(dict_dim)) |
| 78 | + lbl = paddle.layer.data("label", paddle.data_type.integer_value(2)) |
| 79 | + |
| 80 | + #embedding layer |
| 81 | + emb = paddle.layer.embedding(input=data, size=emb_dim) |
| 82 | + |
| 83 | + # convolution layers with max pooling |
| 84 | + conv_3 = paddle.networks.sequence_conv_pool( |
| 85 | + input=emb, context_len=3, hidden_size=hid_dim) |
| 86 | + conv_4 = paddle.networks.sequence_conv_pool( |
| 87 | + input=emb, context_len=4, hidden_size=hid_dim) |
| 88 | + |
| 89 | + # fc and output layer |
| 90 | + output = paddle.layer.fc( |
| 91 | + input=[conv_3, conv_4], size=class_dim, act=paddle.activation.Softmax()) |
| 92 | + |
| 93 | + cost = paddle.layer.classification_cost(input=output, label=lbl) |
| 94 | + |
| 95 | + return cost, output, lbl |
0 commit comments