-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathex9_3_advanced_keras.py
95 lines (69 loc) · 2.09 KB
/
ex9_3_advanced_keras.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
def Lambda_with_lambda():
from keras.layers import Lambda, Input
from keras.models import Model
x = Input((1,))
y = Lambda(lambda x: x + 1)(x)
m = Model(x, y)
yp = m.predict_on_batch([1, 2, 3])
print("np.array([1,2,3]) + 1:")
print(yp)
def Lambda_function():
from keras.layers import Lambda, Input
from keras.models import Model
def kproc(x):
return x ** 2 + 2 * x + 1
def kshape(input_shape):
return input_shape
x = Input((1,))
y = Lambda(kproc, kshape)(x)
m = Model(x, y)
yp = m.predict_on_batch([1, 2, 3])
print("np.array([1,2,3]) + 1:")
print(yp)
def Backend_for_Lambda():
from keras.layers import Lambda, Input
from keras.models import Model
from keras import backend as K
def kproc_concat(x):
m = K.mean(x, axis=1, keepdims=True)
d1 = K.abs(x - m)
d2 = K.square(x - m)
return K.concatenate([x, d1, d2], axis=1)
def kshape_concat(input_shape):
output_shape = list(input_shape)
output_shape[1] *= 3
return tuple(output_shape)
x = Input((3,))
y = Lambda(kproc_concat, kshape_concat)(x)
m = Model(x, y)
yp = m.predict_on_batch([[1, 2, 3], [3, 4, 8]])
print(yp)
def TF_for_Lamda():
from keras.layers import Lambda, Input
from keras.models import Model
import tensorflow as tf
def kproc_concat(x):
m = tf.reduce_mean(x, axis=1, keep_dims=True)
d1 = tf.abs(x - m)
d2 = tf.square(x - m)
return tf.concat([x, d1, d2], axis=1)
def kshape_concat(input_shape):
output_shape = list(input_shape)
output_shape[1] *= 3
return tuple(output_shape)
x = Input((3,))
y = Lambda(kproc_concat, kshape_concat)(x)
m = Model(x, y)
yp = m.predict_on_batch([[1, 2, 3], [3, 4, 8]])
print(yp)
def main():
print('Lambda with lambda')
Lambda_with_lambda()
print('Lambda function')
Lambda_function()
print('Backend for Lambda')
Backend_for_Lambda()
print('TF for Lambda')
TF_for_Lamda()
if __name__ == '__main__':
main()