RNN学习笔记2-Tensorflow代码阅读

Tensorflow中文文档中有非常详细的RNN实现教程,链接:

tensorflow中文文档:http://www.tensorfly.cn/tfdoc/tutorials/overview.html

但是其中对于模型本身的原理没有涉及,故理解RNN、LSTM需要另找资料:

一篇LSTM英文教程:http://colah.github.io/posts/2015-08-Understanding-LSTMs/

从文章中看出,对LSTM的理解其实就是对这张图的理解。

尝试阅读如下代码:

RNN实现文本感情判断:https://github.com/roopalgarg/lstm-tensorflow

RNN学习笔记1-RNN学习方法探究

很久没有更新了,进入了喜大普奔的暑假,也有更多时间开自己喜欢的坑了。

首先我们需要对RNN有一个初步的感觉。但是说实话百度百科搜出来的东西还真是不太靠谱。都是一堆大话连篇,没有一个附了代码……

经过一晚上的摸索,大概知道这种东西该怎么学了:

  1. Github上面搜索RNN会出现按照Star排序的RNN相关仓库,对于查找工程代码非常有用!
  2. Tensorflow中文文档中有一章专门讲RNN的,适合学习算法细节使用。

 

Tensorflow学习笔记2-MNIST数字识别

很早之前写的,鉴于之前Tensorflow学习笔记有1,不能这么轻易弃坑,所以又加了这一部分。

import tensorflow as tf
import read_data
import time
import numpy

mnist = read_data.DataSets(dlimit = 300)
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32,shape=[None,784])
yy = tf.placeholder(tf.float32,shape=[None,10])
w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
sess.run(tf.global_variables_initializer())


y = tf.nn.softmax(tf.matmul(x,w)+b)
cross_entropy = -tf.reduce_sum(yy*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.0001).minimize(cross_entropy)
for i in range(0,600):
    print("Iteration %d"%i)
    batch_xs,batch_ys = mnist.train.next_batch(50)
    train_step.run(feed_dict={x:batch_xs,yy:batch_ys})

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(yy,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(accuracy.eval(feed_dict={x: mnist.test.images, yy:mnist.test.labels}))

这是最简单的线性回归算法,理解了这种算法,其实就可以搞懂Tensorflow的运行机制了,大概就是把梯度下降的图提前建出来,然后在对于训练数据一次次在图上迭代。

接下来是我自己写的CNN代码,自以为是的用了多个不同的卷积核,具体有没有用?Who Knows?毕竟玄学

import tensorflow as tf
import read_data
import numpy
import random

def col_calc(x):
    x_level1 = tf.reshape(x,[-1,28,28,1])
    w_level1 = tf.Variable(tf.truncated_normal([3,16,1,64],stddev=.1))
    b_level1 = tf.Variable(tf.constant(.1,shape=[64]))
    r_level1 = tf.nn.relu(tf.nn.conv2d(x_level1,w_level1,strides=[1,1,4,1],padding='VALID') + b_level1)

    x_level2 = tf.reshape(r_level1,[-1,26*4*64])
    w_level2 = tf.Variable(tf.truncated_normal([26*4*64,2048],stddev=.1))
    b_level2 = tf.Variable(tf.constant(.1,shape=[2048]))
    r_level2 = tf.nn.relu(tf.matmul(x_level2,w_level2)+b_level2)
    return r_level2

def row_calc(x):
    x_level1 = tf.reshape(x,[-1,28,28,1])
    w_level1 = tf.Variable(tf.truncated_normal([16,3,1,32],stddev=.1))
    b_level1 = tf.Variable(tf.constant(.1,shape=[32]))
    r_level1 = tf.nn.relu(tf.nn.conv2d(x_level1,w_level1,strides=[1,4,1,1],padding='VALID') + b_level1)

    x_level2 = tf.reshape(r_level1,[-1,26*4*32])
    w_level2 = tf.Variable(tf.truncated_normal([26*4*32,1024],stddev=.1))
    b_level2 = tf.Variable(tf.constant(.1,shape=[1024]))
    r_level2 = tf.nn.relu(tf.matmul(x_level2,w_level2)+b_level2)
    return r_level2

def block_calc(x):
    '''
    第一层:使用5*5卷积核做卷积,特征数取为16个
    '''
    x_level1 = tf.reshape(x,[-1,28,28,1])
    w_level1 = tf.Variable(tf.truncated_normal([5,5,1,32],stddev=.1))
    b_level1 = tf.Variable(tf.constant(.1,shape=[32]))
#28*28*1
    r_level1 = tf.nn.relu(tf.nn.conv2d(x_level1,w_level1,strides=[1,1,1,1],padding='VALID') + b_level1)
#24*24*16
    h_level1 = tf.nn.max_pool(r_level1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
#12*12*16

    '''
    第二层:使用5*5卷积核做卷积
    '''
    x_level2 = h_level1
    w_level2 = tf.Variable(tf.truncated_normal([5,5,32,64],stddev=.1));
    b_level2 = tf.Variable(tf.constant(.1,shape=[64]));
    r_level2 = tf.nn.relu(tf.nn.conv2d(x_level2,w_level2,strides=[1,1,1,1],padding='SAME') + b_level2)
    h_level2 = tf.nn.max_pool(r_level2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

#12*12*256
    x_level3 = tf.reshape(h_level2,[-1,6*6*64])
    w_level3 = tf.Variable(tf.truncated_normal([6*6*64,1024],stddev=.1))
    b_level3 = tf.Variable(tf.constant(.1,shape=[1024]))
    r_level3 = tf.nn.relu(tf.matmul(x_level3,w_level3)+b_level3)

    return r_level3

if __name__ == '__main__':
#预处理数组
    mnist = read_data.DataSets(dlimit=-1)

#初始化session
    sess = tf.InteractiveSession()
    x = tf.placeholder(tf.float32,shape=[None,784])
    yy = tf.placeholder(tf.float32,shape=[None,10])
    kp = tf.placeholder(tf.float32)

    r_block = block_calc(x)
    r_row = row_calc(x)
    r_col = col_calc(x)

    r_all = tf.concat(axis=1,values=[r_block,r_row,r_col])

    x_level4 = tf.nn.dropout(r_all,kp)
    w_level4 = tf.Variable(tf.truncated_normal([4096,10],stddev=.1))
    b_level4 = tf.Variable(tf.constant(.1,shape=[10]))
    r_level4 = tf.matmul(x_level4,w_level4)+b_level4

    y = tf.nn.softmax(r_level4)
    cross_entropy = tf.reduce_sum(-tf.log(y) * yy)
    correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(yy,1)) # 计算准确度
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    train_step = tf.train.AdamOptimizer(5*1e-5).minimize(cross_entropy)
    sess.run(tf.global_variables_initializer())
    lastans = -1e10
    for i in range(0,1000000):
        if (i%50 == 0):
            print("Iteration #%d..."%i)
        batch_xs,batch_ys = mnist.train.next_batch(60)
        train_step.run(feed_dict={x:batch_xs,yy:batch_ys,kp:0.5})
        if i%200 == 20:
            train_accuracy = accuracy.eval(feed_dict={x:batch_xs, yy: batch_ys,kp: 1.0})
            print("step %d, current training accuracy %g"%(i, train_accuracy))
        if i%1000 == 20:
            train_accuracy = accuracy.eval(feed_dict={x:mnist.test.images, yy:mnist.test.labels,kp: 1.0})
            print("step %d, training accuracy %g"%(i, train_accuracy))
            if train_accuracy>=lastans:
                print("update result...")
                lastans = train_accuracy
                correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(yy,1))
                final_prediction = tf.argmax(y,1);
                result = final_prediction.eval(feed_dict={x:mnist.uploads.images,kp:1})
                print("saving result...")
                mnist.uploads.labels = result
                mnist.uploads.save('result.csv')
                mnist.uploads.save2('result2.csv')

 

花火

雪,稀疏撒在暗黑的画布上

光,渐渐出现

雪中光,光中雪,混杂,交融

雪渐浓,光依旧

霎时间,鲜红的“自杀”二字震撼人心。

如“花火”般绚烂绽放,定会像“花火”般壮烈谢幕。

 

海边

宁静

不会记住

那两声枪响

Python修饰器小应用——级数求和算法简化

假设我们遇到了

    \[\sum_{n=1}^{\infty} \frac{3n+5}{3^n}\]

这样一个东西,我们算出了结果,并且需要用python验算。

通过抽象,我们需要计算

    \[\sum_{n=1}^{\infty} f(x,n)\]

用普通的for语句比较麻烦,正巧python有一个叫做修饰器的东西可以简化运算,我们每次只需要把sigma内部的公式输入即可。

def series_sum(start):
    def _sum(func):
        def wrapper(x):
            ret = 0
            i = start
            while True:
                if (i>10000):
                    break
                t = func(x,i)
                if (i>100 and t==0):
                    break
                ret += t
                i += 1
            return ret
        return wrapper
    return _sum


if __name__ == '__main__':
    @series_sum(1)
    def func_a(x,n=None):
        return (3*n+5)/x**n
    @series_sum(0)
    def func_b(x,n=None):
        return x**n;
    print(func_a(3))
    print(func_b(0.5))

series_sum为一个修饰器,只用输入一次,之后我们只需要将我们的f(x,n)用series_sum函数修饰就好了,超级优美!