This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
LinearTransformer.cs
55 lines (48 loc) · 1.94 KB
/
LinearTransformer.cs
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
using System;
using System.Collections.Generic;
namespace RCNet.Neural.Data.Transformers
{
/// <summary>
/// Implements the transformer of values from two input fields. Computes the linear equation (a*X + b*Y) where "X" is the value from the first input field and "Y" is the value from the second input field. Coefficients "a" and "b" are specified constants.
/// </summary>
[Serializable]
public class LinearTransformer : ITransformer
{
//Attributes
private readonly int _xFieldIdx;
private readonly int _yFieldIdx;
private readonly LinearTransformerSettings _cfg;
//Constructor
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="availableFieldNames">The collection of names of all available input fields.</param>
/// <param name="settings">The configuration.</param>
public LinearTransformer(List<string> availableFieldNames, LinearTransformerSettings settings)
{
_cfg = (LinearTransformerSettings)settings.DeepClone();
_xFieldIdx = availableFieldNames.IndexOf(_cfg.XInputFieldName);
_yFieldIdx = availableFieldNames.IndexOf(_cfg.YInputFieldName);
return;
}
//Methods
/// <inheritdoc />
public void Reset()
{
return;
}
/// <inheritdoc />
public double Transform(double[] data)
{
if (double.IsNaN(data[_xFieldIdx]))
{
throw new InvalidOperationException($"Invalid data value at input field index {_xFieldIdx} (NaN).");
}
if (double.IsNaN(data[_yFieldIdx]))
{
throw new InvalidOperationException($"Invalid data value at input field index {_yFieldIdx} (NaN).");
}
return _cfg.A * data[_xFieldIdx] + _cfg.B * data[_yFieldIdx];
}
}//LinearTransformer
}//Namespace