-
Notifications
You must be signed in to change notification settings - Fork 4
/
create_model.py
146 lines (132 loc) · 5.24 KB
/
create_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
#!/usr/bin/env python
# coding: utf-8
# In[2]:
#要求的库与参数
from tensorflow.keras.utils import Sequence, plot_model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, CSVLogger, TensorBoard
import matplotlib.pyplot as plt
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.layers import(
Input, Dense, Embedding, Conv1D, Flatten, Concatenate, TimeDistributed,
MaxPooling1D, Dropout, RepeatVector, Layer, Reshape, SimpleRNN, LSTM, BatchNormalization
)
import tensorflow as tf
import numpy as np
import pandas as pd
import math
AAINDEX = {'A': 1, 'R': 2, 'N': 3, 'D': 4, 'C': 5, 'Q': 6, 'E': 7, 'G': 8, 'H': 9, 'I': 10, 'L': 11,
'K': 12, 'M': 13, 'F': 14, 'P': 15, 'S': 16, 'T': 17, 'W': 18, 'Y': 19, 'V': 20}
MAXLEN = 2000
def pre_model(model_file, term_file='data/terms.pkl'):
MAXLEN = 2000
with open(term_file, 'rb') as file:
terms_df = pd.read_pickle(file)
terms = terms_df['terms'].values.flatten()
batch_size = 32
params = {
'max_kernel': 65,
'initializer': 'glorot_normal',
'dense_depth': 0,
'nb_filters': 256,
'optimizer': Adam(lr=2e-4),
'loss': 'binary_crossentropy'
}
nb_classes = len(terms)
inp_hot = Input(shape=(MAXLEN, 21), dtype=np.float32)
kernels = range(8, params['max_kernel'], 8)
nets = []
for i in range(len(kernels)):
conv = Conv1D(
filters=params['nb_filters'],
kernel_size=kernels[i],
padding='same',
name='conv_' + str(i),
kernel_initializer=params['initializer'])(inp_hot)
batch_norm = BatchNormalization()(conv)
relu_ = tf.keras.activations.relu(batch_norm)
nets.append(relu_)
new_nets = []
for i in range(len(nets)):
new_conv = Conv1D(filters=params['nb_filters'],
kernel_size=kernels[i],
padding='valid',
name='new_conv_' + str(i),
kernel_initializer=params['initializer'])(nets[i])
new_pool = MaxPooling1D(MAXLEN - kernels[i] + 1, name=str(i) + '_pool')(new_conv)
flat = Flatten(name='flat_' + str(i))(new_pool)
new_nets.append(flat)
net = Concatenate(axis=1)(new_nets)
# net = tf.keras.layers.Add()([pool_i, net])
# net = Flatten()(net)
net = Dense(nb_classes, activation='sigmoid')(net)
model = Model(inputs=inp_hot, outputs=net)
model.compile(optimizer=params['optimizer'], loss=params['loss'])
model.save(model_file)
model.summary()
# print(model)
def crnn_model(model_file, load=False, origin_path='origin_model.h5',
term_file='data/terms.pkl'):
MAXLEN = 2000
with open(term_file, 'rb') as file:
terms_df = pd.read_pickle(file)
terms = terms_df['terms'].values.flatten()
batch_size = 32
params = {
'max_kernel': 65,
'initializer': 'glorot_normal',
'dense_depth': 0,
'nb_filters': 256,
'optimizer': Adam(lr=0.0005),
'loss': 'binary_crossentropy'
}
nb_classes = len(terms)
inp_hot = Input(shape=(MAXLEN, 21), dtype=np.float32)
kernels = range(8, params['max_kernel'], 8)
nets = []
for i in range(len(kernels)):
conv = Conv1D(
filters=params['nb_filters'],
kernel_size=kernels[i],
padding='same',
name='conv_' + str(i),trainable=False,
kernel_initializer=params['initializer'])(inp_hot)
batch_norm = BatchNormalization()(conv)
relu_ = tf.keras.activations.relu(batch_norm)
nets.append(relu_)
new_nets = []
for i in range(len(nets)):
new_conv = Conv1D(filters=params['nb_filters'],
kernel_size=kernels[i],
padding='valid',
name='new_conv_' + str(i),trainable=False,
kernel_initializer=params['initializer'])(nets[i])
new_pool = MaxPooling1D(MAXLEN - kernels[i] + 1, name=str(i) + '_pool')(new_conv)
flat = Flatten(name='flat_' + str(i))(new_pool)
new_nets.append(flat)
net = Concatenate(axis=1)(new_nets)
net = BatchNormalization()(net)
net = Dropout(0.5)(net)
cnn_out = Dense(512, activation='relu')(net)
net = Dropout(0.5)(cnn_out)
net = RepeatVector(11)(net)
net = GRU(256, activation='tanh', return_sequences=True)(net)
net = GRU(256, activation='tanh', return_sequences=True)(net)
net = GRU(256, activation='tanh', return_sequences=True)(net)
net = Flatten()(net)
net = Dense(nb_classes, activation='sigmoid')(net)
model = Model(inputs=inp_hot, outputs=net)
model.compile(optimizer=params['optimizer'], loss=params['loss'])
if load:
loaded_model = load_model(origin_path)
old_weights = loaded_model.get_weights()
now_weights = model.get_weights()
cnt = 0
for i in range(len(now_weights)):
if old_weights[cnt].shape == now_weights[i].shape:
now_weights[i] = old_weights[cnt]
cnt = cnt + 1
print(f'{cnt} layers weights copied, total {len(now_weights)}')
model.set_weights(now_weights)
model.save(model_file)
model.summary()