Commit ede9886
fix(networks): resnet/vgg train adds batch dim for 3d input
ResNet/VGG's Forward() explicitly accepts 3D [C,H,W] input and expands
it to 4D [1,C,H,W] before running the layer stack. Their Train()
overrides, however, called TrainWithTape directly — which delegates to
NeuralNetworkBase.ForwardForTraining, which does NOT add a batch dim
and just runs the raw tensor through every layer.
For a 3D input [3, 32, 32], the conv/pool chain preserves the rank-3
shape and the classifier's AdaptiveAveragePool + Flatten ends up
producing [512, 1] (the 512 final-block channel count gets treated as
a batch dim by FlattenLayer.Forward's "preserve first dim" rule). The
final DenseLayer with inputSize=512 sees actualInputSize=1 via
input.Shape[^1], calls EnsureWeightShapeForInput(1) which resizes
weights to [1, 10], and produces [512, 10] — which then fails the
loss shape check in EnsureTargetMatchesPredicted because the target
is [10].
Fix: mirror Forward()'s expansion in Train() — when input is 3D, add
a leading batch dim to BOTH input and target before dispatching to
TrainWithTape. Any 4D input is passed through untouched. The target
expansion is guarded so a caller that already provided a batched
target is not double-expanded.
Verified locally, all 4 of the previously-failing tests now pass:
- ResNetNetwork_Train_CompletesWithoutError
- ResNetNetwork_Train_LossDecreases
- VGGNetwork_Train_CompletesWithoutError
- VGGNetwork_Train_LossDecreases
Closes the 08a NN-Classic (ResNet/VGG/DenseNet) CI shard failure from
the PR #1154 triage.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent b187e31 commit ede9886
2 files changed
Lines changed: 30 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
521 | 521 | | |
522 | 522 | | |
523 | 523 | | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
524 | 537 | | |
525 | 538 | | |
526 | 539 | | |
527 | 540 | | |
528 | 541 | | |
529 | | - | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
530 | 547 | | |
531 | 548 | | |
532 | 549 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
388 | 388 | | |
389 | 389 | | |
390 | 390 | | |
391 | | - | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
392 | 403 | | |
393 | 404 | | |
394 | 405 | | |
| |||
0 commit comments