-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
208 lines (166 loc) · 6.65 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# -*- coding: utf-8 -*-
import numpy as np
from tensorflow import keras
class DanQ(keras.Model):
def __init__(self):
super(DanQ, self).__init__('DanQ')
self.conv_1 = keras.layers.Conv1D(
filters=320,
kernel_size=26,
strides=1,
padding='valid',
activation='relu')
self.pool_1 = keras.layers.MaxPool1D(
pool_size=13,
strides=13,
padding='valid')
self.dropout_1 = keras.layers.Dropout(0.2)
forward_layer = keras.layers.LSTM(
units=320,
return_sequences=True,
return_state=True)
backward_layer = keras.layers.LSTM(
units=320,
return_sequences=True,
return_state=True,
go_backwards=True)
self.bidirectional_rnn = keras.layers.Bidirectional(
layer=forward_layer,
backward_layer=backward_layer)
self.dropout_2 = keras.layers.Dropout(0.5)
self.flatten = keras.layers.Flatten()
self.dense_1 = keras.layers.Dense(
units=925,
activation='relu')
self.dense_2 = keras.layers.Dense(
units=919,
activation='sigmoid')
def call(self, inputs, training=None, mask=None, **kwargs):
"""
Forward propagation of DeepSEA model.
:param inputs: shape = (batch_size, length, c)
:param training: training or not.
:param kwargs: None
:return: shape = (batch_size, 919)
"""
# Convolution Layer 1
# Input Tensor Shape: [batch_size, 1000, 4]
# Output Tensor Shape: [batch_size, 975, 320]
temp = self.conv_1(inputs)
# Pooling Layer 1
# Input Tensor Shape: [batch_size, 975, 320]
# Output Tensor Shape: [batch_size, 75, 320]
temp = self.pool_1(temp)
# Dropout Layer 1
temp = self.dropout_1(temp, training = training)
# Bidirectional RNN layer 1
# Input Tensor Shape: [batch_size, 75, 320]
# Output Tensor Shape: [batch_size, 75, 640]
temp = self.bidirectional_rnn(temp, training = training, mask=mask)
forward_state_output = temp[1]
backward_state_output = temp[2]
# Dropout Layer 2
temp = self.dropout_2(temp[0], training = training)
# Flatten Layer 1
# Input Tensor Shape: [batch_size, 75, 640]
# Output Tensor Shape: [batch_size, 48000]
temp = self.flatten(temp)
# Fully Connection Layer 1
# Input Tensor Shape: [batch_size, 48000]
# Output Tensor Shape: [batch_size, 925]
temp = self.dense_1(temp)
# Fully Connection Layer 2
# Input Tensor Shape: [batch_size, 925]
# Output Tensor Shape: [batch_size, 919]
output = self.dense_2(temp)
return output
class DanQ_JASPAR(keras.Model):
def __init__(self):
super(DanQ_JASPAR, self).__init__('DanQ_JASPAR')
self.conv_1 = keras.layers.Conv1D(
filters=1024,
kernel_size=30,
strides=1,
padding='valid',
activation='relu')
self.pool_1 = keras.layers.MaxPool1D(
pool_size=15,
strides=15,
padding='valid')
self.dropout_1 = keras.layers.Dropout(0.2)
forward_layer = keras.layers.LSTM(
units=512,
return_sequences=True,
return_state=True)
backward_layer = keras.layers.LSTM(
units=512,
return_sequences=True,
return_state=True,
go_backwards=True)
self.bidirectional_rnn = keras.layers.Bidirectional(
layer=forward_layer,
backward_layer=backward_layer)
self.dropout_2 = keras.layers.Dropout(0.5)
self.flatten = keras.layers.Flatten()
self.dense_1 = keras.layers.Dense(
units=925,
activation='relu')
self.dense_2 = keras.layers.Dense(
units=919,
activation='sigmoid')
def build(self, input_shape):
super(DanQ_JASPAR, self).build(input_shape)
self.set_weights_by_JASPAR()
def call(self, inputs, training=None, mask=None, **kwargs):
"""
Forward propagation of DanQ-JASPAR model.
:param inputs: shape = (batch_size, length, c)
:param training: training or not.
:param kwargs: None
:return: shape = (batch_size, 919)
"""
# Convolution Layer 1
# Input Tensor Shape: [batch_size, 1000, 4]
# Output Tensor Shape: [batch_size, 971, 1024]
temp = self.conv_1(inputs)
# Pooling Layer 1
# Input Tensor Shape: [batch_size, 971, 1024]
# Output Tensor Shape: [batch_size, 64, 1024]
temp = self.pool_1(temp)
# Dropout Layer 1
temp = self.dropout_1(temp, training = training)
# Bidirectional RNN layer 1
# Input Tensor Shape: [batch_size, 64, 1024]
# Output Tensor Shape: [batch_size, 64, 1024]
temp = self.bidirectional_rnn(temp, training=training, mask=mask)
forward_state_output = temp[1]
backward_state_output = temp[2]
# Dropout Layer 2
temp = self.dropout_2(temp[0], training = training)
# Flatten Layer 1
# Input Tensor Shape: [batch_size, 75, 640]
# Output Tensor Shape: [batch_size, 48000]
temp = self.flatten(temp)
# Fully Connection Layer 1
# Input Tensor Shape: [batch_size, 48000]
# Output Tensor Shape: [batch_size, 925]
temp = self.dense_1(temp)
# Fully Connection Layer 2
# Input Tensor Shape: [batch_size, 925]
# Output Tensor Shape: [batch_size, 919]
output = self.dense_2(temp)
return output
def set_weights_by_JASPAR(self):
JASPAR_motifs = np.load('./data/JASPAR_CORE_2016_vertebrates.npy', allow_pickle=True, encoding='bytes')
JASPAR_motifs = list(JASPAR_motifs) # shape = (519, )
reverse_motifs = [JASPAR_motifs[19][::-1, ::-1], JASPAR_motifs[97][::-1, ::-1], JASPAR_motifs[98][::-1, ::-1],
JASPAR_motifs[99][::-1, ::-1], JASPAR_motifs[100][::-1, ::-1], JASPAR_motifs[101][::-1, ::-1]]
JASPAR_motifs = JASPAR_motifs + reverse_motifs # shape = (525, )
conv_weights = self.conv_1.get_weights()
for i in range(len(JASPAR_motifs)):
motif = JASPAR_motifs[i][::-1, :]
length = len(motif)
start = np.random.randint(low=3, high=30-length+1-3)
conv_weights[0][start:start+length, :, i] = motif - 0.25
conv_weights[1][i] = np.random.uniform(low=-1.0, high=0.0)
self.conv_1.set_weights(conv_weights)