Exemple #1
0
def _bin_op(tf_fun, a, b, promote=True):
    if promote:
        a, b = array_creation._promote_dtype(a, b)
    else:
        a = array_creation.asarray(a)
        b = array_creation.asarray(b)
    return utils.tensor_to_ndarray(tf_fun(a.data, b.data))
Exemple #2
0
def dstack(tup):
    arrays = [math_lib.atleast_3d(a) for a in tup]
    arrays = array_creation._promote_dtype(*arrays)  # pylint: disable=protected-access
    unwrapped_arrays = [
        a.data if isinstance(a, arrays_lib.ndarray) else a for a in arrays
    ]
    return tf.concat(unwrapped_arrays, axis=2)
Exemple #3
0
def hstack(tup):
    arrays = [math_lib.atleast_1d(a) for a in tup]
    arrays = array_creation._promote_dtype(*arrays)  # pylint: disable=protected-access
    unwrapped_arrays = [
        a.data if isinstance(a, arrays_lib.ndarray) else a for a in arrays
    ]
    rank = tf.rank(unwrapped_arrays[0])
    return utils.cond(rank == 1, lambda: tf.concat(unwrapped_arrays, axis=0),
                      lambda: tf.concat(unwrapped_arrays, axis=1))
Exemple #4
0
def kron(a, b):
    a, b = array_creation._promote_dtype(a, b)
    ndim = max(a.ndim, b.ndim)
    if a.ndim < ndim:
        a = array_methods.reshape(a, _pad_left_to(ndim, a.shape))
    if b.ndim < ndim:
        b = array_methods.reshape(b, _pad_left_to(ndim, b.shape))
    a_reshaped = array_methods.reshape(a, [i for d in a.shape for i in (d, 1)])
    b_reshaped = array_methods.reshape(b, [i for d in b.shape for i in (1, d)])
    out_shape = tuple(np.multiply(a.shape, b.shape))
    return array_methods.reshape(a_reshaped * b_reshaped, out_shape)
Exemple #5
0
def clip(a, a_min, a_max):  # pylint: disable=missing-docstring
    if a_min is None and a_max is None:
        raise ValueError(
            'Not more than one of `a_min` and `a_max` may be `None`.')
    if a_min is None:
        return minimum(a, a_max)
    elif a_max is None:
        return maximum(a, a_min)
    else:
        a, a_min, a_max = array_creation._promote_dtype(a, a_min, a_max)  # pylint: disable=protected-access
        return utils.tensor_to_ndarray(
            tf.clip_by_value(
                *utils.tf_broadcast(a.data, a_min.data, a_max.data)))
Exemple #6
0
def where(condition, x, y):
  """Return an array with elements from `x` or `y`, depending on condition.

  Args:
    condition: array_like, bool. Where True, yield `x`, otherwise yield `y`.
    x: see below.
    y: array_like, optional. Values from which to choose. `x`, `y` and
      `condition` need to be broadcastable to some shape.

  Returns:
    An array.
  """
  condition = array_creation.asarray(condition, dtype=np.bool_)
  x, y = array_creation._promote_dtype(x, y)
  return utils.tensor_to_ndarray(tf.where(condition.data, x.data, y.data))
Exemple #7
0
def dot(a, b):
    """The dot product of two arrays. See numpy.dot for more details.

  This relies on `tf.tensordot` which does not support types int64 and float64.
  So arrays of those types are "unsafely" cast to int32 and float32.

  Args:
    a: array_like. Could be an ndarray, a Tensor or any object that can
      be converted to a Tensor using `tf.convert_to_tensor`.
    b: array_like. Could be an ndarray, a Tensor or any object that can
      be converted to a Tensor using `tf.convert_to_tensor`.

  Returns:
    An ndarray.
  """
    a, b = array_creation._promote_dtype(a, b)
    if utils.isscalar(a) or utils.isscalar(b):
        a = utils.tensor_to_ndarray(tf.expand_dims(a.data, -1))
        b = utils.tensor_to_ndarray(tf.expand_dims(b.data, -1))
        a_axis = b_axis = -1
    else:
        a_axis = -1
        # TODO(agarwal): handle ndim being None when in graph mode.
        b_axis = -2 if b.ndim > 1 else -1
    # TODO(srbs): When the shape of the output is a scalar e.g. when performing
    # a dot-product of two vectors, numpy returns a scalar object and not an
    # instance of ndarray.

    # tensordot/MatMul does not support int64 and float64 so we manually cast to
    # the compatible types. The conversion may be unsafe.
    # TODO(srbs): Figure out why MatMul does not support larger types.
    output_type = None
    if a.dtype == np.int64:
        logging.warning("Unsafe cast to int32.")
        a = utils.tensor_to_ndarray(tf.cast(a.data, tf.int32))
        b = utils.tensor_to_ndarray(tf.cast(b.data, tf.int32))
        output_type = tf.int64
    elif a.dtype == np.float64:
        logging.warning("Unsafe cast to float32.")
        a = utils.tensor_to_ndarray(tf.cast(a.data, tf.float32))
        b = utils.tensor_to_ndarray(tf.cast(b.data, tf.float32))
        output_type = tf.float64

    result_t = tf.tensordot(a.data, b.data, [[a_axis], [b_axis]])
    if output_type:
        result_t = tf.cast(result_t, output_type)
    return utils.tensor_to_ndarray(result_t)
Exemple #8
0
def stack(arrays, axis=0):
    arrays = array_creation._promote_dtype(*arrays)  # pylint: disable=protected-access
    unwrapped_arrays = [
        a.data if isinstance(a, arrays_lib.ndarray) else a for a in arrays
    ]
    return array_creation.asarray(tf.stack(unwrapped_arrays, axis))