原文地址:http://siligence.ai/article-463-1.html
深度学习最常见的神经网络结构大概有DNN,CNN,RNN,我们可以简单的介绍一下tensorflow中关于上述网络中的DNN以及CNN的简单实现:
关于DNN:
全连接神经网络在我看来主要有两个作用,一个是提取高维特征,另外一个是进行维度变换。在tensorflow中实现较为简单,具体如下所示
with tf.name_scope(‘dnn’):
weight = tf.Variable(tf.truncated_normal([n_filter,numClasses]))
bias = tf.Variable(tf.constant(0.1,shape=[numClasses]))
prediction = (tf.matmul(data,weight) + bias)
weight和bias是tensorflow中变量有两种初始化的方法,tf.Variable()和tf.get_variable(),前者使用简单,后者中定义了很多前者没有方法,两者看自己喜欢进行选取。
需要注意的是里面矩阵数据:f(X*w+b),X为[M,N],w[N,O],b[M,O]。M在大部分情况是一个batch_size的大小。
关于卷积神经网络(CNN):
with tf.name_scope(‘conv1’):
file_shape = [filter_size,numDimensions,1,n_filter]
W_c = tf.Variable(tf.truncated_normal(file_shape,stddev=0.1),name=‘W_c’)
b_c = tf.Variable(tf.truncated_normal([FLAGS.n_filter],name=“b_c”))
conv = tf.nn.conv2d(data,W_c,strides=[1,1,1,1],padding=“VALID”,name=“conv”)
h = tf.nn.relu(tf.nn.bias_add(conv,b_c),name=“relu”)
卷积神经网络在我看来有两个作用:(1)提取周围的一片特征,(2)通过通道数的变化,增加或者减少特征的个数(大部分会增加)。
关于tf.nn.conv2d(data,W_c,strides,padding)四个参数中,data是数据,W_c是kernal的大小以及Kernal的数量,每一个通道一个Kernal,stride是移动的步长,padding是填充方式。
卷积神经网络之后往往特征数量增多,容易出现过拟合,所以需要进行池化操作以此减少特征的数量。
关于池化层:
with tf.name_scope(‘pool1’):
pooled = tf.nn.max_pool(h, ksize=[1, maxSeqLength - filter_size + 1, 1, 1], strides=[1, 1, 1, 1],
padding=“VALID”, name=‘max_pool’)
池化的方式主要有两种,tf.nn.max_pool()以及tf.nn.mean_pool()就是最大池化和平均池化,在卷积结果之后往往会出现很多个通道(每个通道可以理解为一个矩阵),由于CNN提取了很多的特征,容易在我们的模型上产生过拟合的现象,池化的主要目的在于减少特征数量,所以池化中四个参数分别为,数据,kernal,步长,以及padding。最大池化的意思是指,在kernal包裹的范围内,值最大的那个特征作为池化之后的特征,平均池化,则是指用kernal包裹范围内的所有值取平均数,作为这一步的输出。在给出的例子中,我们运用池化不改变识别通道的数目,只改变通道的大小。
以上为tensorflowDNN,和CNN常用接口的实现方式,这里特别说明两个函数tf.squeeze()和tf.expand()一个可以减少维度(比如从3维变成2维,一个可以扩充维度比如从2维变成3维)
好了,附上我写的不太成熟的代码,希望可以给大家一些帮助
import tensorflow as tf
import numpy as np
from random import randint
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_integer(‘batchSize’, 64, “”“flag of type integer”"")
flags.DEFINE_integer(‘maxSeqLength’, 250, “”“The length of sentence”"")
flags.DEFINE_integer(‘numClasses’, 2, “”“The number of Sentiment type”"")
flags.DEFINE_integer(‘numDimensions’, 50, “”“The embedding size”"")
flags.DEFINE_integer(‘iterations’,40000,""“The iteration”"")
flags.DEFINE_integer(‘filter_size’, 3,""“the number of flags_size”"")
flags.DEFINE_integer(‘n_filter’, 64, “”“The number of fiters”"")
flags.DEFINE_string(‘summary_path’, ‘cnn_summary’, “”“summary_path”"")
flags.DEFINE_string(‘ckpt_path’, ‘cnn_ckpt/cnn.ckpt’, “”“ckpt_path”"")
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.4
def getTrainBatch(ids):
labels = []
arr = np.zeros([FLAGS.batchSize,FLAGS.maxSeqLength])
for i in range(FLAGS.batchSize):
if(i % 2) == 0:
num = randint(1,11499)
labels.append([1,0])
else:
num = randint(13499,24999)
labels.append([0,1])
arr[i] = ids[num-1:num]
return arr,labels
if name == ‘main’:
# 40000个词
wordsList = np.load(“data/wordsList.npy”)
wordList = wordsList.tolist()
wordList = [word.decode(‘UTF-8’) for word in wordsList]
# dictionary, reverse_dictionary = bulid_vocabulary_dict(wordList)
# 400000个50维度的词
wordVectors = np.load("data/wordVectors.npy")
# 句子
ids = np.load("data/idsMatrix.npy")
input_data = tf.placeholder(tf.int32,shape=[FLAGS.batchSize,FLAGS.maxSeqLength],name='input_data')
labels = tf.placeholder(tf.int32,shape=[FLAGS.batchSize,FLAGS.numClasses],name='labels')
data = tf.Variable(tf.zeros([FLAGS.batchSize,FLAGS.maxSeqLength,FLAGS.numDimensions]))
# 根据索引来寻找向量
data = tf.nn.embedding_lookup(wordVectors,input_data)
data = tf.cast(data, tf.float32)
data = tf.expand_dims(data, -1)
with tf.name_scope('conv1'):
file_shape = [FLAGS.filter_size,FLAGS.numDimensions,1,FLAGS.n_filter]
W_c = tf.Variable(tf.truncated_normal(file_shape,stddev=0.1),name='W_c')
b_c = tf.Variable(tf.truncated_normal([FLAGS.n_filter],name="b_c"))
conv = tf.nn.conv2d(data,W_c,strides=[1,1,1,1],padding="VALID",name="conv")
h = tf.nn.relu(tf.nn.bias_add(conv,b_c),name="relu")
with tf.name_scope('pool1'):
pooled = tf.nn.max_pool(h, ksize=[1, FLAGS.maxSeqLength - FLAGS.filter_size + 1, 1, 1], strides=[1, 1, 1, 1],
padding="VALID", name='max_pool')
with tf.name_scope("dropout"):
value = tf.nn.dropout(pooled,keep_prob=0.3,name="dropout")
with tf.name_scope('dnn'):
weight = tf.Variable(tf.truncated_normal([FLAGS.n_filter,FLAGS.numClasses]))
bias = tf.Variable(tf.constant(0.1,shape=[FLAGS.numClasses]))
value = tf.transpose(value,[1,2,0,3])
# 消除维度为1的维度
last = tf.squeeze(value)
with tf.name_scope('prediction'):
prediction = (tf.matmul(last,weight) + bias)
tf.add_to_collection("prediction", prediction)
with tf.name_scope('correctPred'):
correctPred = tf.equal(tf.argmax(prediction,1),tf.argmax(labels,1))
tf.add_to_collection("correctPred", prediction)
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=labels))
tf.summary.scalar('loss', loss)
tf.add_to_collection("loss", loss)
with tf.name_scope('accuracy'):
accuracy = tf.reduce_mean(tf.cast(correctPred, tf.float32))
tf.summary.scalar('accuracy',accuracy)
tf.add_to_collection("accuracy", accuracy)
optimizer = tf.train.AdamOptimizer(0.01).minimize(loss)
with tf.Session(config=config) as sess:
saver = tf.train.Saver()
init_op = tf.global_variables_initializer()
sess.run(init_op)
merged_summaries = tf.summary.merge_all()
writer = tf.summary.FileWriter(FLAGS.summary_path, sess.graph)
for step in range(FLAGS.iterations):
nextBatch,nextBatchLabels = getTrainBatch(ids)
sess.run(optimizer,feed_dict={input_data:nextBatch,labels:nextBatchLabels})
summary = sess.run(merged_summaries, {input_data: nextBatch, labels: nextBatchLabels})
writer.add_summary(summary=summary, global_step=step)
if(step % 1000 == 0):
loss_,accuracy_, = sess.run([loss,accuracy,], {input_data: nextBatch, labels: nextBatchLabels})
print("iteration {}/{} ...".format(step+1,FLAGS.iterations),"loss {} ...".format(loss_),"accuracy {} ..." .format(accuracy_))
if((step+1) % 10000 == 0):
save_path = saver.save(sess,FLAGS.ckpt_path,global_step=step)
print("saved to %s" % save_path)
祝大家早日成为大佬。 ------ 作者: cyl250 ([http://siligence.ai/column/index.php?page=1](http://siligence.ai/article-463-1.html))