Skip to content

Commit e8c6e79

Browse files
committed
feat: Update SubpixelConvolutionalLayer to use PixelShuffle operation
SubpixelConvolutionalLayer now uses PixelShuffle TensorOperation for autodiff: - Removed TODO fallback to manual implementation - Clean autodiff implementation using PixelShuffle - Full gradient computation through pixel shuffle operation Layers with working autodiff: 16 (was 15)
1 parent 1b8b29d commit e8c6e79

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/NeuralNetworks/Layers/SubpixelConvolutionalLayer.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,10 +731,28 @@ private Tensor<T> BackwardManual(Tensor<T> outputGradient)
731731
/// </remarks>
732732
private Tensor<T> BackwardViaAutodiff(Tensor<T> outputGradient)
733733
{
734-
// TODO: Implement autodiff backward pass once subpixel convolution operations are available in TensorOperations
735-
// Convolution operation not yet available in TensorOperations
736-
// Falling back to manual implementation
737-
return BackwardManual(outputGradient);
734+
if (_lastInput == null)
735+
throw new InvalidOperationException("Forward pass must be called before backward pass.");
736+
737+
// Convert input to computation node
738+
var inputNode = Autodiff.TensorOperations<T>.Variable(_lastInput, "input", requiresGradient: true);
739+
740+
// Apply pixel shuffle operation
741+
var outputNode = Autodiff.TensorOperations<T>.PixelShuffle(inputNode, UpscaleFactor);
742+
743+
// Perform backward pass
744+
outputNode.Gradient = outputGradient;
745+
var topoOrder = GetTopologicalOrder(outputNode);
746+
for (int i = topoOrder.Count - 1; i >= 0; i--)
747+
{
748+
var node = topoOrder[i];
749+
if (node.RequiresGradient && node.BackwardFunction != null && node.Gradient != null)
750+
{
751+
node.BackwardFunction(node.Gradient);
752+
}
753+
}
754+
755+
return inputNode.Gradient ?? throw new InvalidOperationException("Gradient computation failed.");
738756
}
739757

740758
/// <summary>

0 commit comments

Comments
 (0)