5

I am teaching myself CNTK by going through this tutorial.

There is a function defined as

def linear_layer(input_var, output_dim):

    input_dim = input_var.shape[0]
    weight_param = C.parameter(shape=(input_dim, output_dim))
    bias_param = C.parameter(shape=(output_dim))

    mydict['w'], mydict['b'] = weight_param, bias_param

    return C.times(input_var, weight_param) + bias_param

In linear algebra, when multiplying 2 matrices together, it only makes sense when the inner dimensions match. More explicitly, the 2nd dimension of the left side, must equal the 1st dimension of the right side.

In this function, the 2nd (right) parameter of the 'times' function is 'weight_param', which has it's first dimension set to be the same as the first dimension of the first (left) parameter. This should not work because the inner dimensions do not match.

I am not sure whether input_var is (n x 2) or (2 x n) but either way should cause an error. If it is (n x 2) then weight_param is (n x 2) and (n x 2)*(n x 2) should not compute. If it is (2 x n) then weight_param is (2 x 2) and (2 x n)*(2 x 2) should not compute.

But somehow this runs just fine. Even more confusing, is if I change the final line of the function to

return C.times_transpose(input_var, weight_param) + bias_param

it produces exaclty the same results.

So what is going on here? Why does this work? Why does the transpose version produce the same results?

PS: I am teaching myself python at the same time as cntk so these observations may be explained by something about python. Let me know.

Chechy Levas
  • 2,206
  • 1
  • 13
  • 28

1 Answers1

0

input_var is (n x 2), where n represents batch axis. If you print input_var, you will get something like Input('Input7', [#], [2]), # means batch axis.

input_var.shape[0] returns 2, so the first dimension of the weight_param is 2 (weight_param doesn't have batch axis because it's a parameter, not a variable).

So inner dimensions match.

C.times_transpose produces the same results because output_dim also happens to be 2 in the example.

Sergii Dymchenko
  • 6,890
  • 1
  • 21
  • 46