-
Notifications
You must be signed in to change notification settings - Fork 9
/
multiOutputLSTM.py
152 lines (126 loc) · 4.93 KB
/
multiOutputLSTM.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
# -*- coding:utf-8 -*-
import numpy as np
# 自定义损失和多输出的LSTM
from keras.preprocessing import sequence
from keras.models import Sequential,Model
from keras.layers import Dense, Dropout, Embedding, LSTM, Bidirectional,Input,Lambda
from keras.layers.merge import concatenate
from keras.datasets import imdb
import tensorflow as tf
from keras.losses import mean_squared_error
max_features = 20000
# cut texts after this number of words
# (among top max_features most common words)
maxlen = 100
batch_size = 32
def imdbload(path='imdb.npz', num_words=None, skip_top=0,
maxlen=None, seed=113,
start_char=1, oov_char=2, index_from=3, **kwargs):
f = np.load(path)
x_train = f['x_train']
labels_train = f['y_train']
x_test = f['x_test']
labels_test = f['y_test']
f.close()
np.random.seed(seed)
np.random.shuffle(x_train)
np.random.seed(seed)
np.random.shuffle(labels_train)
np.random.seed(seed * 2)
np.random.shuffle(x_test)
np.random.seed(seed * 2)
np.random.shuffle(labels_test)
xs = np.concatenate([x_train, x_test])
labels = np.concatenate([labels_train, labels_test])
if start_char is not None:
xs = [[start_char] + [w + index_from for w in x] for x in xs]
elif index_from:
xs = [[w + index_from for w in x] for x in xs]
if maxlen:
new_xs = []
new_labels = []
for x, y in zip(xs, labels):
if len(x) < maxlen:
new_xs.append(x)
new_labels.append(y)
xs = new_xs
labels = new_labels
if not xs:
raise ValueError('After filtering for sequences shorter than maxlen=' +
str(maxlen) + ', no sequence was kept. '
'Increase maxlen.')
if not num_words:
num_words = max([max(x) for x in xs])
# by convention, use 2 as OOV word
# reserve 'index_from' (=3 by default) characters:
# 0 (padding), 1 (start), 2 (OOV)
if oov_char is not None:
xs = [[oov_char if (w >= num_words or w < skip_top) else w for w in x] for x in xs]
else:
new_xs = []
for x in xs:
nx = []
for w in x:
if w >= num_words or w < skip_top:
nx.append(w)
new_xs.append(nx)
xs = new_xs
x_train = np.array(xs[:len(x_train)])
y_train = np.array(labels[:len(x_train)])
x_test = np.array(xs[len(x_train):])
y_test = np.array(labels[len(x_train):])
return (x_train, y_train), (x_test, y_test)
def loadData():
print('Loading data...')
(x_train, y_train), (x_test, y_test) = imdbload(maxlen=max_features)
print(len(x_train), 'train sequences')
print(len(x_test), 'test sequences')
print("Pad sequences (samples x time)")
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)
y_train = np.array(y_train)
y_test = np.array(y_test)
return x_train,y_train,x_test,y_test
def mean_squared_errors(y_true,y_pred):
import keras.backend as K
return K.mean(K.square(y_pred-y_true),axis=-1)
def lossfunction(args):
import keras.backend as K
enout,cnout =args
out =K.mean(K.square(enout-cnout),axis=-1)
return out
def E_C_lstm():
English_input = Input(shape=(maxlen,))
Engem =Embedding(max_features, 128, input_length=maxlen)(English_input)
Chinese_input =Input(shape=(maxlen,))
CHem =Embedding(max_features,64,input_length=maxlen)(Chinese_input)
ENlstm =LSTM(64,dropout=0.5)(Engem)
CHlstm =LSTM(64,dropout=0.5)(CHem)
ENout =Dense(2,activation='sigmoid',name='ENout')(ENlstm)
CHout =Dense(2,activation='sigmoid',name='CHout')(CHlstm)
loss_out =Lambda(lossfunction,output_shape=(2,),name='losss')([ENout,CHout])
out =concatenate(inputs=[ENout,CHout])
out =Dense(2,activation='softmax',name='Finout')(out)
model =Model(inputs=[English_input,Chinese_input], outputs=[out,ENout,CHout,loss_out])
model.summary()
return model
model =E_C_lstm()
model.compile(optimizer='sgd',loss={'Finout':'categorical_crossentropy','losss':lambda x ,y:y,'ENout':'categorical_crossentropy',
'CHout':'categorical_crossentropy'})
from keras.utils import plot_model
x_train,y_train,x_test,y_test =loadData()
from keras.utils.np_utils import to_categorical
y_train =to_categorical(y_train,2)
model.fit([x_train,x_train],[y_train,y_train,y_train,y_train],batch_size=32,epochs=10)
plot_model(model,'model.png')
# import json
# from keras.models import model_from_json
# with open('model.json', 'r') as f:
# jsonfile =json.load(f)
# print 'load json file...'
# model =model_from_json(json_string=jsonfile)
# # try using different optimizers and different optimizer configs
# model.compile(optimizer='adam',loss= 'binary_crossentropy', loss_weights=[1,0.5,0.5],metrics=['accuracy'])
#