Layer<N extends num, E, T extends Signal<N, E, T>, S extends Scale<N>>.fromJson constructor

Layer<N extends num, E, T extends Signal<N, E, T>, S extends Scale<N>>.fromJson(
  1. dynamic json
)

Implementation

factory Layer.fromJson(dynamic json) {
  Map<String, dynamic> jsonMap = json is String ? parseJSON(json) : json;

  var format = jsonMap['format']! as String;
  var layerType = jsonMap['type']! as String;
  var neuronsSize = jsonMap['neurons']! as int;
  var bias = (jsonMap['bias'] ?? false) as bool;
  var activation = jsonMap['activation']! as Map<String, dynamic>;
  var weightsValues = (jsonMap['weights'] as List?)?.cast<List>();

  var activationFunction = ActivationFunction.fromJson(activation);

  switch (format) {
    case 'Float32x4':
      {
        var neurons = Signal.fromFormat<double, Float32x4, SignalFloat32x4>(
            format,
            size: neuronsSize) as SignalFloat32x4;

        var weights = weightsValues
            ?.map((e) =>
                Signal.fromFormat<double, Float32x4, SignalFloat32x4>(format,
                    values: e.asDoubles()) as SignalFloat32x4)
            .toList();

        var af = activationFunction as ActivationFunction<double, Float32x4>;

        return Layer<double, Float32x4, SignalFloat32x4,
                Scale<double>>._byType(layerType, neurons, bias, af, weights)
            as Layer<N, E, T, S>;
      }
    case 'Int32x4':
      {
        var neurons = Signal.fromFormat<int, Int32x4, SignalInt32x4>(format,
            size: neuronsSize) as SignalInt32x4;

        var weights = weightsValues
            ?.map((e) => Signal.fromFormat<int, Int32x4, SignalInt32x4>(
                format,
                values: e.asInts()) as SignalInt32x4)
            .toList();

        var af = activationFunction as ActivationFunction<int, Int32x4>;

        return Layer<int, Int32x4, SignalInt32x4, Scale<int>>._byType(
            layerType, neurons, bias, af, weights) as Layer<N, E, T, S>;
      }
    case 'Float32x4Mod4':
      {
        var neurons = Signal.fromFormat<double, Float32x4, SignalFloat32x4>(
            format,
            size: neuronsSize) as SignalFloat32x4Mod4;

        var weights = weightsValues
            ?.map((e) =>
                Signal.fromFormat<double, Float32x4, SignalFloat32x4>(format,
                    values: e.asDoubles()) as SignalFloat32x4Mod4)
            .toList();

        var af = activationFunction as ActivationFunction<double, Float32x4>;

        return Layer<double, Float32x4, SignalFloat32x4,
                Scale<double>>._byType(layerType, neurons, bias, af, weights)
            as Layer<N, E, T, S>;
      }
    default:
      throw StateError('Unknown format: $format');
  }
}