-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
94 lines (77 loc) · 3.1 KB
/
model.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# =====================================
# @Time : 2020/6/10
# @Author : Yang Guan (Tsinghua Univ.)
# @FileName: model.py
# =====================================
import tensorflow as tf
from tensorflow import Variable
from tensorflow.keras import Model, Sequential
from tensorflow.keras.layers import Dense
import numpy as np
tf.config.experimental.set_visible_devices([], 'GPU')
class MLPNet(Model):
def __init__(self, input_dim, num_hidden_layers, num_hidden_units, hidden_activation, output_dim, **kwargs):
super(MLPNet, self).__init__(name=kwargs['name'])
self.first_ = Dense(num_hidden_units,
activation=hidden_activation,
kernel_initializer=tf.keras.initializers.Orthogonal(np.sqrt(2.)),
dtype=tf.float32)
self.hidden = Sequential([Dense(num_hidden_units,
activation=hidden_activation,
kernel_initializer=tf.keras.initializers.Orthogonal(np.sqrt(2.)),
dtype=tf.float32) for _ in range(num_hidden_layers-1)])
output_activation = kwargs['output_activation'] if kwargs.get('output_activation') else 'linear'
self.outputs = Dense(output_dim,
activation=output_activation,
kernel_initializer=tf.keras.initializers.Orthogonal(1.),
bias_initializer=tf.keras.initializers.Constant(0.),
dtype=tf.float32)
self.build(input_shape=(None, input_dim))
def call(self, x, **kwargs):
x = self.first_(x)
x = self.hidden(x)
x = self.outputs(x)
return x
def test_attrib():
a = Variable(0, name='d')
p = MLPNet(2, 2, 128, 1, name='ttt')
print(hasattr(p, 'get_weights'))
print(hasattr(p, 'trainable_weights'))
print(hasattr(a, 'get_weights'))
print(hasattr(a, 'trainable_weights'))
print(type(a))
print(type(p))
# print(a.name)
# print(p.name)
# p.build((None, 2))
p.summary()
# inp = np.random.random([10, 2])
# out = p.forward(inp)
# print(p.get_weights())
# print(p.trainable_weights)
def test_clone():
p = MLPNet(2, 2, 128, 1, name='ttt')
print(p._is_graph_network)
s = tf.keras.models.clone_model(p)
print(s)
def test_out():
import numpy as np
Qs = tuple(MLPNet(8, 2, 128, 1, name='Q' + str(i)) for i in range(2))
inp = np.random.random((128, 8))
out = [Q(inp) for Q in Qs]
print(out)
def test_memory():
import time
Q = MLPNet(8, 2, 128, 1)
time.sleep(111111)
def test_memory2():
import time
model = tf.keras.Sequential([tf.keras.layers.Dense(10, input_shape=(30,), activation='relu'),
tf.keras.layers.Dense(20, activation='relu'),
tf.keras.layers.Dense(20, activation='relu'),
tf.keras.layers.Dense(10, activation='relu')])
time.sleep(10000)
if __name__ == '__main__':
test_memory2()