-
Notifications
You must be signed in to change notification settings - Fork 291
/
ji-qi-xue-xi-ku3.md
280 lines (211 loc) · 6.1 KB
/
ji-qi-xue-xi-ku3.md
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# tensorflow
---
**Main classes**
* tf.Graph\(\)
* tf.Operation\(\)
* tf.Tensor\(\)
* tf.Session\(\)
**Some useful functions**
* tf.get\_default\_session\(\)
* tf.get\_default\_graph\(\)
* tf.reset\_default\_graph\(\)
* ops.reset\_default\_graph\(\)
* tf.device\(“/cpu:0”\)
* tf.name\_scope\(value\)
* tf.convert\_to\_tensor\(value\)
**TensorFlow Optimizers**
* GradientDescentOptimizer
* AdadeltaOptimizer
* AdagradOptimizer
* MomentumOptimizer
* AdamOptimizer
* FtrlOptimizer
* RMSPropOptimizer
**Reduction**
* reduce\_sum
* reduce\_prod
* reduce\_min
* reduce\_max
* reduce\_mean
* reduce\_all
* reduce\_any
* accumulate\_n
**Activation functions**
* tf.nn
* relu
* relu6
* elu
* softplus
* softsign
* dropout
* bias\_add
* sigmoid
* tanh
* sigmoid\_cross\_entropy\_with\_logits
* softmax
* log\_softmax
* softmax\_cross\_entropy\_with\_logits
* sparse\_softmax\_cross\_entropy\_with\_logits
* weighted\_cross\_entropy\_with\_logits
# keras
基本例子
```python
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
data = np.random.random((1000,100))
labels = np.random.randint(2,size=(1000,1))
model = Sequential()
model.add(Dense(32,activation='relu',input_dim=100))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',loss='binary_crossentroy', metrics=['accuracy'])
```
keras数据集
```python
from keras.datasets import boston_housing, mnist, cifar10,imdb
(x_train,y_train),(x_test,y_test) = mnist.load_data()
(x_train2,y_train2),(x_test2,y_test2) = boston_housing.load_data()
(x_train3,y_train3),(x_test3,y_test3) = cifar10.load_data()
(x_train4,y_train4),(x_test4,y_test4) = imdb.load_data(num_words=20000)
num_classes = 10
model.fit(data,labels,epochs=10,batch_size=32)
predictions = model.predict(data)
from urllib.request import urlopen
data = np.loadtxt(urlopen(
"http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data"),
delimiter=",")
X = data[:,0:8]
y = data [:,8]
```
模型-顺序模型
```python
from keras.models import Sequential
model = Sequential()
model2 = Sequential()
model3 = Sequential()
```
模型-多层感知机
```python
#Binary Classification
from keras.layers import Dense
model.add(Dense(12,input_dim=8,kernel_initializer='uniform',activation='relu'))
model.add(Dense(8,kernel_initializer='uniform',activation='relu'))
model.add(Dense(1,kernel_initializer='uniform',activation='sigmoid'))
#Multi-Class Classification
from keras.layers import Dropout
model.add(Dense(512,activation='relu',input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512,activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10,activation='softmax'))
#Regression
model.add(Dense(64,activation='relu',input_dim=train_data.shape[1]))
model.add(Dense(1))
```
模型-卷积神经网络
```python
from keras.layers import Activation,Conv2D,MaxPooling2D,Flatten
model2.add(Conv2D(32,(3,3),padding='same',input_shape=x_train.shape[1:]))
model2.add(Activation('relu'))
model2.add(Conv2D(32,(3,3)))
model2.add(Activation('relu'))
model2.add(MaxPooling2D(pool_size=(2,2)))
model2.add(Dropout(0.25))
model2.add(Conv2D(64,(3,3), padding='same'))
model2.add(Activation('relu'))
model2.add(Conv2D(64,(3, 3)))
model2.add(Activation('relu'))
model2.add(MaxPooling2D(pool_size=(2,2)))
model2.add(Dropout(0.25))
model2.add(Flatten())
model2.add(Dense(512))
model2.add(Activation('relu'))
model2.add(Dropout(0.5))
model2.add(Dense(num_classes))
model2.add(Activation('softmax'))
```
模型-循环神经网络
```python
from keras.klayers import Embedding,LSTM
model3.add(Embedding(20000,128))
model3.add(LSTM(128,dropout=0.2,recurrent_dropout=0.2))
model3.add(Dense(1,activation='sigmoid'))
```
模型调优
```python
from keras.optimizers import RMSprop
opt = RMSprop(lr=0.0001, decay=1e-6)
model2.compile(loss='categorical_crossentropy',optimizer=opt,metrics=['accuracy'])
```
early stopping
```python
from keras.callbacks import EarlyStopping
early_stopping_monitor = EarlyStopping(patience=
model3.fit(x_train4, y_train4, batch_size=32, epochs=15,
validation_data=(x_test4,y_test4),callbacks=[early_stopping_monitor])
```
编译模型
```python
#MLP: Binary Classification
model.compile(optimizer='adam', loss='binary_crossentropy',metrics=['accuracy'])
#MLP: Multi-Class Classification
model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])
#MLP: Regression
model.compile(optimizer='rmsprop',loss='mse',metrics=['mae'])
#Recurrent Neural Network
model3.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
```
保存和加载模型
```python
from keras.models import load_model
model3.save('model_file.h5')
my_model = load_model('my_model.h5')
```
查看模型详情
```python
model.output_shape #Model output shape
model.summary() #Model summary representation
model.get_config() #Model configuration
model.get_weights() #List all weight tensors in the model
```
预测
```python
model3.predict(x_test4, batch_size=32)
model3.predict_classes(x_test4,batch_size=32)
```
训练模型
```python
model3.fit(x_train4,y_train4,batch_size=32,epochs=15,verbose=1,
validation_data=(x_test4,y_test4))
```
评价模型性能
```python
score = model3.evaluate(x_test,y_test,batch_size=32)
```
数据预处理
```python
#Sequence Padding
from keras.preprocessing import sequence
x_train4 = sequence.pad_sequences(x_train4,maxlen=80)
x_test4 = sequence.pad_sequences(x_test4,maxlen=80)
#One-Hot Encoding
from keras.utils import to_categorical
Y_train = to_categorical(y_train, num_classes)
Y_test = to_categorical(y_test, num_classes)
Y_train3 = to_categorical(y_train3, num_classes)
Y_test3 = to_categorical(y_test3, num_classes)
#Train and Test Sets
from sklearn.model_selection import train_test_split
X_train5,X_test5,y_train5,y_test5 = train_test_split(X,y,test_size=0.33,
random_state=42)
#Standardization/Normalization
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler().fit(x_train2)
standardized_X = scaler.transform(x_train2)
standardized_X_test = scaler.transform(x_test2)
```