Theano 编程核心

1. 求偏导、更新以及模型的训练

以 LogisticRegression 为例:

  • 求损失函数关于参数的偏导:

    import theano.tensor as T
    g_W = T.gradient(cost=cost, wrt=clf.W)
    g_b = T.gradient(cost=cost, wrt=clf.b)
  • 参数的更新

    updates = [(clf.W, clf.W - learning_rate*g_W), (clf.b, clf.b - learning_rate*g_b)]
  • 模型的训练:

    train_model = theano.function(
        inputs=[index],
        outputs=cost,
        updates=updates,
        givens={
            x: trainset_x[index*batch_size:(index+1)*batch_size],
            y: trainset_y[index*batch_size:(index+1)*batch_size]
        }
    )
原文地址:https://www.cnblogs.com/mtcnn/p/9423004.html