def _bin_op(tf_fun, a, b, promote=True): if promote: a, b = array_ops._promote_dtype(a, b) # pylint: disable=protected-access else: a = array_ops.array(a) b = array_ops.array(b) return utils.tensor_to_ndarray(tf_fun(a.data, b.data))
def kron(a, b): # pylint: disable=protected-access,g-complex-comprehension a, b = array_ops._promote_dtype(a, b) ndim = max(a.ndim, b.ndim) if a.ndim < ndim: a = array_ops.reshape(a, array_ops._pad_left_to(ndim, a.shape)) if b.ndim < ndim: b = array_ops.reshape(b, array_ops._pad_left_to(ndim, b.shape)) a_reshaped = array_ops.reshape(a, [i for d in a.shape for i in (d, 1)]) b_reshaped = array_ops.reshape(b, [i for d in b.shape for i in (1, d)]) out_shape = tuple(np.multiply(a.shape, b.shape)) return array_ops.reshape(a_reshaped * b_reshaped, out_shape)
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_ops._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)))