/
time_relu.py
136 lines (106 loc) · 3.65 KB
/
time_relu.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
133
134
135
136
'''
This is the benchmark of 4 different implementations
of rectified linear activation in Theano.
Two types of computations are tested w.r.t.
each implementation: fprop and grad.
Results: in seconds, float32 (details in the code)
Implementation, CPU (fprop, bprop), GPU (fprop, bprop), (final score)
a) ScalarRectifier: (2.32, 2.40) (1.36, 2.67) (8.75)
b) T.max(.0, x): (5.19, 3.65) (1.38, 2.38) (12.60)
c) x*(x>0.): (2.85, 2.84) (1.31, 2.91) (9.91)
d) T.switch(x<0., 0., x): (2.32, 1.41) (1.41, 2.84) (8.39)
Conlusion:
In terms of efficiency, d) > a) > c) > b)
'''
from __future__ import print_function
__authors__ = "Li Yao and Frederic Bastien"
import theano
import theano.tensor as T
import numpy
import time
floatX = 'float32'
def relu(x):
"""
relu implementation with T.maximum
Parameters
----------
x: tensor variable
"""
return T.maximum(0.0, x)
def relu_(x):
"""
Alternative relu implementation
Parameters
----------
x: tensor variable
"""
return x * (x > 0)
def relu__(x):
"""
Alternative relu implementation. The most efficient one.
Parameters
----------
x: tensor variable
"""
return T.switch(x < 0., 0., x)
def test_scalar_rectifier():
"""
verify different implementations of relu
"""
x = T.fmatrix('inputs')
y1 = relu(x)
y3 = relu_(x)
y4 = relu__(x)
f1 = theano.function(inputs=[x], outputs=y1, name='benchmark_1_forward')
f3 = theano.function(inputs=[x], outputs=y3, name='benchmark_3_forward')
f4 = theano.function(inputs=[x], outputs=y4, name='benchmark_4_forward')
g1 = theano.function(inputs=[x], outputs=T.grad(y1.sum(),x),
name='benchmark_1_grad')
g3 = theano.function(inputs=[x], outputs=T.grad(y3.sum(),x),
name='benchmark_3_grad')
g4 = theano.function(inputs=[x], outputs=T.grad(y4.sum(),x),
name='benchmark_4_grad')
for i in range(10):
value = numpy.random.uniform(-1,1,size=(100,500)).astype(floatX)
numpy.testing.assert_array_equal(f1(value), f3(value),
err_msg='arrays not equal' )
numpy.testing.assert_array_equal(f1(value), f4(value),
err_msg='arrays not equal' )
numpy.testing.assert_array_equal(g1(value), g3(value),
err_msg='grad:arrays not equal' )
numpy.testing.assert_array_equal(g1(value), g4(value),
err_msg='grad:arrays not equal' )
def benchmark_relu():
"""
Benchmark the speed of different relu implementations.
Both fprop and grad are tested.
"""
x = T.ftensor4('inputs')
ops = [
relu_(x).sum(), # old
relu(x).sum(), # alter, short for alternative
relu__(x).sum(), # alter 2
T.grad(relu_(x).sum(),x), # grad_old
T.grad(relu(x).sum(),x), # grad_alter
T.grad(relu__(x).sum(),x), # grad_alter2
]
names = ['fprop_old', 'fprop_alter', 'fprop_alter2',
'grad_old', 'grad_alter', 'grad_alter2']
value = numpy.random.uniform(size=(512,32,32,100)).astype(floatX)
times = []
for op, name in zip(ops, names):
f = theano.function(inputs=[x], outputs=op, name=name)
n_loops = 10
t0 = time.time()
for i in range(n_loops):
f(value)
t1 = time.time()
benchmark = t1-t0
times.append(benchmark)
print(name)
theano.printing.debugprint(f, print_type=True)
print(names)
print(times)
if __name__ == '__main__':
benchmark_relu()
#test_scalar_rectifier()