This repository was archived by the owner on Feb 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputOutput.cs
More file actions
69 lines (56 loc) · 1.73 KB
/
InputOutput.cs
File metadata and controls
69 lines (56 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
namespace NeuralNetwork;
[Serializable]
public unsafe class Input : Layer
{
public event Action<Tensor> OnBackPropEnd;
public Input(Tensor.ShapeInfo shape, string name = null) : base(name)
{
this.inputShape = shape;
outputShape = inputShape;
}
public override void Init(Optimizer optimizer)
{
inputDerivatives = inputShape;
}
public void Forward(Tensor input, bool training)
{
int actualMBSize = input.shape.n0;
if (input.shape.nF0 % inputShape.nF1 == 0)
nextLayer.Forward(input, in actualMBSize, in training);
else throw new Exception("wrong format of input");
}
public void Forward(Array input, bool training)
{
Forward(Tensor.Create(input).Reshape(inputShape), training);
}
public sealed override void BackProp(Tensor deriv, in int actualMBSize)
{
deriv.CopyTo(inputDerivatives);
OnBackPropEnd?.Invoke(inputDerivatives);
}
}
[Serializable]
public unsafe class Output : Layer
{
public Output(string name = null) : base(name)
{
}
public sealed override Tensor OutputTensor => output;
public event Action<Tensor> OnForwardEnd;
public sealed override void Init(Optimizer optimizer)
{
output = new(inputShape);
}
public sealed override void Forward(Tensor input, in int actualMBSize, in bool training)
{
input.CopyTo(output);
OnForwardEnd?.Invoke(output);
}
public void BackProp(Tensor deriv)
{
int actualMBSize = deriv.shape.n0;
if (deriv.shape.rank == inputShape.rank)
prevLayer.BackProp(deriv, in actualMBSize);
else prevLayer.BackProp(deriv.Reshape(inputShape), actualMBSize);
}
}