Skip to content

Commit b355fce

Browse files
ooplesclaude
andcommitted
fix: address pr comments and build errors in timeseries models
- Fix ambiguous type references by renaming internal classes: - LSTMLayer -> DeepARLstmCell - TransformerEncoderLayer -> InformerEncoderBlock - TransformerDecoderLayer -> InformerDecoderBlock - Replace _numOps.Tanh() with MathHelper.Tanh() for scalar operations - Remove unused variables (learningRate, error, batchLoss, headDim) - Fix quantile validation to allow 0 and 1 (inclusive bounds) - Fix integer overflow in stddev calculation by casting to double/long - Fix DeepANT PredictSingle to use features directly instead of pooled value - Fix LSTMVAE deserialization to rebuild encoder/decoder with correct dims - Fix LSTMEncoder/Decoder GetParameters to include all serialized weights - Initialize non-nullable fields with proper default values 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 94b3c2f commit b355fce

10 files changed

Lines changed: 112 additions & 94 deletions

File tree

src/Models/Options/InformerOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,5 @@ public InformerOptions(InformerOptions<T> other)
100100
/// the sequence length of the previous one.
101101
/// </para>
102102
/// </remarks>
103-
public int DistillingFactor { get; set} = 2;
103+
public int DistillingFactor { get; set; } = 2;
104104
}

src/Models/Options/NHiTSOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ public NHiTSOptions(NHiTSOptions<T> other)
4040

4141
NumStacks = other.NumStacks;
4242
NumBlocksPerStack = other.NumBlocksPerStack;
43-
PoolingModes = other.PoolingModes != null ? (string[])other.PoolingModes.Clone() : null;
44-
InterpolationModes = other.InterpolationModes != null ? (string[])other.InterpolationModes.Clone() : null;
43+
PoolingModes = other.PoolingModes != null ? (string[])other.PoolingModes.Clone() : new string[] { "MaxPool", "AvgPool", "AvgPool" };
44+
InterpolationModes = other.InterpolationModes != null ? (string[])other.InterpolationModes.Clone() : new string[] { "Linear", "Linear", "Linear" };
4545
LookbackWindow = other.LookbackWindow;
4646
ForecastHorizon = other.ForecastHorizon;
4747
HiddenLayerSize = other.HiddenLayerSize;
4848
NumHiddenLayers = other.NumHiddenLayers;
4949
LearningRate = other.LearningRate;
5050
Epochs = other.Epochs;
5151
BatchSize = other.BatchSize;
52-
PoolingKernelSizes = other.PoolingKernelSizes != null ? (int[])other.PoolingKernelSizes.Clone() : null;
52+
PoolingKernelSizes = other.PoolingKernelSizes != null ? (int[])other.PoolingKernelSizes.Clone() : new int[] { 2, 4, 8 };
5353
DropoutRate = other.DropoutRate;
5454
}
5555

src/Models/Options/TemporalFusionTransformerOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public TemporalFusionTransformerOptions(TemporalFusionTransformerOptions<T> othe
4949
LearningRate = other.LearningRate;
5050
Epochs = other.Epochs;
5151
BatchSize = other.BatchSize;
52-
QuantileLevels = other.QuantileLevels != null ? (double[])other.QuantileLevels.Clone() : null;
52+
QuantileLevels = other.QuantileLevels != null ? (double[])other.QuantileLevels.Clone() : new double[] { 0.1, 0.5, 0.9 };
5353
UseVariableSelection = other.UseVariableSelection;
5454
StaticCovariateSize = other.StaticCovariateSize;
5555
TimeVaryingKnownSize = other.TimeVaryingKnownSize;

src/TimeSeries/AnomalyDetection/DeepANT.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public class DeepANT<T> : TimeSeriesModelBase<T>
3333
private readonly INumericOperations<T> _numOps;
3434

3535
// CNN layers
36-
private List<ConvLayer<T>> _convLayers;
37-
private Matrix<T> _fcWeights;
38-
private Vector<T> _fcBias;
36+
private List<ConvLayer<T>> _convLayers = new List<ConvLayer<T>>();
37+
private Matrix<T> _fcWeights = new Matrix<T>(0, 0);
38+
private Vector<T> _fcBias = new Vector<T>(0);
3939

4040
// Anomaly detection threshold
4141
private T _anomalyThreshold;
@@ -168,21 +168,11 @@ public override T PredictSingle(Vector<T> input)
168168
features = conv.Forward(features);
169169
}
170170

171-
// Global average pooling
172-
T pooled = _numOps.Zero;
173-
if (features.Length > 0)
174-
{
175-
for (int i = 0; i < features.Length; i++)
176-
pooled = _numOps.Add(pooled, features[i]);
177-
pooled = _numOps.Divide(pooled, _numOps.FromDouble(features.Length));
178-
}
179-
180-
// Fully connected output
171+
// Fully connected output using features directly
181172
T output = _fcBias[0];
182-
Vector<T> pooledVec = new Vector<T>(new[] { pooled });
183-
for (int j = 0; j < Math.Min(_fcWeights.Columns, pooledVec.Length); j++)
173+
for (int j = 0; j < Math.Min(_fcWeights.Columns, features.Length); j++)
184174
{
185-
output = _numOps.Add(output, _numOps.Multiply(_fcWeights[0, j], pooledVec[Math.Min(j, pooledVec.Length - 1)]));
175+
output = _numOps.Add(output, _numOps.Multiply(_fcWeights[0, j], features[j]));
186176
}
187177

188178
return output;
@@ -366,7 +356,7 @@ public ConvLayer(int inputChannels, int outputChannels, int kernelSize)
366356
_kernelSize = kernelSize;
367357

368358
var random = new Random(42);
369-
double stddev = Math.Sqrt(2.0 / (inputChannels * kernelSize));
359+
double stddev = Math.Sqrt(2.0 / ((double)inputChannels * kernelSize));
370360

371361
_kernels = new Matrix<T>(outputChannels, inputChannels * kernelSize);
372362
for (int i = 0; i < _kernels.Rows; i++)

src/TimeSeries/AnomalyDetection/LSTMVAE.cs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ protected override void TrainCore(Matrix<T> x, Vector<T> y)
6969
{
7070
Vector<T> input = x.GetRow(i);
7171

72-
// Forward pass
73-
var (mean, logVar) = _encoder.Encode(input);
72+
// Forward pass - logVar is available for full VAE training with KL loss
73+
var (mean, _) = _encoder.Encode(input);
7474

7575
// Reparameterization trick (simplified - use mean in deterministic mode)
7676
Vector<T> z = mean.Clone();
@@ -237,6 +237,10 @@ protected override void DeserializeCore(BinaryReader reader)
237237
_options.LatentDim = reader.ReadInt32();
238238
_reconstructionThreshold = _numOps.FromDouble(reader.ReadDouble());
239239

240+
// Rebuild encoder/decoder to match deserialized dimensions
241+
_encoder = new LSTMEncoder<T>(_options.WindowSize, _options.LatentDim, _options.HiddenSize);
242+
_decoder = new LSTMDecoder<T>(_options.LatentDim, _options.WindowSize, _options.HiddenSize);
243+
240244
int encoderParamCount = reader.ReadInt32();
241245
var encoderParams = new Vector<T>(encoderParamCount);
242246
for (int i = 0; i < encoderParamCount; i++)
@@ -362,7 +366,7 @@ private Matrix<T> CreateRandomMatrix(int rows, int cols, double stddev, Random r
362366
{
363367
sum = _numOps.Add(sum, _numOps.Multiply(_weights[i, j], input[j]));
364368
}
365-
hidden[i] = _numOps.Tanh(sum);
369+
hidden[i] = MathHelper.Tanh(sum);
366370
}
367371

368372
// Compute mean
@@ -395,22 +399,44 @@ private Matrix<T> CreateRandomMatrix(int rows, int cols, double stddev, Random r
395399
public Vector<T> GetParameters()
396400
{
397401
var parameters = new List<T>();
402+
// Include all weights that contribute to ParameterCount
398403
for (int i = 0; i < _weights.Rows; i++)
399404
for (int j = 0; j < _weights.Columns; j++)
400405
parameters.Add(_weights[i, j]);
401406
for (int i = 0; i < _bias.Length; i++)
402407
parameters.Add(_bias[i]);
408+
for (int i = 0; i < _meanWeights.Rows; i++)
409+
for (int j = 0; j < _meanWeights.Columns; j++)
410+
parameters.Add(_meanWeights[i, j]);
411+
for (int i = 0; i < _meanBias.Length; i++)
412+
parameters.Add(_meanBias[i]);
413+
for (int i = 0; i < _logVarWeights.Rows; i++)
414+
for (int j = 0; j < _logVarWeights.Columns; j++)
415+
parameters.Add(_logVarWeights[i, j]);
416+
for (int i = 0; i < _logVarBias.Length; i++)
417+
parameters.Add(_logVarBias[i]);
403418
return new Vector<T>(parameters.ToArray());
404419
}
405420

406421
public void SetParameters(Vector<T> parameters)
407422
{
408423
int idx = 0;
424+
// Set all weights that contribute to ParameterCount
409425
for (int i = 0; i < _weights.Rows && idx < parameters.Length; i++)
410426
for (int j = 0; j < _weights.Columns && idx < parameters.Length; j++)
411427
_weights[i, j] = parameters[idx++];
412428
for (int i = 0; i < _bias.Length && idx < parameters.Length; i++)
413429
_bias[i] = parameters[idx++];
430+
for (int i = 0; i < _meanWeights.Rows && idx < parameters.Length; i++)
431+
for (int j = 0; j < _meanWeights.Columns && idx < parameters.Length; j++)
432+
_meanWeights[i, j] = parameters[idx++];
433+
for (int i = 0; i < _meanBias.Length && idx < parameters.Length; i++)
434+
_meanBias[i] = parameters[idx++];
435+
for (int i = 0; i < _logVarWeights.Rows && idx < parameters.Length; i++)
436+
for (int j = 0; j < _logVarWeights.Columns && idx < parameters.Length; j++)
437+
_logVarWeights[i, j] = parameters[idx++];
438+
for (int i = 0; i < _logVarBias.Length && idx < parameters.Length; i++)
439+
_logVarBias[i] = parameters[idx++];
414440
}
415441
}
416442

@@ -469,7 +495,7 @@ public Vector<T> Decode(Vector<T> latent)
469495
{
470496
sum = _numOps.Add(sum, _numOps.Multiply(_weights[i, j], latent[j]));
471497
}
472-
hidden[i] = _numOps.Tanh(sum);
498+
hidden[i] = MathHelper.Tanh(sum);
473499
}
474500

475501
// Decode to output
@@ -490,21 +516,33 @@ public Vector<T> Decode(Vector<T> latent)
490516
public Vector<T> GetParameters()
491517
{
492518
var parameters = new List<T>();
519+
// Include all weights that contribute to ParameterCount
493520
for (int i = 0; i < _weights.Rows; i++)
494521
for (int j = 0; j < _weights.Columns; j++)
495522
parameters.Add(_weights[i, j]);
496523
for (int i = 0; i < _bias.Length; i++)
497524
parameters.Add(_bias[i]);
525+
for (int i = 0; i < _outputWeights.Rows; i++)
526+
for (int j = 0; j < _outputWeights.Columns; j++)
527+
parameters.Add(_outputWeights[i, j]);
528+
for (int i = 0; i < _outputBias.Length; i++)
529+
parameters.Add(_outputBias[i]);
498530
return new Vector<T>(parameters.ToArray());
499531
}
500532

501533
public void SetParameters(Vector<T> parameters)
502534
{
503535
int idx = 0;
536+
// Set all weights that contribute to ParameterCount
504537
for (int i = 0; i < _weights.Rows && idx < parameters.Length; i++)
505538
for (int j = 0; j < _weights.Columns && idx < parameters.Length; j++)
506539
_weights[i, j] = parameters[idx++];
507540
for (int i = 0; i < _bias.Length && idx < parameters.Length; i++)
508541
_bias[i] = parameters[idx++];
542+
for (int i = 0; i < _outputWeights.Rows && idx < parameters.Length; i++)
543+
for (int j = 0; j < _outputWeights.Columns && idx < parameters.Length; j++)
544+
_outputWeights[i, j] = parameters[idx++];
545+
for (int i = 0; i < _outputBias.Length && idx < parameters.Length; i++)
546+
_outputBias[i] = parameters[idx++];
509547
}
510548
}

src/TimeSeries/ChronosFoundationModel.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public class ChronosFoundationModel<T> : TimeSeriesModelBase<T>
3333
private readonly INumericOperations<T> _numOps;
3434

3535
// Tokenization
36-
private Vector<T> _vocabularyCentroids;
36+
private Vector<T> _vocabularyCentroids = new Vector<T>(0);
3737
private int _vocabularySize;
3838

3939
// Transformer components
40-
private Matrix<T> _tokenEmbeddings;
41-
private List<TransformerBlock<T>> _transformerLayers;
42-
private Matrix<T> _outputProjection;
43-
private Vector<T> _outputBias;
40+
private Matrix<T> _tokenEmbeddings = new Matrix<T>(0, 0);
41+
private List<TransformerBlock<T>> _transformerLayers = new List<TransformerBlock<T>>();
42+
private Matrix<T> _outputProjection = new Matrix<T>(0, 0);
43+
private Vector<T> _outputBias = new Vector<T>(0);
4444

4545
public ChronosFoundationModel(ChronosOptions<T>? options = null)
4646
: base(options ?? new ChronosOptions<T>())
@@ -120,20 +120,15 @@ private T Detokenize(int tokenIdx)
120120

121121
protected override void TrainCore(Matrix<T> x, Vector<T> y)
122122
{
123-
T learningRate = _numOps.FromDouble(_options.LearningRate);
124-
123+
// Note: This is a simplified training loop. In practice, gradients would be computed and applied.
125124
for (int epoch = 0; epoch < _options.Epochs; epoch++)
126125
{
127126
for (int i = 0; i < x.Rows; i++)
128127
{
129128
Vector<T> input = x.GetRow(i);
130-
T target = y[i];
131-
132-
// Forward pass
133-
T prediction = PredictSingle(input);
134129

135-
// Simple gradient update (simplified)
136-
T error = _numOps.Subtract(target, prediction);
130+
// Forward pass - prediction computed for gradient calculation in full implementation
131+
_ = PredictSingle(input);
137132
}
138133
}
139134
}
@@ -374,7 +369,7 @@ public Vector<T> Forward(Vector<T> input)
374369
{
375370
sum = _numOps.Add(sum, _numOps.Multiply(_weights[i, j], input[j]));
376371
}
377-
output[i] = _numOps.Tanh(sum);
372+
output[i] = MathHelper.Tanh(sum);
378373
}
379374
return output;
380375
}

src/TimeSeries/DeepARModel.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ public class DeepARModel<T> : TimeSeriesModelBase<T>
4040
private readonly INumericOperations<T> _numOps;
4141

4242
// LSTM layers
43-
private List<LSTMLayer<T>> _lstmLayers;
44-
private Matrix<T> _outputWeights;
45-
private Vector<T> _outputBias;
43+
private List<DeepARLstmCell<T>> _lstmLayers = new List<DeepARLstmCell<T>>();
44+
private Matrix<T> _outputWeights = new Matrix<T>(0, 0);
45+
private Vector<T> _outputBias = new Vector<T>(0);
4646

4747
// Distribution parameters
48-
private Matrix<T> _meanWeights;
49-
private Vector<T> _meanBias;
50-
private Matrix<T> _scaleWeights;
51-
private Vector<T> _scaleBias;
48+
private Matrix<T> _meanWeights = new Matrix<T>(0, 0);
49+
private Vector<T> _meanBias = new Vector<T>(0);
50+
private Matrix<T> _scaleWeights = new Matrix<T>(0, 0);
51+
private Vector<T> _scaleBias = new Vector<T>(0);
5252

5353
/// <summary>
5454
/// Initializes a new instance of the DeepARModel class.
@@ -59,7 +59,7 @@ public DeepARModel(DeepAROptions<T>? options = null)
5959
{
6060
_options = options ?? new DeepAROptions<T>();
6161
_numOps = MathHelper.GetNumericOperations<T>();
62-
_lstmLayers = new List<LSTMLayer<T>>();
62+
_lstmLayers = new List<DeepARLstmCell<T>>();
6363

6464
ValidateDeepAROptions();
6565
InitializeModel();
@@ -100,7 +100,7 @@ private void InitializeModel()
100100
for (int i = 0; i < _options.NumLayers; i++)
101101
{
102102
int layerInputSize = (i == 0) ? inputSize : _options.HiddenSize;
103-
_lstmLayers.Add(new LSTMLayer<T>(layerInputSize, _options.HiddenSize));
103+
_lstmLayers.Add(new DeepARLstmCell<T>(layerInputSize, _options.HiddenSize));
104104
}
105105

106106
// Output projection for distribution parameters
@@ -136,8 +136,8 @@ protected override void TrainCore(Matrix<T> x, Vector<T> y)
136136
{
137137
int batchEnd = Math.Min(batchStart + _options.BatchSize, numSamples);
138138

139-
// Compute loss and update weights
140-
T batchLoss = ComputeBatchLoss(x, y, batchStart, batchEnd);
139+
// Compute loss for training metrics and update weights
140+
_ = ComputeBatchLoss(x, y, batchStart, batchEnd);
141141
UpdateWeights(x, y, batchStart, batchEnd, learningRate);
142142
}
143143
}
@@ -435,7 +435,7 @@ public override int ParameterCount
435435
/// <summary>
436436
/// Simplified LSTM layer implementation.
437437
/// </summary>
438-
internal class LSTMLayer<T>
438+
internal class DeepARLstmCell<T>
439439
{
440440
private readonly INumericOperations<T> _numOps;
441441
private readonly int _inputSize;
@@ -447,7 +447,7 @@ internal class LSTMLayer<T>
447447

448448
public int ParameterCount => _weights.Rows * _weights.Columns + _bias.Length;
449449

450-
public LSTMLayer(int inputSize, int hiddenSize)
450+
public DeepARLstmCell(int inputSize, int hiddenSize)
451451
{
452452
_numOps = MathHelper.GetNumericOperations<T>();
453453
_inputSize = inputSize;
@@ -489,7 +489,7 @@ public Vector<T> Forward(Vector<T> input)
489489
{
490490
sum = _numOps.Add(sum, _numOps.Multiply(_weights[i, j], combined[j]));
491491
}
492-
output[i] = _numOps.Tanh(sum); // Simplified activation
492+
output[i] = MathHelper.Tanh(sum); // Simplified activation
493493
_hiddenState[i] = output[i];
494494
}
495495

0 commit comments

Comments
 (0)