【问题标题】:How to keep the weight value to zero in a particular location using theano or lasagne?如何使用 theano 或 lasagne 在特定位置将重量值保持为零?
【发布时间】:2016-04-08 14:12:15
【问题描述】:

我是 Theano 和千层面的用户。

我在处理输入矩阵的可变长度时遇到了问题。

即)

x1 = [0, 1, 3]
x2 = [1, 2]

matrix_embedding = [ [ 0.1, 0.2, 0.3],
                     [ 0.4, 0.5, 0.6],
                     [ 0.2, 0.3, 0.5],
                     [ 0.5, 0.6, 0.7],    ]

matrix_embedding[x1] = [
                     [ 0.1, 0.2, 0.3],
                     [ 0.4, 0.5, 0.6],
                     [ 0.5, 0.6, 0.7]
                             ]

matrix_embedding[x2] = [
                     [ 0.4, 0.5, 0.6],
                     [ 0.2, 0.3, 0.5],
                             ]

所以,我尝试使用填充。

matrix_padding_embedding = [ [ 0.1, 0.2, 0.3],
                           [ 0.4, 0.5, 0.6],
                           [ 0.2, 0.3, 0.5],
                           [ 0.5, 0.6, 0.7],
                           [ 0.0, 0.0, 0.0] ]

x1 = [0, 1, 3]
x2 = [1, 2, -1]

matrix_embedding[x1] = [
                     [ 0.1, 0.2, 0.3],
                     [ 0.4, 0.5, 0.6],
                     [ 0.5, 0.6, 0.7]
                             ]

 matrix_embedding[x2] = [
                     [ 0.4, 0.5, 0.6],
                     [ 0.2, 0.3, 0.5],
                     [ 0.0, 0.0, 0.0]       ]

但是,经过处理后,theano会更新参数matrix_padding_embedding,所以matrix_padding_embedding[-1]不再是0。

matrix_padding_embedding[-1]中的权重值如何保持为零?

或者,是否有其他处理变长的方法?

【问题讨论】:

    标签: python neural-network theano lasagne


    【解决方案1】:

    您可以通过连接两个矩阵来创建填充矩阵,例如,

    import theano as the
    import theano.tensor as ten
    import numpy as np    
    matrix_embedding = the.shared(np.asarray([[0.1, 0.2, 0.3],
                                              [0.4, 0.5, 0.6],
                                              [0.2, 0.3, 0.5],
                                              [0.5, 0.6, 0.7]]))
    matrix_padding_embedding = ten.concatenate((matrix_embedding, ten.zeros((1, 3))))
    
    x = ten.lvector()
    y = ten.sum(matrix_padding_embedding[x])
    grad = the.grad(y, matrix_embedding)
    fn = the.function([x], [matrix_padding_embedding, grad])
    
    x2 = [1, 2, -1]
    p, g = fn(x2)
    print p
    print g
    

    结果是

    # [[ 0.1  0.2  0.3]
    #  [ 0.4  0.5  0.6]
    #  [ 0.2  0.3  0.5]
    #  [ 0.5  0.6  0.7]
    #  [ 0.   0.   0. ]]
    # 
    # [[ 0.  0.  0.]
    #  [ 1.  1.  1.]
    #  [ 1.  1.  1.]
    #  [ 0.  0.  0.]]
    

    【讨论】:

    • 谢谢,非常有用!!
    猜你喜欢
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 2021-03-03
    相关资源
    最近更新 更多