getMapWithFloatValue method

Map<String, double> getMapWithFloatValue()

Gets a map that maps label to float. Only allow the mapping on the first axis with size greater than 1, and the axis should be effectively the last axis (which means every sub tensor specified by this axis should have a flat size of 1).

TensorLabel.getCategoryList is an alternative API to get the result.

Throws StateError if size of a sub tensor on each label is not 1.

Implementation

Map<String, double> getMapWithFloatValue() {
  int labeledAxis = getFirstAxisWithSizeGreaterThanOne(_tensorBuffer);
  SupportPreconditions.checkState(labeledAxis == _shape.length - 1,
      errorMessage:
          "get a <String, Scalar> map is only valid when the only labeled axis is the last one.");
  List<String> labels = _axisLabels[labeledAxis]!;
  List<double> data = _tensorBuffer.getDoubleList();
  SupportPreconditions.checkState(labels.length == data.length);
  Map<String, double> result = {};
  labels.asMap().forEach((i, label) {
    result[label] = data[i];
  });
  return result;
}