如何使用 theano 或 lasagne 在特定位置将权重值保持为零?

How to keep the weight value to zero in a particular location using theano or lasagne?

我是 theano 和 lasagne 用户。

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

即)

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]中保持权重值为零?

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

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

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.]]