-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
128 lines (93 loc) · 3.86 KB
/
models.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
from keras.layers import Conv2D, Dense, Flatten, Activation, LSTM, GRU
from keras.models import Sequential
from keras.layers.wrappers import TimeDistributed
import keras.backend as K
def change_input_dim(input_dim):
if K.image_dim_ordering() == 'tf':
input_dim = list(input_dim[1:]) + [input_dim[0]]
return list(input_dim)
def get_rnn(rnn_type):
if rnn_type == 'LSTM':
return LSTM
elif rnn_type == 'GRU':
return GRU
else:
raise ValueError('wrong rnn type')
def build_fc(input_dim=100, out_size=10):
model = Sequential([
Dense(100, input_dim=input_dim),
Activation('relu'),
Dense(out_size),
Activation('softmax'),
])
return model
def build_cnn_dqn(input_dim=(4, 84, 84), out_size=10):
assert len(input_dim) == 3
input_dim = change_input_dim(input_dim)
model = Sequential([
Conv2D(32, 8, 8, subsample=(4, 4), activation='relu', input_shape=input_dim),
Conv2D(64, 4, 4, subsample=(2, 2), activation='relu'),
Conv2D(64, 3, 3, subsample=(1, 1), activation='relu'),
Flatten(),
Dense(512, activation='relu'),
Dense(out_size),
Activation('softmax')
])
return model
def build_cnn_openai(input_dim=(4, 42, 42), out_size=10):
assert len(input_dim) == 3
input_dim = change_input_dim(input_dim)
model = Sequential([
Conv2D(32, 3, 3, subsample=(2, 2), activation='elu', border_mode='same', input_shape=input_dim),
Conv2D(32, 3, 3, subsample=(2, 2), activation='elu'),
Conv2D(32, 3, 3, subsample=(2, 2), activation='elu'),
Conv2D(32, 3, 3, subsample=(2, 2), activation='elu'),
Flatten(),
Dense(256, activation='relu'),
Dense(out_size),
Activation('softmax')
])
return model
def build_rnn_fc(input_dim=100, seq_len=None, out_size=10, consume_less='gpu', rnn_type='LSTM',):
rnn = get_rnn(rnn_type)
input_shape = [seq_len, input_dim]
unroll = seq_len > 0
model = Sequential([
rnn(256, return_sequences=True, unroll=unroll, input_shape=input_shape, consume_less=consume_less),
TimeDistributed(Dense(out_size)),
TimeDistributed(Activation('softmax')),
])
return model
def build_rnn_dqn(input_dim=(1, 84, 84), seq_len=None, out_size=10, consume_less='gpu', rnn_type='LSTM',):
assert len(input_dim) == 3
rnn = get_rnn(rnn_type)
input_dim = change_input_dim(input_dim)
input_dim = [seq_len] + input_dim
unroll = seq_len > 0
model = Sequential([
TimeDistributed(Conv2D(32, 8, 8, subsample=(4, 4), activation='relu'), input_shape=input_dim),
TimeDistributed(Conv2D(64, 4, 4, subsample=(2, 2), activation='relu')),
TimeDistributed(Conv2D(64, 3, 3, subsample=(1, 1), activation='relu')),
TimeDistributed(Flatten()),
rnn(256, unroll=unroll, return_sequences=True, consume_less=consume_less),
TimeDistributed(Dense(out_size)),
TimeDistributed(Activation('softmax')),
])
return model
def build_rnn_openai(input_dim=(1, 42, 42), seq_len=None, out_size=10, consume_less='gpu', rnn_type='LSTM',):
assert len(input_dim) == 3
rnn = get_rnn(rnn_type)
input_dim = change_input_dim(input_dim)
input_dim = [seq_len] + input_dim
unroll = seq_len > 0
model = Sequential([
TimeDistributed(Conv2D(32, 3, 3, subsample=(2, 2), activation='elu'), input_shape=input_dim),
TimeDistributed(Conv2D(32, 3, 3, subsample=(2, 2), activation='elu')),
TimeDistributed(Conv2D(32, 3, 3, subsample=(2, 2), activation='elu')),
TimeDistributed(Conv2D(32, 3, 3, subsample=(2, 2), activation='elu')),
TimeDistributed(Flatten()),
rnn(256, unroll=unroll, return_sequences=True, consume_less=consume_less),
TimeDistributed(Dense(out_size)),
TimeDistributed(Activation('softmax')),
])
return model