Python numpy.tri() Examples
The following are 30 code examples for showing how to use numpy.tri(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example 1
Project: fine-lm Author: akzaidi File: common_layers.py License: MIT License | 6 votes |
def ones_matrix_band_part(rows, cols, num_lower, num_upper, out_shape=None): """Matrix band part of ones.""" if all([isinstance(el, int) for el in [rows, cols, num_lower, num_upper]]): # Needed info is constant, so we construct in numpy if num_lower < 0: num_lower = rows - 1 if num_upper < 0: num_upper = cols - 1 lower_mask = np.tri(cols, rows, num_lower).T upper_mask = np.tri(rows, cols, num_upper) band = np.ones((rows, cols)) * lower_mask * upper_mask if out_shape: band = band.reshape(out_shape) band = tf.constant(band, tf.float32) else: band = tf.matrix_band_part( tf.ones([rows, cols]), tf.cast(num_lower, tf.int64), tf.cast(num_upper, tf.int64)) if out_shape: band = tf.reshape(band, out_shape) return band
Example 2
Project: Counterfactual-StoryRW Author: qkaren File: transformer_attentions.py License: MIT License | 6 votes |
def _ones_matrix_band_part(rows, cols, num_lower, num_upper, out_shape=None): """Matrix band part of ones. """ if all([isinstance(el, int) for el in [rows, cols, num_lower, num_upper]]): # Needed info is constant, so we construct in numpy if num_lower < 0: num_lower = rows - 1 if num_upper < 0: num_upper = cols - 1 lower_mask = np.tri(cols, rows, num_lower).T upper_mask = np.tri(rows, cols, num_upper) band = np.ones((rows, cols)) * lower_mask * upper_mask if out_shape: band = band.reshape(out_shape) band = tf.constant(band, tf.float32) else: band = tf.matrix_band_part(tf.ones([rows, cols]), tf.cast(num_lower, tf.int64), tf.cast(num_upper, tf.int64)) if out_shape: band = tf.reshape(band, out_shape) return band
Example 3
Project: knmt Author: fabiencro File: utils.py License: GNU General Public License v3.0 | 6 votes |
def make_batch_mask(mb_size, n_head, max_length_1, max_length_2, key_seq_lengths=None, future_mask=False, mask_value=-10000): if future_mask: assert max_length_1 == max_length_2 mask = np.array( np.broadcast_to(( (-mask_value) * (np.tri(max_length_1, dtype = np.float32)-1))[None,None,:,:], (mb_size, n_head, max_length_1, max_length_2)) ) else: mask = np.zeros((mb_size, n_head, max_length_1, max_length_2), dtype = np.float32) if key_seq_lengths is not None: assert mb_size == len(key_seq_lengths) assert min(key_seq_lengths) > 0 assert max(key_seq_lengths) <= max_length_2 for num_batch, length in enumerate(key_seq_lengths): mask[num_batch, :, :, length:] = mask_value return mask
Example 4
Project: trax Author: google File: array_ops.py License: Apache License 2.0 | 6 votes |
def tri(N, M=None, k=0, dtype=None): # pylint: disable=invalid-name,missing-docstring M = M if M is not None else N if dtype is not None: dtype = utils.result_type(dtype) else: dtype = dtypes.default_float_type() if k < 0: lower = -k - 1 if lower > N: r = tf.zeros([N, M], dtype) else: # Keep as tf bool, since we create an upper triangular matrix and invert # it. o = tf.ones([N, M], dtype=tf.bool) r = tf.cast(tf.math.logical_not(tf.linalg.band_part(o, lower, -1)), dtype) else: o = tf.ones([N, M], dtype) if k > M: r = o else: r = tf.linalg.band_part(o, -1, k) return utils.tensor_to_ndarray(r)
Example 5
Project: trax Author: google File: array_ops.py License: Apache License 2.0 | 6 votes |
def tril(m, k=0): # pylint: disable=missing-docstring m = asarray(m).data m_shape = m.shape.as_list() if len(m_shape) < 2: raise ValueError('Argument to tril must have rank at least 2') if m_shape[-1] is None or m_shape[-2] is None: raise ValueError('Currently, the last two dimensions of the input array ' 'need to be known.') z = tf.constant(0, m.dtype) mask = tri(*m_shape[-2:], k=k, dtype=bool) return utils.tensor_to_ndarray( tf.where(tf.broadcast_to(mask, tf.shape(m)), m, z))
Example 6
Project: trax Author: google File: array_ops.py License: Apache License 2.0 | 6 votes |
def triu(m, k=0): # pylint: disable=missing-docstring m = asarray(m).data m_shape = m.shape.as_list() if len(m_shape) < 2: raise ValueError('Argument to triu must have rank at least 2') if m_shape[-1] is None or m_shape[-2] is None: raise ValueError('Currently, the last two dimensions of the input array ' 'need to be known.') z = tf.constant(0, m.dtype) mask = tri(*m_shape[-2:], k=k - 1, dtype=bool) return utils.tensor_to_ndarray( tf.where(tf.broadcast_to(mask, tf.shape(m)), z, m))
Example 7
Project: cupy Author: cupy File: matrix.py License: MIT License | 6 votes |
def tri(N, M=None, k=0, dtype=float): """Creates an array with ones at and below the given diagonal. Args: N (int): Number of rows. M (int): Number of columns. M == N by default. k (int): The sub-diagonal at and below which the array is filled. Zero is the main diagonal, a positive value is above it, and a negative value is below. dtype: Data type specifier. Returns: cupy.ndarray: An array with ones at and below the given diagonal. .. seealso:: :func:`numpy.tri` """ if M is None: M = N out = cupy.empty((N, M), dtype=dtype) return _tri_kernel(M, k, out)
Example 8
Project: cupy Author: cupy File: matrix.py License: MIT License | 6 votes |
def tril(m, k=0): """Returns a lower triangle of an array. Args: m (array-like): Array or array-like object. k (int): The diagonal above which to zero elements. Zero is the main diagonal, a positive value is above it, and a negative value is below. Returns: cupy.ndarray: A lower triangle of an array. .. seealso:: :func:`numpy.tril` """ m = cupy.asarray(m) mask = tri(*m.shape[-2:], k=k, dtype=bool) return cupy.where(mask, m, m.dtype.type(0))
Example 9
Project: texar-pytorch Author: asyml File: transformer_attentions.py License: Apache License 2.0 | 6 votes |
def _ones_matrix_band_part(rows: int, cols: int, num_lower: int, num_upper: int, out_shape: Optional[Tuple[int, ...]] = None) \ -> torch.Tensor: r"""Matrix band part of ones. """ if num_lower < 0: num_lower = rows - 1 if num_upper < 0: num_upper = cols - 1 lower_mask = np.tri(cols, rows, num_lower).T upper_mask = np.tri(rows, cols, num_upper) band = np.ones((rows, cols)) * lower_mask * upper_mask if out_shape: band = band.reshape(out_shape) band = torch.as_tensor(band, dtype=torch.float32) return band
Example 10
Project: texar Author: asyml File: transformer_attentions.py License: Apache License 2.0 | 6 votes |
def _ones_matrix_band_part(rows, cols, num_lower, num_upper, out_shape=None): """Matrix band part of ones. """ if all([isinstance(el, int) for el in [rows, cols, num_lower, num_upper]]): # Needed info is constant, so we construct in numpy if num_lower < 0: num_lower = rows - 1 if num_upper < 0: num_upper = cols - 1 lower_mask = np.tri(cols, rows, num_lower).T upper_mask = np.tri(rows, cols, num_upper) band = np.ones((rows, cols)) * lower_mask * upper_mask if out_shape: band = band.reshape(out_shape) band = tf.constant(band, tf.float32) else: band = tf.matrix_band_part(tf.ones([rows, cols]), tf.cast(num_lower, tf.int64), tf.cast(num_upper, tf.int64)) if out_shape: band = tf.reshape(band, out_shape) return band
Example 11
Project: recruit Author: Frank-qlu File: test_ufunc.py License: Apache License 2.0 | 5 votes |
def test_euclidean_pdist(self): a = np.arange(12, dtype=float).reshape(4, 3) out = np.empty((a.shape[0] * (a.shape[0] - 1) // 2,), dtype=a.dtype) umt.euclidean_pdist(a, out) b = np.sqrt(np.sum((a[:, None] - a)**2, axis=-1)) b = b[~np.tri(a.shape[0], dtype=bool)] assert_almost_equal(out, b) # An output array is required to determine p with signature (n,d)->(p) assert_raises(ValueError, umt.euclidean_pdist, a)
Example 12
Project: AIX360 Author: IBM File: feature_binarizer_from_trees_demo.py License: Apache License 2.0 | 5 votes |
def get_corr_columns(df: pd.DataFrame, threshold: float = 0.98) -> list: c = df.corr().abs() c: pd.DataFrame = c * np.tri(c.shape[0], c.shape[1], -1) c = c.transpose() return [col for col in c.columns if any(c[col] > threshold)]
Example 13
Project: auto-alt-text-lambda-api Author: abhisuri97 File: test_ufunc.py License: MIT License | 5 votes |
def test_euclidean_pdist(self): a = np.arange(12, dtype=np.float).reshape(4, 3) out = np.empty((a.shape[0] * (a.shape[0] - 1) // 2,), dtype=a.dtype) umt.euclidean_pdist(a, out) b = np.sqrt(np.sum((a[:, None] - a)**2, axis=-1)) b = b[~np.tri(a.shape[0], dtype=bool)] assert_almost_equal(out, b) # An output array is required to determine p with signature (n,d)->(p) assert_raises(ValueError, umt.euclidean_pdist, a)
Example 14
Project: tensor2tensor Author: tensorflow File: common_layers.py License: Apache License 2.0 | 5 votes |
def ones_matrix_band_part(rows, cols, num_lower, num_upper, out_shape=None): """Matrix band part of ones. Args: rows: int determining number of rows in output cols: int num_lower: int, maximum distance backward. Negative values indicate unlimited. num_upper: int, maximum distance forward. Negative values indicate unlimited. out_shape: shape to reshape output by. Returns: Tensor of size rows * cols reshaped into shape out_shape. """ if all([isinstance(el, int) for el in [rows, cols, num_lower, num_upper]]): # Needed info is constant, so we construct in numpy if num_lower < 0: num_lower = rows - 1 if num_upper < 0: num_upper = cols - 1 lower_mask = np.tri(cols, rows, num_lower).T upper_mask = np.tri(rows, cols, num_upper) band = np.ones((rows, cols)) * lower_mask * upper_mask if out_shape: band = band.reshape(out_shape) band = tf.constant(band, tf.float32) else: band = tf.linalg.band_part( tf.ones([rows, cols]), tf.cast(num_lower, tf.int64), tf.cast(num_upper, tf.int64)) if out_shape: band = tf.reshape(band, out_shape) return band
Example 15
Project: vnpy_crypto Author: birforce File: test_ufunc.py License: MIT License | 5 votes |
def test_euclidean_pdist(self): a = np.arange(12, dtype=float).reshape(4, 3) out = np.empty((a.shape[0] * (a.shape[0] - 1) // 2,), dtype=a.dtype) umt.euclidean_pdist(a, out) b = np.sqrt(np.sum((a[:, None] - a)**2, axis=-1)) b = b[~np.tri(a.shape[0], dtype=bool)] assert_almost_equal(out, b) # An output array is required to determine p with signature (n,d)->(p) assert_raises(ValueError, umt.euclidean_pdist, a)
Example 16
Project: BERT Author: yyht File: common_layers.py License: Apache License 2.0 | 5 votes |
def ones_matrix_band_part(rows, cols, num_lower, num_upper, out_shape=None): """Matrix band part of ones. Args: rows: int determining number of rows in output cols: int num_lower: int, maximum distance backward. Negative values indicate unlimited. num_upper: int, maximum distance forward. Negative values indicate unlimited. out_shape: shape to reshape output by. Returns: Tensor of size rows * cols reshaped into shape out_shape. """ if all([isinstance(el, int) for el in [rows, cols, num_lower, num_upper]]): # Needed info is constant, so we construct in numpy if num_lower < 0: num_lower = rows - 1 if num_upper < 0: num_upper = cols - 1 lower_mask = np.tri(cols, rows, num_lower).T upper_mask = np.tri(rows, cols, num_upper) band = np.ones((rows, cols)) * lower_mask * upper_mask if out_shape: band = band.reshape(out_shape) band = tf.constant(band, tf.float32) else: band = tf.matrix_band_part( tf.ones([rows, cols]), tf.cast(num_lower, tf.int64), tf.cast(num_upper, tf.int64)) if out_shape: band = tf.reshape(band, out_shape) return band
Example 17
Project: Mastering-Elasticsearch-7.0 Author: PacktPublishing File: test_ufunc.py License: MIT License | 5 votes |
def test_euclidean_pdist(self): a = np.arange(12, dtype=float).reshape(4, 3) out = np.empty((a.shape[0] * (a.shape[0] - 1) // 2,), dtype=a.dtype) umt.euclidean_pdist(a, out) b = np.sqrt(np.sum((a[:, None] - a)**2, axis=-1)) b = b[~np.tri(a.shape[0], dtype=bool)] assert_almost_equal(out, b) # An output array is required to determine p with signature (n,d)->(p) assert_raises(ValueError, umt.euclidean_pdist, a)
Example 18
Project: trax Author: google File: lax_numpy_test.py License: Apache License 2.0 | 5 votes |
def testTri(self, m, n, k, dtype, rng_factory): rng = rng_factory() onp_fun = lambda: onp.tri(n, M=m, k=k, dtype=dtype) lnp_fun = lambda: lnp.tri(n, M=m, k=k, dtype=dtype) args_maker = lambda: [] self._CheckAgainstNumpy(onp_fun, lnp_fun, args_maker, check_dtypes=True) self._CompileAndCheck( lnp_fun, args_maker, check_dtypes=True, check_incomplete_shape=True)
Example 19
Project: training_results_v0.5 Author: mlperf File: common_layers.py License: Apache License 2.0 | 5 votes |
def ones_matrix_band_part(rows, cols, num_lower, num_upper, out_shape=None): """Matrix band part of ones. Args: rows: int determining number of rows in output cols: int num_lower: int, maximum distance backward. Negative values indicate unlimited. num_upper: int, maximum distance forward. Negative values indicate unlimited. out_shape: shape to reshape output by. Returns: Tensor of size rows * cols reshaped into shape out_shape. """ if all([isinstance(el, int) for el in [rows, cols, num_lower, num_upper]]): # Needed info is constant, so we construct in numpy if num_lower < 0: num_lower = rows - 1 if num_upper < 0: num_upper = cols - 1 lower_mask = np.tri(cols, rows, num_lower).T upper_mask = np.tri(rows, cols, num_upper) band = np.ones((rows, cols)) * lower_mask * upper_mask if out_shape: band = band.reshape(out_shape) band = tf.constant(band, tf.float32) else: band = tf.matrix_band_part( tf.ones([rows, cols]), tf.cast(num_lower, tf.int64), tf.cast(num_upper, tf.int64)) if out_shape: band = tf.reshape(band, out_shape) return band
Example 20
Project: GraphicDesignPatternByPython Author: Relph1119 File: test_ufunc.py License: MIT License | 5 votes |
def test_euclidean_pdist(self): a = np.arange(12, dtype=float).reshape(4, 3) out = np.empty((a.shape[0] * (a.shape[0] - 1) // 2,), dtype=a.dtype) umt.euclidean_pdist(a, out) b = np.sqrt(np.sum((a[:, None] - a)**2, axis=-1)) b = b[~np.tri(a.shape[0], dtype=bool)] assert_almost_equal(out, b) # An output array is required to determine p with signature (n,d)->(p) assert_raises(ValueError, umt.euclidean_pdist, a)
Example 21
Project: GraphicDesignPatternByPython Author: Relph1119 File: test_decomp_update.py License: MIT License | 5 votes |
def assert_upper_tri(a, rtol=None, atol=None): if rtol is None: rtol = 10.0 ** -(np.finfo(a.dtype).precision-2) if atol is None: atol = 2*np.finfo(a.dtype).eps mask = np.tri(a.shape[0], a.shape[1], -1, np.bool_) assert_allclose(a[mask], 0.0, rtol=rtol, atol=atol)
Example 22
Project: deep_image_model Author: tobegit3hub File: distribution_util_test.py License: Apache License 2.0 | 5 votes |
def testCorrectlyMakesNoBatchLowerTril(self): with self.test_session(): x = tf.convert_to_tensor(self._rng.randn(10)) expected = self._fill_lower_triangular(tensor_util.constant_value(x)) actual = distribution_util.fill_lower_triangular(x, validate_args=True) self.assertAllEqual(expected.shape, actual.get_shape()) self.assertAllEqual(expected, actual.eval()) g = tf.gradients(distribution_util.fill_lower_triangular(x), x) self.assertAllEqual(np.tri(4).reshape(-1), g[0].values.eval())
Example 23
Project: predictive-maintenance-using-machine-learning Author: awslabs File: test_ufunc.py License: Apache License 2.0 | 5 votes |
def test_euclidean_pdist(self): a = np.arange(12, dtype=float).reshape(4, 3) out = np.empty((a.shape[0] * (a.shape[0] - 1) // 2,), dtype=a.dtype) umt.euclidean_pdist(a, out) b = np.sqrt(np.sum((a[:, None] - a)**2, axis=-1)) b = b[~np.tri(a.shape[0], dtype=bool)] assert_almost_equal(out, b) # An output array is required to determine p with signature (n,d)->(p) assert_raises(ValueError, umt.euclidean_pdist, a)
Example 24
Project: cupy Author: cupy File: matrix.py License: MIT License | 5 votes |
def triu(m, k=0): """Returns an upper triangle of an array. Args: m (array-like): Array or array-like object. k (int): The diagonal below which to zero elements. Zero is the main diagonal, a positive value is above it, and a negative value is below. Returns: cupy.ndarray: An upper triangle of an array. .. seealso:: :func:`numpy.triu` """ m = cupy.asarray(m) mask = tri(*m.shape[-2:], k=k-1, dtype=bool) return cupy.where(mask, m.dtype.type(0), m) # TODO(okuta): Implement vander # TODO(okuta): Implement mat # TODO(okuta): Implement bmat
Example 25
Project: pySINDy Author: luckystarufo File: test_ufunc.py License: MIT License | 5 votes |
def test_euclidean_pdist(self): a = np.arange(12, dtype=float).reshape(4, 3) out = np.empty((a.shape[0] * (a.shape[0] - 1) // 2,), dtype=a.dtype) umt.euclidean_pdist(a, out) b = np.sqrt(np.sum((a[:, None] - a)**2, axis=-1)) b = b[~np.tri(a.shape[0], dtype=bool)] assert_almost_equal(out, b) # An output array is required to determine p with signature (n,d)->(p) assert_raises(ValueError, umt.euclidean_pdist, a)
Example 26
Project: PDP-Solver Author: microsoft File: dimacs2json.py License: MIT License | 5 votes |
def _propagate_constraints(self, clause_mat): n = clause_mat.shape[0] if n < 2: return clause_mat length = np.tile(np.sum(np.abs(clause_mat), 1), (n, 1)) intersection_len = np.matmul(clause_mat, np.transpose(clause_mat)) temp = intersection_len == np.transpose(length) temp *= np.tri(*temp.shape, k=-1, dtype=bool) flags = np.logical_not(np.any(temp, 0)) clause_mat = clause_mat[flags, :] n = clause_mat.shape[0] if n < 2: return clause_mat length = np.tile(np.sum(np.abs(clause_mat), 1), (n, 1)) intersection_len = np.matmul(clause_mat, np.transpose(clause_mat)) temp = intersection_len == length temp *= np.tri(*temp.shape, k=-1, dtype=bool) flags = np.logical_not(np.any(temp, 1)) return clause_mat[flags, :]
Example 27
Project: mxnet-lambda Author: awslabs File: test_ufunc.py License: Apache License 2.0 | 5 votes |
def test_euclidean_pdist(self): a = np.arange(12, dtype=np.float).reshape(4, 3) out = np.empty((a.shape[0] * (a.shape[0] - 1) // 2,), dtype=a.dtype) umt.euclidean_pdist(a, out) b = np.sqrt(np.sum((a[:, None] - a)**2, axis=-1)) b = b[~np.tri(a.shape[0], dtype=bool)] assert_almost_equal(out, b) # An output array is required to determine p with signature (n,d)->(p) assert_raises(ValueError, umt.euclidean_pdist, a)
Example 28
Project: BERT_TF Author: KnightZhang625 File: model_helper.py License: Apache License 2.0 | 5 votes |
def create_lr_mask(batch_size, input_length): """as input length changes, could not use placeholder to create triangle matrix in tensorflow, cause tensorflow does not support create triangle matrix. """ # triangle_matrix = np.tile( # np.tri(input_length, input_length, 0, dtype=np.float), # [batch_size, 1, 1]) # mask = tf.cast(triangle_matrix, dtype=tf.float32) triangle_matrix = np.tri(input_length, input_length, 0, dtype=np.float) return triangle_matrix
Example 29
Project: factor_analyzer Author: EducationalTestingService File: utils.py License: GNU General Public License v2.0 | 5 votes |
def fill_lower_diag(x): """ Fill the lower diagonal of a square matrix, given a 1d input array. Parameters ---------- x : array-like The flattened input matrix that will be used to fill the lower diagonal of the square matrix. Returns ------- out : numpy array The output square matrix, with the lower diagonal filled by x. Reference --------- [1] https://stackoverflow.com/questions/51439271/ convert-1d-array-to-lower-triangular-matrix """ x = np.array(x) x = x if len(x.shape) == 1 else np.squeeze(x, axis=1) n = int(np.sqrt(len(x) * 2)) + 1 out = np.zeros((n, n), dtype=float) out[np.tri(n, dtype=bool, k=-1)] = x return out
Example 30
Project: elasticintel Author: securityclippy File: test_ufunc.py License: GNU General Public License v3.0 | 5 votes |
def test_euclidean_pdist(self): a = np.arange(12, dtype=np.float).reshape(4, 3) out = np.empty((a.shape[0] * (a.shape[0] - 1) // 2,), dtype=a.dtype) umt.euclidean_pdist(a, out) b = np.sqrt(np.sum((a[:, None] - a)**2, axis=-1)) b = b[~np.tri(a.shape[0], dtype=bool)] assert_almost_equal(out, b) # An output array is required to determine p with signature (n,d)->(p) assert_raises(ValueError, umt.euclidean_pdist, a)