CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

Problems with non-standard network structure

Open PaulDufort opened this issue 10 years ago • 9 comments

Hi,

I am a new user of CNTK on Windows 8.1 using VS 2013 and MKL. I've built the project, run the tests, and created a few toy projects with standard network structures and everything seems to be working fine.

I am now trying to employ a non-standard network structure and have run into some difficulties regarding how to specify it. Here is what I am trying to do:

  1. Start with a 15 x 1 column vector of features called features
  2. Put the first 3 elements of features into a 3 x 1 column vector called VC
  3. Put the remaining 12 elements from features into a 12 x 1 column vector called VN
  4. Reshape VN into a 3 x 4 matrix MN
  5. Multiply a 5 x 3 parameter matrix WC by VC to yield a 5 x 1 column vector TimesC
  6. Multiply a 5 x 3 parameter matrix WN by MN to yield a 5 x 4 matrix TimesN
  7. Add 5 x 1 column vector TimesC to _EACH_COLUMN* of 5 x 4 matrix TimesN, to get a new 5 x 4 matrix Plus1
  8. Add 5 x 1 parameter vector B to _EACH_COLUMN* of 5 x 4 matrix Plus1, to get a new 5 x 4 matrix Plus2
  9. Sum along the rows of Plus 2 to get a 5 x 1 column vector, which then goes into the CrossEntropyWithSoftmax. I am doing the summation explicitly with several Plus operators at the moment - is there a SumRowElements function or something like that to do the sum more parsimoniously?

In MATLAB I would use repmat to replicate the column vector or bsxfun for expansion of the singleton dimension for steps 7 and 8, but I'm not sure how to do this in CNTK. Is there a way? I tried naively using the Plus function, but it generated the following log file with an exception in it:

Log.txt

Any advice would be appreciated! Do I need to write my own nodes to do this kind of stuff?

Thanks, Paul

PaulDufort avatar Mar 15 '16 00:03 PaulDufort

@dongyu888 Can you please help answer this one?

mradmila avatar Mar 20 '16 21:03 mradmila

Thanks, but no need - I figured it out. The problem was the absence of a transpose function. I updated to the latest version yesterday and the transpose was there, so I was able to make it work.

Thanks, Paul

PaulDufort avatar Mar 20 '16 23:03 PaulDufort

A few quick notes (I have not had time to understand your scenario, so these may or may not help):

  • If you need the Transpose for a subsequent multiplication, you can also use TransposeTimes().
  • Plus() is able to broadcast. E.g. if you add a [3]-dim vector to a [3 x 5] tensor, it will automatically pad the vector to a [3 x 1] which will then be broadcast to [3 x 5].

frankseide avatar Mar 21 '16 04:03 frankseide

Thanks Frank.

I have got things working now through several uses of transpose, but it would be interesting to know if I could have avoided it in some cases. In your example above, will the broadcast work if the second (5) dimension of the [3 x 5] was not an MB dimension? Generally, does broadcast work across non-MB dimensions?

In one case I had to sum elements within rows of a tensor that was 2D, not including additional MB dimensions. The operand MN was a 5 x 4 matrix, with MB dimensions beyond those first two. I ended up doing something like:

Ones = Constant(1.0, 1, 4); MNT = Transpose(MN); ColSum = Times(Ones, MNT); RowSum = Transpose(ColSum);

This gave me the 5 x 1 (plus additional MB dimensions) that I needed. I found I could not just use Transpose followed by SumColumnElements, as the latter expects all dimensions beyond the first to be MB dimensions and this condition was not satisfied by MNT above.

PaulDufort avatar Mar 21 '16 13:03 PaulDufort

Closing the issue, since Paul seems to be unblocked.

mradmila avatar Mar 23 '16 21:03 mradmila

CNTK distinguishes between tensor dimensions of samples, and the samples themselves, which internally also manifest as a tensor dimension. For example, if your samples are 640 x 480 images, and you process in minibatches of 10, then internally CNTK uses a [640 x 480 x 10] tensor.

That last dimension is not accessible to NDL, it is managed by CNTK for you. The rule is

  • any InputValue gets that extra sample dimension
  • any LearnableParameter or Constant does not have that extra sample dimension
  • any operation broadcasts as needed

E.g. when you a bias vector to activations, you'd be broadcasting from a tensor that does not have the sample dimension to one that does. But in NDL, you won't see that, as you write your formula for individual samples, not for whole minibatches.

On the other hand, the tensor dimensions of each sample are under your control with NDL. To broadcast into the tensor dimensions of the sample, just create a tensor that matches the sample's dimension except set one or more dimensions to 1.

frankseide avatar Mar 24 '16 16:03 frankseide

That is cool, thanks! And great explanation!

Regards, Paul

Sent from my Samsung device over Bell's LTE network.

-------- Original message -------- From: Frank Seide [email protected] Date: 03-24-2016 12:54 PM (GMT-05:00) To: Microsoft/CNTK [email protected] Cc: "Dufort, Paul" [email protected] Subject: Re: [CNTK] Problems with non-standard network structure (#234)

CNTK distinguishes between tensor dimensions of samples, and the samples themselves, which internally also manifest as a tensor dimension. For example, if your samples are 640 x 480 images, and you process in minibatches of 10, then internally CNTK uses a [640 x 480 x 10] tensor.

That last dimension is not accessible to NDL, it is managed by CNTK for you. The rule is

  • any InputValue gets that extra sample dimension
  • any LearnableParameter or Constant does not have that extra sample dimension
  • any operation broadcasts as needed

E.g. when you a bias vector to activations, you'd be broadcasting from a tensor that does not have the sample dimension to one that does. But in NDL, you won't see that, as you write your formula for individual samples, not for whole minibatches.

On the other hand, the tensor dimensions of each sample are under your control with NDL. To broadcast into the tensor dimensions of the sample, just create a tensor that matches the sample's dimension except set one or more dimensions to 1.

� You are receiving this because you authored the thread. Reply to this email directly or view it on GitHubhttps://github.com/Microsoft/CNTK/issues/234#issuecomment-200921711

This e-mail may contain confidential and/or privileged information for the sole use of the intended recipient. Any review or distribution by anyone other than the person for whom it was originally intended is strictly prohibited. If you have received this e-mail in error, please contact the sender and delete all copies. Opinions, conclusions or other information contained in this e-mail may not be that of the organization.

PaulDufort avatar Mar 24 '16 17:03 PaulDufort

Does anybody know how to do the tranpose in NDL? When I am using ArrayTransposeDimensions, it says "EXCEPTION occurred: Undefined function or macro 'ArrayTransposeDimensions'" The same for TransposeDimensions.

Please help!

pinzhang10 avatar Jul 27 '16 22:07 pinzhang10

This is a duplicate of #576

frankseide avatar Jul 27 '16 22:07 frankseide