1-train-CBOW.py 文件源码

python
阅读 60 收藏 0 点赞 0 评论 0

项目:Deep-Learning-with-Theano 作者: PacktPublishing 项目源码 文件源码
def get_train_model(data, inputs, loss, params, batch_size=32):

    """
        trainer: Function to define the trainer of the model on the data set that bassed as the parameters of the function


        parameters:
            contexts: List of the contexts (the input of the trainer)
            targets: List of the targets.

        return:
            Theano function represents the train model
    """



    data_contexts = data[0]
    data_targets = data[1]

    context = inputs[0]
    target = inputs[1]


    learning_rate = T.fscalar('learning_rate') # theano input: the learning rate, the value of this input
                                               # can be constant like 0.1 or
                                               #it can be come from a function like a decay learning rate function





    index = T.lscalar('index') # the index of minibatch



    g_params = T.grad(cost=loss, wrt=params)

    updates = [
            (param, param - learning_rate * gparam)
            for param, gparam in zip(params, g_params)
    ]


    train_fun = theano.function(
        [index, learning_rate],
        loss,
        updates=updates,
        givens={
            context: data_contexts[index * args.batch_size: (index + 1) * args.batch_size],
            target: data_targets[index * args.batch_size: (index + 1) * args.batch_size]
        }
    )


    return train_fun
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号