一、搭建簡單的CNN做序列標注代碼
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
TIME_STEPS = 15# backpropagation through time 的time_steps
BATCH_SIZE = 1#50
INPUT_SIZE = 1 # x數據輸入size
LR = 0.05 # learning rate
num_tags = 2
# 定義一個生成數據的 get_batch function:
def get_batch():
xs = np.array([[[[2], [3], [4], [5], [5], [5], [1], [5], [3], [2], [5], [5], [5], [3], [5]]]])
res = np.array([[0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1]])
ys = np.zeros([1,TIME_STEPS,2])
for i in range(TIME_STEPS):
if(res[0,i] == 0):
ys[0,i,0] = 1
ys[0,i,1] = 0
else:
ys[0,i,0] = 0
ys[0,i,1] = 1
return [xs, res,ys]
# 定義 CNN 的主體結構
class CNN(object):
def __init__(self, n_steps, input_size, num_tags, batch_size):
self.n_steps = n_steps
self.input_size = input_size
self.num_tags = num_tags
self.batch_size = batch_size
#卷積神將網絡的輸入:[batch, in_height, in_width, in_channels],在自然語言處理中height為1
self.xs = tf.placeholder(tf.float32, [self.batch_size,1, self.n_steps, self.input_size], name='xs')
#做序列標注,第二維對應好輸入的n_steps,相當於每個時刻的輸入都有一個輸出
self.ys = tf.placeholder(tf.int32, [self.batch_size, self.n_steps,self.num_tags], name='ys')#
self.featureNum = 10#提取10個特征
#[卷積核的高度,卷積核的寬度,圖像通道數,卷積核個數]
W_conv1 = self.weight_variable([1,3,1,self.featureNum])#提取10個特征
#對應10個卷積核輸出
b_conv1 = self.bias_varibale([self.featureNum])
#卷積操作
layer_conv1 = tf.nn.conv2d(self.xs, W_conv1,strides=[1, 1, 1, 1],padding="SAME",) + b_conv1
#激勵層
layer_conv1 = tf.nn.relu(layer_conv1)
#最大值池化 本處去除池化層為了后續計算簡便
#layer_pool1 = tf.nn.max_pool(layer_conv1,
# [1, 1, 3, 1],[1,1,1,1],padding='VALID')
layer_pool1 = layer_conv1
# 全連接層 映射到self.n_steps x self.num_tags
layer_pool1 = tf.reshape(layer_pool1,[self.n_steps,self.featureNum])
W_fc1 = self.weight_variable([self.featureNum,self.num_tags])
b_fc1 = self.bias_varibale([self.num_tags])
h_fc1 = tf.matmul(layer_pool1, W_fc1) + b_fc1
#激勵層
h_fc1 = tf.nn.relu(h_fc1)
#softmax 歸一化
self.y_conv = tf.nn.softmax(h_fc1)
self.label = tf.reshape(self.ys,[self.n_steps,2])
self.cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.label, logits=self.y_conv))
#梯度下降
self.train_op = tf.train.AdamOptimizer(LR).minimize(self.cost)
self.pred = tf.argmax(self.y_conv,axis = 1)
def weight_variable(self,shape):
initial=tf.truncated_normal(shape, mean=0.0, stddev=0.1)
return tf.Variable(initial)
def bias_varibale(self,shape):
initial=tf.constant(0,1,shape=shape)
return tf.Variable(initial)
# 訓練CNN
if __name__ == '__main__':
# 搭建 CNN 模型
model = CNN(TIME_STEPS, INPUT_SIZE, num_tags, BATCH_SIZE)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# matplotlib可視化
plt.ion() # 設置連續 plot
plt.show()
# 訓練多次
for i in range(150):
xs, res,ys = get_batch() # 提取 batch data
# 初始化 data
feed_dict = {
model.xs: xs,
model.ys: ys,
}
# 訓練
_, cost,pred = sess.run(
[model.train_op, model.cost, model.pred],
feed_dict=feed_dict)
# plotting
x = xs.reshape(-1,1)
r = res.reshape(-1, 1)
p = pred.reshape(-1, 1)
x = range(len(x))
plt.clf()
plt.plot(x, r, 'r', x, p, 'b--')
plt.ylim((-1.2, 1.2))
plt.draw()
plt.pause(0.3) # 每 0.3 s 刷新一次
# 打印 cost 結果
if i % 20 == 0:
print('cost: ', round(cost, 4))
得到結果:

二、CNN主要知識點
待整理。
