|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// <copyright file="ElementwiseVectorAddOperation.cs" author="ameritusweb" date="5/2/2023"> |
| 3 | +// Copyright (c) 2023 ameritusweb All rights reserved. |
| 4 | +// </copyright> |
| 5 | +//------------------------------------------------------------------------------ |
| 6 | +namespace ParallelReverseAutoDiff.RMAD |
| 7 | +{ |
| 8 | + using System; |
| 9 | + using System.Threading.Tasks; |
| 10 | + |
| 11 | + /// <summary> |
| 12 | + /// Element-wise add operation. |
| 13 | + /// </summary> |
| 14 | + public class ElementwiseVectorAddOperation : Operation |
| 15 | + { |
| 16 | + private Matrix input1; |
| 17 | + private Matrix input2; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// A common method for instantiating an operation. |
| 21 | + /// </summary> |
| 22 | + /// <param name="net">The neural network.</param> |
| 23 | + /// <returns>The instantiated operation.</returns> |
| 24 | + public static IOperation Instantiate(NeuralNetwork net) |
| 25 | + { |
| 26 | + return new ElementwiseVectorAddOperation(); |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Performs the forward operation for the element-wise vector summation function. |
| 31 | + /// </summary> |
| 32 | + /// <param name="input1">The first input to the element-wise vector summation operation.</param> |
| 33 | + /// <param name="input2">The second input to the element-wise vector summation operation.</param> |
| 34 | + /// <returns>The output of the element-wise vector summation operation.</returns> |
| 35 | + public Matrix Forward(Matrix input1, Matrix input2) |
| 36 | + { |
| 37 | + this.input1 = input1; |
| 38 | + this.input2 = input2; |
| 39 | + |
| 40 | + this.Output = new Matrix(this.input1.Rows, this.input1.Cols); |
| 41 | + Parallel.For(0, input1.Rows, i => |
| 42 | + { |
| 43 | + for (int j = 0; j < input1.Cols / 2; j++) |
| 44 | + { |
| 45 | + // Accessing the magnitudes and angles from the concatenated matrices |
| 46 | + double magnitude = input1[i, j]; |
| 47 | + double angle = input1[i, j + (input1.Cols / 2)]; |
| 48 | + |
| 49 | + double wMagnitude = input2[i, j]; |
| 50 | + double wAngle = input2[i, j + (input2.Cols / 2)]; |
| 51 | + |
| 52 | + // Compute vector components |
| 53 | + double x1 = magnitude * Math.Cos(angle); |
| 54 | + double y1 = magnitude * Math.Sin(angle); |
| 55 | + double x2 = wMagnitude * Math.Cos(wAngle); |
| 56 | + double y2 = wMagnitude * Math.Sin(wAngle); |
| 57 | + |
| 58 | + double sumx = x1 + x2; |
| 59 | + double sumy = y1 + y2; |
| 60 | + |
| 61 | + // Compute resultant vector magnitude and angle |
| 62 | + double resultMagnitude = Math.Sqrt((sumx * sumx) + (sumy * sumy)); |
| 63 | + double resultAngle = Math.Atan2(sumy, sumx); |
| 64 | + |
| 65 | + this.Output[i, j] = resultMagnitude; |
| 66 | + this.Output[i, j + (this.input1.Cols / 2)] = resultAngle; |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + return this.Output; |
| 71 | + } |
| 72 | + |
| 73 | + /// <inheritdoc /> |
| 74 | + public override BackwardResult Backward(Matrix dOutput) |
| 75 | + { |
| 76 | + Matrix dInput1 = new Matrix(this.input1.Rows, this.input1.Cols); |
| 77 | + Matrix dInput2 = new Matrix(this.input2.Rows, this.input2.Cols); |
| 78 | + |
| 79 | + Parallel.For(0, this.input1.Rows, i => |
| 80 | + { |
| 81 | + for (int j = 0; j < this.input1.Cols / 2; j++) |
| 82 | + { |
| 83 | + var magnitude = this.input1[i, j]; |
| 84 | + var angle = this.input1[i, j + (this.input1.Cols / 2)]; |
| 85 | + var wMagnitude = this.input2[i, j]; |
| 86 | + var wAngle = this.input2[i, j + (this.input2.Cols / 2)]; |
| 87 | + |
| 88 | + var x1 = magnitude * Math.Cos(angle); |
| 89 | + var y1 = magnitude * Math.Sin(angle); |
| 90 | + var x2 = wMagnitude * Math.Cos(wAngle); |
| 91 | + var y2 = wMagnitude * Math.Sin(wAngle); |
| 92 | + |
| 93 | + var combinedX = x1 + x2; |
| 94 | + var combinedY = y1 + y2; |
| 95 | + |
| 96 | + // Compute gradients for magnitude and angle |
| 97 | + double dResultMagnitude_dX = combinedX / this.Output[i, j]; |
| 98 | + double dResultMagnitude_dY = combinedY / this.Output[i, j]; |
| 99 | + |
| 100 | + double dResultAngle_dX = -combinedY / ((combinedX * combinedX) + (combinedY * combinedY)); |
| 101 | + double dResultAngle_dY = combinedX / ((combinedX * combinedX) + (combinedY * combinedY)); |
| 102 | + |
| 103 | + // Chain rule to compute gradients for input vectors |
| 104 | + dInput1[i, j] = (dOutput[i, j] * dResultMagnitude_dX * Math.Cos(angle)) + |
| 105 | + (dOutput[i, j + (this.input1.Cols / 2)] * dResultAngle_dX * -Math.Sin(angle)); |
| 106 | + dInput1[i, j + (this.input1.Cols / 2)] = (dOutput[i, j] * dResultMagnitude_dY * Math.Sin(angle)) + |
| 107 | + (dOutput[i, j + (this.input1.Cols / 2)] * dResultAngle_dY * Math.Cos(angle)); |
| 108 | + |
| 109 | + dInput2[i, j] = (dOutput[i, j] * dResultMagnitude_dX * Math.Cos(wAngle)) + |
| 110 | + (dOutput[i, j + (this.input2.Cols / 2)] * dResultAngle_dX * -Math.Sin(wAngle)); |
| 111 | + dInput2[i, j + (this.input2.Cols / 2)] = (dOutput[i, j] * dResultMagnitude_dY * Math.Sin(wAngle)) + |
| 112 | + (dOutput[i, j + (this.input2.Cols / 2)] * dResultAngle_dY * Math.Cos(wAngle)); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + return new BackwardResultBuilder() |
| 117 | + .AddInputGradient(dInput1) |
| 118 | + .AddInputGradient(dInput2) |
| 119 | + .Build(); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments