-
Notifications
You must be signed in to change notification settings - Fork 2
/
RelationNet.py
132 lines (124 loc) · 4.31 KB
/
RelationNet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import paddle
import paddle.fluid as fluid
from paddle.fluid import ParamAttr
import numpy as np
import math
class BaseNet:
def conv_bn_layer(self,
input,
num_filters,
filter_size,
stride=1,
groups=1,
padding=0,
act=None,
name=None,
data_format='NCHW'):
n = filter_size * filter_size * num_filters
conv = fluid.layers.conv2d(
input=input,
num_filters=num_filters,
filter_size=filter_size,
stride=stride,
padding=padding,
groups=groups,
act=None,
param_attr=ParamAttr(name=name + "_weights",
initializer=fluid.initializer.Normal(0, math.sqrt(2. / n))),
bias_attr=ParamAttr(name=name + "_bias",
initializer=fluid.initializer.Constant(0.0)),
name=name + '.conv2d.output.1',
data_format=data_format)
bn_name = "bn_" + name
return fluid.layers.batch_norm(
input=conv,
act=act,
momentum=1,
name=bn_name + '.output.1',
param_attr=ParamAttr(name=bn_name + '_scale',
initializer=fluid.initializer.Constant(1)),
bias_attr=ParamAttr(bn_name + '_offset',
initializer=fluid.initializer.Constant(0)),
moving_mean_name=bn_name + '_mean',
moving_variance_name=bn_name + '_variance',
data_layout=data_format)
class EmbeddingNet(BaseNet):
def net(self,input):
conv = self.conv_bn_layer(
input=input,
num_filters=64,
filter_size=3,
padding=0,
act='relu',
name='embed_conv1')
conv = fluid.layers.pool2d(
input=conv,
pool_size=2,
pool_stride=2,
pool_type='max')
conv = self.conv_bn_layer(
input=conv,
num_filters=64,
filter_size=3,
padding=0,
act='relu',
name='embed_conv2')
conv = fluid.layers.pool2d(
input=conv,
pool_size=2,
pool_stride=2,
pool_type='max')
conv = self.conv_bn_layer(
input=conv,
num_filters=64,
filter_size=3,
padding=1,
act='relu',
name='embed_conv3')
conv = self.conv_bn_layer(
input=conv,
num_filters=64,
filter_size=3,
padding=1,
act='relu',
name='embed_conv4')
return conv
class RelationNet(BaseNet):
def net(self, input, hidden_size):
conv = self.conv_bn_layer(
input=input,
num_filters=64,
filter_size=3,
padding=0,
act='relu',
name='rn_conv1')
conv = fluid.layers.pool2d(
input=conv,
pool_size=2,
pool_stride=2,
pool_type='max')
conv = self.conv_bn_layer(
input=conv,
num_filters=64,
filter_size=3,
padding=0,
act='relu',
name='rn_conv2')
conv = fluid.layers.pool2d(
input=conv,
pool_size=2,
pool_stride=2,
pool_type='max')
fc = fluid.layers.fc(conv,size=hidden_size,act='relu',
param_attr=ParamAttr(name='fc1_weights',
initializer=fluid.initializer.Normal(0,0.01)),
bias_attr=ParamAttr(name='fc1_bias',
initializer=fluid.initializer.Constant(1)),
)
fc = fluid.layers.fc(fc, size=1,act='sigmoid',
param_attr=ParamAttr(name='fc2_weights',
initializer=fluid.initializer.Normal(0,0.01)),
bias_attr=ParamAttr(name='fc2_bias',
initializer=fluid.initializer.Constant(1)),
)
return fc