Skip to content

Commit aeb3dfb

Browse files
ooplesclaude
andcommitted
fix: resolve all build errors in nested learning implementation
Fixed multiple compilation errors across HopeNetwork and ContinuumMemorySystemLayer: - Fixed RecurrentLayer and DenseLayer constructor calls to use correct signatures - Added explicit casts to resolve activation function constructor ambiguity - Fixed LayerBase constructor to use 2-parameter version - Initialized non-nullable fields in HopeNetwork constructor - Added null coalescing for nullable loss function parameter - Replaced ILossFunction method names (ComputeLoss → CalculateLoss) - Fixed Vector construction to use AiDotNet pattern instead of MathNet.Numerics - Fixed Tensor construction using correct constructor signature - Replaced protected Parameters access with public ParameterCount and GetParameters() - Added LastInput and LastOutput fields to ContinuumMemorySystemLayer - Fixed test ambiguity in Forward() calls with explicit type casts Build now completes successfully with 0 errors. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent c9e5a1a commit aeb3dfb

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

src/NestedLearning/HopeNetwork.cs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public HopeNetwork(
3939
int numCMSLevels = 4,
4040
int numRecurrentLayers = 3,
4141
int inContextLearningLevels = 5)
42-
: base(architecture, lossFunction, maxGradNorm: 1.0)
42+
: base(architecture, lossFunction ?? new MeanSquaredErrorLoss<T>(), maxGradNorm: 1.0)
4343
{
4444
_hiddenDim = hiddenDim;
4545
_numCMSLevels = numCMSLevels;
@@ -48,6 +48,10 @@ public HopeNetwork(
4848
_adaptationStep = 0;
4949
_selfModificationRate = _numOps.FromDouble(0.01);
5050

51+
// Initialize arrays to avoid non-nullable warnings
52+
_cmsBlocks = new ContinuumMemorySystemLayer<T>[numCMSLevels];
53+
_recurrentLayers = new RecurrentLayer<T>[numRecurrentLayers];
54+
5155
// Initialize context flow for multi-level optimization
5256
_contextFlow = new ContextFlow<T>(hiddenDim, inContextLearningLevels);
5357

@@ -79,9 +83,9 @@ protected override void InitializeLayers()
7983
for (int i = 0; i < _numRecurrentLayers; i++)
8084
{
8185
_recurrentLayers[i] = new RecurrentLayer<T>(
82-
inputShape: new[] { _hiddenDim },
83-
outputUnits: _hiddenDim,
84-
activation: ActivationFunction.Tanh);
86+
_hiddenDim,
87+
_hiddenDim,
88+
(IActivationFunction<T>)new TanhActivation<T>());
8589

8690
Layers.Add(_recurrentLayers[i]);
8791
}
@@ -330,11 +334,16 @@ public void ResetRecurrentState()
330334
/// </summary>
331335
public void AddOutputLayer(int outputDim, ActivationFunction activation = ActivationFunction.Linear)
332336
{
333-
_outputLayer = new DenseLayer<T>(
334-
inputShape: new[] { _hiddenDim },
335-
outputUnits: outputDim,
336-
activation: activation);
337+
IActivationFunction<T> activationFunc = activation switch
338+
{
339+
ActivationFunction.Tanh => new TanhActivation<T>(),
340+
ActivationFunction.Softmax => new SoftmaxActivation<T>(),
341+
ActivationFunction.Sigmoid => new SigmoidActivation<T>(),
342+
ActivationFunction.ReLU => new ReLUActivation<T>(),
343+
_ => new IdentityActivation<T>()
344+
};
337345

346+
_outputLayer = new DenseLayer<T>(_hiddenDim, outputDim, activationFunc);
338347
Layers.Add(_outputLayer);
339348
}
340349

@@ -451,11 +460,18 @@ public override void Train(Tensor<T> input, Tensor<T> expectedOutput)
451460
// Forward pass
452461
var prediction = Forward(input);
453462

463+
// Convert tensors to vectors for loss computation
464+
var predictionVector = new Vector<T>(prediction.ToArray());
465+
var expectedVector = new Vector<T>(expectedOutput.ToArray());
466+
454467
// Compute loss
455-
var loss = LossFunction.ComputeLoss(prediction, expectedOutput);
468+
var loss = LossFunction.CalculateLoss(predictionVector, expectedVector);
456469

457470
// Compute loss gradient
458-
var lossGradient = LossFunction.ComputeGradient(prediction, expectedOutput);
471+
var lossGradientVector = LossFunction.CalculateDerivative(predictionVector, expectedVector);
472+
473+
// Convert gradient vector back to tensor for backward pass
474+
var lossGradient = new Tensor<T>(prediction.Shape, lossGradientVector);
459475

460476
// Backward pass
461477
Backward(lossGradient);
@@ -503,7 +519,7 @@ public override ModelMetadata<T> GetModelMetadata()
503519
{ "RecurrentLayers", _numRecurrentLayers },
504520
{ "InContextLearningLevels", _inContextLearningLevels },
505521
{ "AdaptationStep", _adaptationStep },
506-
{ "SelfModificationRate", _selfModificationRate },
522+
{ "SelfModificationRate", (object?)_selfModificationRate ?? 0 },
507523
{ "ParameterCount", ParameterCount },
508524
{ "LayerCount", Layers?.Count ?? 0 }
509525
};

src/NeuralNetworks/Layers/ContinuumMemorySystemLayer.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class ContinuumMemorySystemLayer<T> : LayerBase<T>
2222
private readonly int[] _stepCounters;
2323
private readonly Vector<T>[] _storedInputs; // Store input to each MLP block for Modified GD
2424
private int _globalStep;
25+
private Tensor<T>? LastInput;
26+
private Tensor<T>? LastOutput;
2527
private static readonly INumericOperations<T> _numOps = MathHelper.GetNumericOperations<T>();
2628

2729
/// <summary>
@@ -43,7 +45,7 @@ public ContinuumMemorySystemLayer(
4345
int numFrequencyLevels = 3,
4446
int[]? updateFrequencies = null,
4547
T[]? learningRates = null)
46-
: base(inputShape, new[] { hiddenDim }, null, null)
48+
: base(inputShape, new[] { hiddenDim })
4749
{
4850
// Validate inputs
4951
if (inputShape == null || inputShape.Length == 0)
@@ -103,10 +105,7 @@ public ContinuumMemorySystemLayer(
103105

104106
for (int i = 0; i < numFrequencyLevels; i++)
105107
{
106-
_mlpBlocks[i] = new DenseLayer<T>(
107-
inputShape: new[] { currentDim },
108-
outputUnits: hiddenDim,
109-
activation: ActivationFunction.ReLU);
108+
_mlpBlocks[i] = new DenseLayer<T>(currentDim, hiddenDim, (IActivationFunction<T>)new ReLUActivation<T>());
110109
currentDim = hiddenDim;
111110
}
112111

@@ -115,7 +114,7 @@ public ContinuumMemorySystemLayer(
115114
_stepCounters = new int[numFrequencyLevels];
116115
for (int i = 0; i < numFrequencyLevels; i++)
117116
{
118-
int paramCount = _mlpBlocks[i].Parameters.Length;
117+
int paramCount = _mlpBlocks[i].ParameterCount;
119118
_accumulatedGradients[i] = new Vector<T>(paramCount);
120119
_stepCounters[i] = 0;
121120
}
@@ -238,7 +237,7 @@ private void UpdateLevelParameters(int level)
238237
if (_mlpBlocks[level] == null)
239238
throw new InvalidOperationException($"MLP block at level {level} is null");
240239

241-
var currentParams = _mlpBlocks[level].Parameters;
240+
var currentParams = _mlpBlocks[level].GetParameters();
242241
if (currentParams == null || currentParams.Length == 0)
243242
throw new InvalidOperationException($"MLP block at level {level} has no parameters");
244243

@@ -295,8 +294,8 @@ public void ConsolidateMemory()
295294
if (_mlpBlocks[i + 1] == null)
296295
throw new InvalidOperationException($"MLP block at level {i + 1} is null");
297296

298-
var fastParams = _mlpBlocks[i].Parameters;
299-
var slowParams = _mlpBlocks[i + 1].Parameters;
297+
var fastParams = _mlpBlocks[i].GetParameters();
298+
var slowParams = _mlpBlocks[i + 1].GetParameters();
300299

301300
if (fastParams == null || fastParams.Length == 0)
302301
throw new InvalidOperationException($"Fast MLP at level {i} has no parameters");
@@ -404,7 +403,7 @@ public override Vector<T> GetParameters()
404403
if (mlp == null)
405404
throw new InvalidOperationException("MLP block is null");
406405

407-
totalParams += mlp.Parameters.Length;
406+
totalParams += mlp.ParameterCount;
408407
}
409408

410409
// Concatenate all parameters
@@ -413,7 +412,7 @@ public override Vector<T> GetParameters()
413412

414413
foreach (var mlp in _mlpBlocks)
415414
{
416-
var mlpParams = mlp.Parameters;
415+
var mlpParams = mlp.GetParameters();
417416
for (int i = 0; i < mlpParams.Length; i++)
418417
{
419418
allParams[offset + i] = mlpParams[i];
@@ -444,7 +443,7 @@ public override void SetParameters(Vector<T> parameters)
444443
if (mlp == null)
445444
throw new InvalidOperationException("MLP block is null");
446445

447-
totalParams += mlp.Parameters.Length;
446+
totalParams += mlp.ParameterCount;
448447
}
449448

450449
if (parameters.Length != totalParams)
@@ -458,7 +457,7 @@ public override void SetParameters(Vector<T> parameters)
458457
int offset = 0;
459458
foreach (var mlp in _mlpBlocks)
460459
{
461-
int mlpParamCount = mlp.Parameters.Length;
460+
int mlpParamCount = mlp.ParameterCount;
462461
var mlpParams = new Vector<T>(mlpParamCount);
463462

464463
for (int i = 0; i < mlpParamCount; i++)
@@ -499,7 +498,7 @@ public override Vector<T> GetParameterGradients()
499498
if (mlp == null)
500499
throw new InvalidOperationException("MLP block is null");
501500

502-
totalParams += mlp.Parameters.Length;
501+
totalParams += mlp.ParameterCount;
503502
}
504503

505504
// Concatenate all accumulated gradients

tests/AiDotNet.Tests/UnitTests/NestedLearning/ContinuumMemorySystemLayerTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void Forward_WithNullInput_ThrowsArgumentNullException()
173173
numFrequencyLevels: 3);
174174

175175
// Act & Assert
176-
Assert.Throws<ArgumentNullException>(() => layer.Forward(null!));
176+
Assert.Throws<ArgumentNullException>(() => layer.Forward((Tensor<double>)null!));
177177
}
178178

179179
[Fact]
@@ -202,7 +202,7 @@ public void Forward_ProcessesSequentiallyThroughAllMLPBlocks()
202202
foreach (var block in mlpBlocks)
203203
{
204204
Assert.NotNull(block);
205-
Assert.True(block.Parameters.Length > 0);
205+
Assert.True(block.ParameterCount > 0);
206206
}
207207
}
208208

@@ -302,7 +302,7 @@ public void ConsolidateMemory_TransfersKnowledgeBetweenLevels()
302302
foreach (var block in mlpBlocks)
303303
{
304304
Assert.NotNull(block);
305-
Assert.True(block.Parameters.Length > 0);
305+
Assert.True(block.ParameterCount > 0);
306306
}
307307
}
308308

0 commit comments

Comments
 (0)