Beispiel #1
0
def get_reduction_of_data(aggregation_name, tensor_data, tensor_name, abs=False):
    reduction_name = aggregation_name
    # If tensor_data is of np.ndarray type invoke np operators.
    if isinstance(tensor_data, np.ndarray):
        return get_numpy_reduction(reduction_name, tensor_data, abs)
    if abs:
        tensor_data = mx.ndarray.abs(tensor_data)

    if reduction_name in ALLOWED_REDUCTIONS:
        if hasattr(mx.ndarray, aggregation_name):
            f = getattr(mx.ndarray, aggregation_name)
            op = f(tensor_data, name=tensor_name)
        else:
            # If aggregation is not supported by mxnet, we convert data into np.ndarray and invoke numpy operators
            tensor_data_np = make_numpy_array(tensor_data)
            op = get_numpy_reduction(aggregation_name, numpy_data=tensor_data_np, abs=abs)
        return op
    elif reduction_name in ALLOWED_NORMS:
        if reduction_name in ["l1", "l2"]:
            op = mx.ndarray.norm(data=tensor_data, ord=int(reduction_name[1]))
            return op
        else:
            raise RuntimeError(
                "Invalid normalization operation {0} for mx.NDArray".format(reduction_name)
            )
    elif hasattr(mx, reduction_name):
        f = getattr(mx, reduction_name)
        op = f(tensor_data, name=tensor_name)
        return op
    elif hasattr(np, aggregation_name):
        f = getattr(np, aggregation_name)
        op = f(tensor_data)
        return op
    raise RuntimeError("Invalid aggregation_name {0} for mx.NDArray".format(aggregation_name))
Beispiel #2
0
def histogram_summary(tag, values, bins, max_bins=None):
    """Outputs a `Summary` protocol buffer with a histogram.
    Adding a histogram summary makes it possible to visualize the data's distribution in
    TensorBoard. See detailed explanation of the TensorBoard histogram dashboard at
    https://www.tensorflow.org/get_started/tensorboard_histograms
    This op reports an `InvalidArgument` error if any value is not finite.
    Adapted from the TensorFlow function `histogram()` at
    https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/summary/summary.py
    and Pytorch's function
    https://github.com/pytorch/pytorch/blob/2655b2710c8f0b3253fe2cfe0d2674f5d3592d22/torch/utils/tensorboard/summary.py#L232
    Parameters
    ----------
        tag : str
            A name for the summary of the histogram. Will also serve as a series name in
            TensorBoard.
        values : `numpy.ndarray`
            Values for building the histogram.
    Returns
    -------
        A `Summary` protobuf of the histogram.
    """
    tag = _clean_tag(tag)
    values = make_numpy_array(values)
    hist = _make_histogram(values.astype(float), bins, max_bins)
    return Summary(value=[Summary.Value(tag=tag, histo=hist)])
Beispiel #3
0
def test_make_numpy_array():
    simple_numpy_array = np.ndarray(shape=(2, 2), dtype=float, order="F")

    # Check support for ndarray
    try:
        x = make_numpy_array(simple_numpy_array)
        assert x.all() == simple_numpy_array.all()
    except:
        assert False

    # Check support for scalar
    simple_scalar = "foo"
    try:
        x = make_numpy_array(simple_scalar)
        assert x == np.array([simple_scalar])
    except:
        assert False

    # Check support for tuple
    simple_tuple = (0.5, 0.7)
    try:
        x = make_numpy_array(simple_tuple)
        assert x.all() == np.array(simple_tuple).all()
    except:
        assert False

    # Check support for list
    simple_list = [0.5, 0.7]
    try:
        x = make_numpy_array(simple_list)
        assert x.all() == np.array(simple_list).all()
    except:
        assert False

    # Check support for dict
    simple_dict = {"a": 0.5, "b": 0.7}
    try:
        x = make_numpy_array(simple_dict)
        assert x == np.array(simple_dict)
    except:
        assert False
 def _make_numpy_array(tensor_value):
     """
     Convert the tensor value into a numpy array.
     Here it's already numpy array
     """
     if is_tf_version_2x() and tf.executing_eagerly():
         if (isinstance(tensor_value, tf.Variable)
                 or isinstance(tensor_value, tf.Tensor)) and hasattr(
                     tensor_value, "numpy"):
             # TF 2.X eager mode
             return tensor_value.numpy()
     return make_numpy_array(tensor_value)
Beispiel #5
0
def scalar_summary(tag, scalar):
    """Outputs a `Summary` protocol buffer containing a single scalar value.
    The generated Summary has a Tensor.proto containing the input Tensor.
    Adapted from the TensorFlow function `scalar()` at
    https://github.com/tensorflow/tensorflow/blob/r1.6/tensorflow/python/summary/summary.py
    Parameters
    ----------
      tag : str
          A name for the generated summary. Will also serve as the series name in TensorBoard.
      scalar : int, MXNet `NDArray`, or `numpy.ndarray`
          A scalar value or an ndarray of shape (1,).
    Returns
    -------
      A `Summary` protobuf of the `scalar` value.
    Raises
    ------
      ValueError: If the scalar has the wrong shape or type.
    """
    tag = _clean_tag(tag)
    scalar = make_numpy_array(scalar)
    assert scalar.squeeze().ndim == 0, "scalar should be 0D"
    scalar = float(scalar)
    return Summary(value=[Summary.Value(tag=tag, simple_value=scalar)])
Beispiel #6
0
 def _make_numpy_array(tensor_value):
     """
     Convert the tensor value into a numpy array.
     Here it's already numpy array
     """
     return make_numpy_array(tensor_value)
Beispiel #7
0
 def _make_numpy_array(tensor_value):
     if isinstance(tensor_value, torch.Tensor):
         return tensor_value.to(torch.device("cpu")).data.numpy()
     return make_numpy_array(tensor_value)
Beispiel #8
0
 def _make_numpy_array(tensor_value):
     return make_numpy_array(tensor_value)