zoukankan      html  css  js  c++  java
  • Theano学习

    一、Numpy

    复杂的是Numpy的语法和矩阵的逻辑关系

    Numpy中复杂的则是矩阵切片以及[]中的一些操作,如双引号:

    Z[::2]指的是每两个中的第一个

    Z[1::2]则指的是每两个中的第二个

    因此第一个值指的是便宜,第二个指的是切片的大小

    np.random.random()是随机数函数

    np.array([])直接生成数组

    np.tile(array,(4,4)) 复制数组模式,例子中为复制4*4个

    np.dot(array,array) np.dot(matrix,matrix)向量乘向量、矩阵乘矩阵

    矩阵加向量是矩阵的每一行加上这个向量

    Z = np.zeros((5,5))
    Z += np.arange(5)
    print Z
    这三行的代码运行结果是:
     [[ 0.  1.  2.  3.  4.]
     [ 0.  1.  2.  3.  4.]
     [ 0.  1.  2.  3.  4.]
     [ 0.  1.  2.  3.  4.]
     [ 0.  1.  2.  3.  4.]] 

    还有很多numpy应该掌握的东西,和例子,看这个网址基本上就可以全部掌握了:https://github.com/rougier/numpy-100

    二、简单的Theano

    theano最主要的优势是不用手动写梯度,下面的代码很好地说明了这一问题:

    classifier = LogisticRegression(input=x, n_in=28 * 28, n_out=10)
    cost = classifier.negative_log_likelihood(y)
    g_W = T.grad(cost=cost, wrt=classifier.W)
    g_b = T.grad(cost=cost, wrt=classifier.b)
    updates = [(classifier.W, classifier.W - learning_rate * g_W),
               (classifier.b, classifier.b - learning_rate * g_b)]
    train_model = theano.function(
    inputs=[index],
    outputs=cost,
    updates=updates,
    givens={
    x: train_set_x[index * batch_size: (index + 1) * batch_size],
    y: train_set_y[index * batch_size: (index + 1) * batch_size]
    }
    )
    其中,classifier是定义的一个python类,作为逻辑斯蒂回归的分类器;
    cost是损失函数;
    g_W和g_b是根据cost自动计算出来的梯度,是精髓;
    updates定义了分类器中参数W和b的更新方式;
    train_model是定义的学习模型

    三、宏观地看一个用theano的分类器,和训练过程:
    分类器:
    __int__定义分类器的各种变量,以及简单的操作
    cost方法:学习过程中用来优化的目标函数,注意:要能求导,这样T.grad就可以自动给它求导了
    errors方法:validate和test用来判断的函数,和学习参数没关系,但是确实真实的评价指标,如果F1之类的作为评价,这个errors函数也要写成综合准确率和召回率的值

    注意:The initial values for the weights of a hidden layer should be uniformly sampled from a symmetric interval that depends on the activation function.

    在以MLP(多层神经网络)为模型的时候,有点奇怪,例子中用了一个HiddenLayer和一个LogisticRegression Layer,后者直接就能重用之前的那个,还挺酷的。
    两层相连接的方式完全是靠损失函数(cost函数)内部自动的联系,上一层的输出作为下一层的输入,所以层与层之间的输入输出要设计好,贴上一点代码:
    self.hiddenLayer = HiddenLayer(
    rng=rng,
    input=input,
    n_in=n_in,
    n_out=n_hidden,
    activation=T.tanh
    )
    self.logRegressionLayer = LogisticRegression(
    input=self.hiddenLayer.output,
    n_in=n_hidden,
    n_out=n_out
    )
    两层之间是靠输入输出联系的,所以HiddenLayer的输出一定要是向参数中带入了上一层input的值的输出,例如这个例子中的如下:
    lin_output = T.dot(input, self.W) + self.b
    self.output = (
    lin_output if activation is None
    else activation(lin_output)
    )

     四、卷积和Maxpooling
      max-pooling is a form of non-linear down-sampling.

    如何选择卷积层的filter size和filter个数:
    Since feature map size decreases with depth, layers near the input layer will tend to have fewer filters while layers higher up can have much more. 其中,feature map是指变换后的image,就是随着层数增加而大小减小的那个,最最开始也就是原image
    The number of feature maps directly controls capacity and so that depends on the number of available examples and the complexity of the task.

    Tips

    If you want to try this model on a new dataset, here are a few tips that can help you get better results:

    
    
    • Whitening the data (e.g. with PCA)
    • Decay the learning rate in each epoch
    五、Autoencoder

    
    
  • 相关阅读:
    tomcat常见报错解决方法汇总
    C++中socket编程
    Winsock解析
    等价类划分的原则
    在线编译器
    条件覆盖,路径覆盖,语句覆盖,分支覆盖解释
    并发测试
    针对C程序员的 C++
    缸中之脑
    什么是薛定谔的猫
  • 原文地址:https://www.cnblogs.com/cloudssdut/p/theano.html
Copyright © 2011-2022 走看看