2

I'm a theano and lasagne user.

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

i.e)

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

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

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

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

이경호
  • 97
  • 9

1 Answers1

0

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

results are

# [[ 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.]]
dontloo
  • 10,067
  • 4
  • 29
  • 50