【问题标题】:Can I store intermediate results?我可以存储中间结果吗?
【发布时间】:2023-03-20 21:28:01
【问题描述】:

我想存储中间结果以避免对一件事进行多次计算。我正在寻找的是这样的:

    h1_activ = sigmoid(self.bias_visiblie + T.dot(D, self.W))
    h1_sample = h1_activ > rnds.uniform((n_samples, self.n_hidden )) 

    f_h1_sample = theano.function(
        inputs=[D],
        outputs=h1_sample,
        # I'd like to take the result from 'h1_sample' and store it into 'H1_sample'
        updates=[(self.H1_sample, ??? )] 
    )

上面的代码当然不会运行,但是有没有办法做这样的事情?将中间值存储到共享变量中?

【问题讨论】:

    标签: gpu theano


    【解决方案1】:

    您可以将使用相同中间结果的最终结果写入相同的theano.function

    例如:

    h1_activ = sigmoid(self.bias_visiblie + T.dot(D, self.W))
    h1_sample = h1_activ > rnds.uniform((n_samples, self.n_hidden )) 
    # h2_sample use the intermediate result h1_sample.
    h2_sample = h1_sample * 2
    
    f_h1_sample = theano.function(
        inputs=[D],
        outputs=[h1_sample, h2_sample],
    )
    

    h2_smaple 是使用 h1_sample 的最终结果。

    您还可以保存中间结果并将它们用作另一个theano.function 的输入。

    不同的theano.functions对应不同的计算图。我认为不同的计算图之间不能共享任何计算。

    【讨论】:

    • 嗨!当你说“你可以写出最终结果”时,我不知道我会怎么做。我不想在 GPU 和 CPU RAM 之间洗牌太多数据,那么在 GPU 的 RAM 上最有效地执行图形后,我将如何存储中间结果?
    • @displayname 我在原始答案中添加了一个示例。
    • 但这不会将数据复制回主机吗?
    猜你喜欢
    • 2012-03-11
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多