0

We use chainer.functions.linear to compute y=Wx+b.

In my case, I have to implement a linear link with one more dimension.

Say the input example is (c, x), the desired output is then y = W_c x + b. Let's ignore the bias and make it y = W_c x.
The cardinal number of {c} is known in advance (usually classes of samples).

Theoretically the W parameter can be implemented as a 3-d tensor (C, y_dims, x_dims). But what else? Do I have to iterate over the batch and extract W_c in shape (y_dims, x_dims) and call functions.linear merely for that (1, x_dims)-shaped example?

1 Answers1

0

Well, I myself found one solution to the question.

Let the data be of shapes as follows,

  • W: (C, y_dims, x_dims)
  • x: (batch, x_dims)
  • c: (batch, 1)

First I have to get a weight matrix for every x in batch:

W_c = chainer.functions.get_item(W, chainer.as_variable(c).data)
y = chainer.functions.batch_matmul(W_c, chainer.expand_dims(x, 2)) // in shape (batch, y_dims, 1)

So the key function here is get_item which accepts both numpy.ndarray and cupy.ndarray but NOT chainer.Variable. It works like numpy.take but is differentiable and saves a lot of work.