How can I assign/update subset of tensor shared variable in Theano?

前端 未结 2 533
野的像风
野的像风 2020-12-14 16:49

When compiling a function in theano, a shared variable(say X) can be updated by specifying updates=[(X, new_value)]. Now I am trying to update only

相关标签:
2条回答
  • 2020-12-14 17:20

    Use set_subtensor or inc_subtensor:

    from theano import tensor as T
    from theano import function, shared
    import numpy
    
    X = shared(numpy.array([0,1,2,3,4]))
    Y = T.vector()
    X_update = (X, T.set_subtensor(X[2:4], Y))
    f = function([Y], updates=[X_update])
    f([100,10])
    print X.get_value() # [0 1 100 10 4]
    

    There's now a page about this in the Theano FAQ: http://deeplearning.net/software/theano/tutorial/faq_tutorial.html

    0 讨论(0)
  • 2020-12-14 17:24

    This code should solve your problem:

    from theano import tensor as T
    from theano import function, shared
    import numpy
    
    X = shared(numpy.array([0,1,2,3,4], dtype='int'))
    Y = T.lvector()
    X_update = (X, X[2:4]+Y)
    f = function(inputs=[Y], updates=[X_update])
    f([100,10])
    print X.get_value()
    # output: [102 13]
    

    And here is the introduction about shared variables in the official tutorial.

    Please ask, if you have further questions!

    0 讨论(0)
提交回复
热议问题