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
  W_f_tiers = []; W_i_tiers = []; W_c_tiers = []; W_o_tiers = [];
  b_f_tiers = []; b_i_tiers = []; b_c_tiers = []; b_o_tiers = [];

  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);
  }

  // Loop to build parameters for each tier
  for (int i = 0; i < numTiers; i++) {
    int combinedSize;
    if (i == 0) {
      // Lowest tier's input: [h_0, c_1, c_2, ..., x_t]
      combinedSize = hiddenSize + ((numTiers - 1) * hiddenSize) + inputSize;
    } else {
      // Higher tier's input: [h_i, h_{i-1}]
      combinedSize = hiddenSize + hiddenSize;
    }

    W_f_tiers.add(initWeights(combinedSize, hiddenSize));
    W_i_tiers.add(initWeights(combinedSize, hiddenSize));
    W_c_tiers.add(initWeights(combinedSize, hiddenSize));
    W_o_tiers.add(initWeights(combinedSize, hiddenSize));
    b_f_tiers.add(Tensor<Vector>(List<double>.filled(hiddenSize, 0.0)));
    b_i_tiers.add(Tensor<Vector>(List<double>.filled(hiddenSize, 0.0)));
    b_c_tiers.add(Tensor<Vector>(List<double>.filled(hiddenSize, 0.0)));
    b_o_tiers.add(Tensor<Vector>(List<double>.filled(hiddenSize, 0.0)));
  }

  super.build(input);
}