build method

  1. @override
void build(
  1. Tensor input
)
override

Initializes the layer's parameters based on the shape of the first input.

Subclasses should override this method to create their weights and biases. This method is called automatically by call and should not be called directly.

Implementation

@override
void build(Tensor<dynamic> input) {
  Matrix inputMatrix = input.value as Matrix;
  int inputSize = inputMatrix.isNotEmpty ? inputMatrix[0].length : 0;
  Random random = Random();

  // Initialize parameter lists
  lstmWf = []; lstmWi = []; lstmWc = []; lstmWo = [];
  lstmBf = []; lstmBi = []; lstmBc = []; lstmBo = [];
  aggW = []; aggB = [];

  Tensor<Matrix> initWeights(int fanIn, int fanOut) {
    double stddev = sqrt(1.0 / fanIn);
    Matrix values = [];
    for (int i = 0; i < fanOut; i++) {
      Vector row = [];
      for (int j = 0; j < fanIn; j++) {
        row.add((random.nextDouble() * 2 - 1) * stddev);
      }
      values.add(row);
    }
    return Tensor<Matrix>(values);
  }

  // --- Initialize Aggregation Weights ---
  int currentAggInputSize = inputSize;
  for (int grainSize in grainingSizes) {
    // Input is `grainSize` vectors of size `currentAggInputSize`
    int fanIn = grainSize * currentAggInputSize;
    // Output is a single summary vector of the same size `currentAggInputSize`
    aggW.add(initWeights(fanIn, currentAggInputSize));
    aggB.add(Tensor<Vector>(List<double>.filled(currentAggInputSize, 0.0)));
  }

  // --- Initialize LSTM Weights for Each Tier ---
  for (int i = 0; i < numTiers; i++) {
    int lstmInputFeatureSize = (i == 0) ? inputSize : inputSize;
    int lstmCombinedSize = hiddenSize + lstmInputFeatureSize;
    lstmWf.add(initWeights(lstmCombinedSize, hiddenSize));
    lstmWi.add(initWeights(lstmCombinedSize, hiddenSize));
    lstmWc.add(initWeights(lstmCombinedSize, hiddenSize));
    lstmWo.add(initWeights(lstmCombinedSize, hiddenSize));
    lstmBf.add(Tensor<Vector>(List<double>.filled(hiddenSize, 0.0)));
    lstmBi.add(Tensor<Vector>(List<double>.filled(hiddenSize, 0.0)));
    lstmBc.add(Tensor<Vector>(List<double>.filled(hiddenSize, 0.0)));
    lstmBo.add(Tensor<Vector>(List<double>.filled(hiddenSize, 0.0)));
  }

  super.build(input);
}