文章目录
Mnist模型定义keras层
优化算法及损失函数文字识别模型检验
CNNFilterpoolingexampleimportload data设定神经网络设定优化方法及损失函数训练
RNNLSTM影评分析importload预处理model损失函数&优化方法模型训练打印输出
Mnist
模型定义
tf
.keras
.Sequential
([
tf
.keras
.layers
.Flatten
(input_shape
=(28,28)),
tf
.keras
.layers
.Dense
(hidden_layer_size
,activation
='rule'),
tf
.keras
.layers
.Dropout
(0,1),
tf
.keras
.layers
.Dense
(output_size
,activation
='softmax')
])
keras层
Dense 全连接层DropoutEmbedding word2vecflatten
优化算法及损失函数
model
.compile(optimizer
='adam',loss
='sparse_categorical_crossentropy',
metrics
=['accuracy'])
可以使用下面命令看下模型的情况
model
.summary
()
文字识别
NUM_EPOCHS
=10
x_train
,x_valid
,x_test
=x_train
/255.0,x_valid
/255.0,x_test
/255.0
history
=model
.fit
(x_train
,y_train
,epochs
=NUM_EPOCHS
,
validation_data
=(x_valid
,y_valid
),verbose
=2)
模型检验
test_loss
,test_accuracy
=model
.evaluate
(x_test
,y_test
)
CNN
Filter
每个卷积核会提取图片某种特征,相当于滤镜 下面的卷积会锐化图片
0-10-15-10-10
下面的卷积会检查出边缘
0101-41010
pooling
池化操作主要是为了降维,常见操作是提取最大值
1124567832101234
按2*2卷积后
6834
example
import
import numpy
as np
import tersorflow
as tf
import pandas
as pd
from tensorflow
import keras
import matplotlib
as mpl
import matplotlib
.pyplot
as plt
%matplotlib inline
load data
npz
=np
.load
(...)
x_train
=npz
['inputs'].astype
(np
.float)
y_train
=npz
['targets'].astype
(np
.int)
npz
=np
.load
(...)
x_valid
,y_valid
=npz
['inputs'].astype
(np
.float).npz
('targets').astype
(np
.int)
x_train
[0].shape
from sklearn
.preprocessing
import StandardScaler
scaler
=StandardScaler
()
x_train_scaled
=scaler
.fit_transform
(x_tratin
.astype
(np
.float32
).reshape
(-1,1))
.reshape
(-1,28,28,1)
设定神经网络
model
=keras
.models
.Sequential
()
model
.add
(keras
.layer
.Conv2D
(filters
=32,padding
='same',kernel_size
=3,
activation
='selu',input_shape
=(28,28,1)))
model
.add
(keras
.layers
.MaxPool2D
(poo_size
=)
model
.add
(keras
.layers
.Flatten
())
model
.add
(keras
.layer
.Dense
(128,activat
='selu'))
model
.add
(keras
.layer
.Dense
(10,activation
='softmax'))
设定优化方法及损失函数
model
.compile(optimizer
='adam',loss
='sparse_categorical_crossentropy',metrics
=['accuracy'])
model
.summary
()
Model:"sequential"
---------------------------------------------------------------------------
Layer(type) Output Shape Param #
===========================================================================
conv2d(Conv2D) (None,28,28,32) 320
max_pooling2d(MaxPooling2D) (None,14,14,32) 0
flatten(Flatten) (None,6272) 0
dense(Dense) (None,128) 802944
dense_1(Dense) (None,10) 1290
===========================================================================
Total params:804,554
Trainable params:804,554
Non-trainable params:0
---------------------------------------------------------------------------
320=32*((3*3)+1) 6272=14*14*32
训练
import os
logdir
='cnn-selu-callbacks'
if not os
.path
.exists
(logdir
):
os
.mkdir
(logdir
)
output_model_file
=os
.path
.join
(logdir
,'model.h5')
callbacks
=[
keras
.callbacks
.TensorBoard
(logdir
),
keras
.callbacks
.ModelCheckpoint
(output_model_file
,save_best_only
=True),
keras
.callbacks
.EarlyStopping
(patience
=5,min_delta
=le
-3),
]
history
=model
.fit
(x_train_scaled
,y_train
,epochs
=10,
validation_data
=(x_valid_scaled
,y_valid
),callbacks
=callbacks
)
RNN
h
t
=
f
W
(
h
t
−
1
,
x
t
)
h_t=f_W(h_{t-1},x_t)
ht=fW(ht−1,xt)
LSTM
影评分析
import
import matplotlib
as mpl
import matplotlib
.pyplot
as plt
%matplotlib inline
import numpy
as np
import sklearn
import pandas
as pd
import os
import sys
import time
import tensorflow tf
from tensorflow
import keras
load
npz
=np
.load
(r
'data\indb_data_train.npz')
x_train
=npz
['inputs']
y_train
=npz
['targets']
预处理
imdb
=keras
.datasets
.imdb
word_index
=imdb
.get_word_index
()
word_index
={k
:(v
+3) for k
,v
in word_index
.items
()}
word_index
['<PAD>']=0
word_index
['<START>']=1
word_index
['<UNK>']=2
word_index
['<END>']=3
reverse_word_index
=dict([(value
,key
) for key
,value
in word_index
.items
()])
进入神经网络的每段文字应当具有相同长度,过长的文字需要截断,过短的问题需要填充空白字符
max_length
=500
x_train
=keras
.preprocession
.sequence
.pad_sequences
(x_train
,
value
=word_index
['<PAD>'],padding
='post',maxlen
=max_length
)
model
embedding层生成词向量Word2Vec,embedding_dim为每个单词的词向量维度
embedding_dim
=16
batch_size
=512
vocab_size
=10000
rnn_model
=keras
.models
.Sequential
([
keras
.layers
.Embedding
(input_dim
=vocab_size
,output_dim
=embedding_dim
,input_length
=max_length
),
keras
.layers
.Bidirectional
(keras
.layers
.SimpleRNN
(units
=32,return_sequences
=True)),
keras
.layers
.Bidirectional
(keras
.layers
.SimpleRNN
(units
=32,return_sequences
=False)),
keras
.layers
.Dense
(64,activation
='relu'),
keras
.layers
.Dense
(1,activation
='sigmoid'),
])
其中return_sequences用于标识下一层是否还是RNN
损失函数&优化方法
rnn_model
.compile(optimizer
='adam',loss
='binary_crossentropy',metrics
=['accuracy'])
模型训练
history
=rnn_model
.fit
(x_train
,y_train
,epochs
=10,
batch_size
=batch_size
,validation_split
=0.2)
打印输出
def plot_learning_curves(history
,label
,epochs
,min_value
,max_value
):
data
={}
data
[label
]=history
.history
[label
]
data
['val_'+label
]=history
.history
['val_'+label
]
pd
.DataFrame
(data
).plot
(figsize
=(8,5))
plt
.grid
(True)
plt
.axis
([0,epochs
,min_value
,max_value
])
plt
.show
()
plot_learning_curves
(history
,'accuracy',10,0,1)
plot_learning_curves
(history
,'loss',10,0,1)