如何使用theano或烤宽面条在特定位置将重量值保持为零? [英] How to keep the weight value to zero in a particular location using theano or lasagne?

查看:90
本文介绍了如何使用theano或烤宽面条在特定位置将重量值保持为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是theano和千层面的用户.

I'm a theano and lasagne user.

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

I have a problem dealing with the variable length of the input matrix.

即)

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

所以,我尝试使用填充.

So, I try to use the padding.

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.

But, after processing, theano updates the parameters matrix_padding_embedding, so, matrix_padding_embedding[-1] no longer a 0.

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

How to keep the weight value to zero in matrix_padding_embedding[-1]?

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

Or, whether there are other ways of dealing with variable length?

推荐答案

您可以通过串联两个矩阵(例如,

you can create the padded matrix by concatenating two matrices, like,

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

这篇关于如何使用theano或烤宽面条在特定位置将重量值保持为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆