本文整理汇总了Python中tensorflow.matrix_band_part方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.matrix_band_part方法的具体用法?Python tensorflow.matrix_band_part怎么用?Python tensorflow.matrix_band_part使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.matrix_band_part方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ones_matrix_band_part
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_band_part [as 别名]
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
示例2: _ones_matrix_band_part
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_band_part [as 别名]
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
示例3: _GetMatrixBandPartTest
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_band_part [as 别名]
def _GetMatrixBandPartTest(dtype_, batch_shape_, shape_):
def Test(self):
mat = np.ones(shape_).astype(dtype_)
batch_mat = np.tile(mat, batch_shape + (1, 1))
with self.test_session(use_gpu=True):
for lower in -1, 0, 1, shape_[-2] - 1:
for upper in -1, 0, 1, shape_[-1] - 1:
band_np = mat
if lower >= 0:
band_np = np.triu(band_np, -lower)
if upper >= 0:
band_np = np.tril(band_np, upper)
if batch_shape is not ():
band_np = np.tile(band_np, batch_shape + (1, 1))
band = tf.matrix_band_part(batch_mat, lower, upper)
self.assertAllEqual(band_np, band.eval())
return Test
示例4: _build_predict_train
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_band_part [as 别名]
def _build_predict_train(self):
Kuf = self._Kuf
Kuu = [make_Kuu(kern, a, b, self.ms) for kern, a, b, in zip(self.kerns, self.a, self.b)]
KiKuf = [Kuu_d.solve(Kuf_d) for Kuu_d, Kuf_d in zip(Kuu, Kuf)]
KfuKi = [tf.transpose(mat) for mat in KiKuf]
mu = kvs_dot_vec(KfuKi, self.q_mu)
L = tf.matrix_band_part(self.q_sqrt, -1, 0)
tmp1 = kvs_dot_mat(KfuKi, L, num_cols=np.prod(self.Ms))
# Kff:
var = reduce(tf.multiply, [k.Kdiag(self.X[:, i:i+1]) for i, k in enumerate(self.kerns)])
# Projected variance Kfu Ki [WWT] Ki Kuf
# var = var + reduce(tf.multiply, [tf.reduce_sum(tf.square(tmp1_d), 0) for tmp1_d in tmp1])
var = var + tf.reduce_sum(tf.square(tmp1), 1)
# Qff
var = var - reduce(tf.multiply, [tf.reduce_sum(Kuf_d * KiKuf_d, 0) for Kuf_d, KiKuf_d in zip(Kuf, KiKuf)])
var = tf.reshape(var, (-1, 1))
return mu, var
示例5: get_decoder_self_attention_bias
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_band_part [as 别名]
def get_decoder_self_attention_bias(length, dtype=tf.float32):
"""Calculate bias for decoder that maintains model's autoregressive property.
Creates a tensor that masks out locations that correspond to illegal
connections, so prediction at position i cannot draw information from future
positions.
Args:
length: int length of sequences in batch.
Returns:
float tensor of shape [1, 1, length, length]
"""
#print("get_decoder_self_attention_bias", dtype)
with tf.name_scope("decoder_self_attention_bias"):
#valid_locs = tf.matrix_band_part(tf.ones([length, length], dtype=dtype), -1, 0)
valid_locs = tf.matrix_band_part(tf.ones([length, length], dtype=tf.float32), -1, 0)
valid_locs = tf.reshape(valid_locs, [1, 1, length, length])
neg_inf=_NEG_INF #if (dtype==tf.float32) else _NEG_INF_FP16
bias = neg_inf * (1.0 - valid_locs)
#bias=tf.saturate_cast(bias, dtype=dtype)
return bias
示例6: _ones_matrix_band_part
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_band_part [as 别名]
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
示例7: get_decoder_self_attention_bias
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_band_part [as 别名]
def get_decoder_self_attention_bias(length):
"""Calculate bias for decoder that maintains model's autoregressive property.
Creates a tensor that masks out locations that correspond to illegal
connections, so prediction at position i cannot draw information from future
positions.
Args:
length: int length of sequences in batch.
Returns:
float tensor of shape [1, 1, length, length]
"""
with tf.name_scope("decoder_self_attention_bias"):
valid_locs = tf.matrix_band_part(tf.ones([length, length]), -1, 0)
valid_locs = tf.reshape(valid_locs, [1, 1, length, length])
decoder_bias = _NEG_INF * (1.0 - valid_locs)
return decoder_bias
示例8: get_attention_mask
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_band_part [as 别名]
def get_attention_mask(nd, ns, *, dtype):
"""
this is a TPU compatible version of tf.matrix_band_part(tf.ones([nd, ns]), -1, ns-nd)
where the lower right triangle contains 1s
"""
i = tf.range(nd)[:, None]
j = tf.range(ns)
m = i >= j - ns + nd
return tf.cast(m, dtype)
示例9: build_variational
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_band_part [as 别名]
def build_variational(hps, kernel, z_pos, x, n_particles):
bn = zs.BayesianNet()
z_mean = tf.get_variable(
'z/mean', [hps.n_z], hps.dtype, tf.zeros_initializer())
z_cov_raw = tf.get_variable(
'z/cov_raw', initializer=tf.eye(hps.n_z, dtype=hps.dtype))
z_cov_tril = tf.matrix_set_diag(
tf.matrix_band_part(z_cov_raw, -1, 0),
tf.nn.softplus(tf.matrix_diag_part(z_cov_raw)))
fz = bn.multivariate_normal_cholesky(
'fz', z_mean, z_cov_tril, n_samples=n_particles)
bn.stochastic('fx', gp_conditional(z_pos, fz, x, False, kernel))
return bn
示例10: cumulative_max
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_band_part [as 别名]
def cumulative_max(x):
"""Takes the (inclusive) cumulative maximum along the last axis of x. (Not efficient.)"""
x = tf.convert_to_tensor(x)
with tf.name_scope('cumulative_max', values=[x]) as scope:
repeated = tf.tile(
tf.expand_dims(x, axis=-1),
tf.concat([tf.ones(x.shape.rank, dtype=tf.int32), tf.shape(x)[-1:]], axis=0))
trues = tf.ones_like(repeated, dtype=tf.bool)
upper_triangle = tf.matrix_band_part(trues, 0, -1)
neg_inf = tf.ones_like(repeated) * tf.dtypes.saturate_cast(-np.inf, dtype=x.dtype)
prefixes = tf.where(upper_triangle, repeated, neg_inf)
return tf.math.reduce_max(prefixes, axis=-2, name=scope)
示例11: attention_mask
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_band_part [as 别名]
def attention_mask(nd, ns, *, dtype):
"""1's in the lower triangle, counting from the lower right corner.
Same as tf.matrix_band_part(tf.ones([nd, ns]), -1, ns-nd), but doesn't produce garbage on TPUs.
"""
i = tf.range(nd)[:,None]
j = tf.range(ns)
m = i >= j - ns + nd
# to ignore first parts of context (useful for sampling with static shapes)
# m = tf.math.logical_and(m, tf.math.logical_or(j >= ignore, i < ignore - ns + nd))
return tf.cast(m, dtype)
示例12: test_attention_mask
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_band_part [as 别名]
def test_attention_mask():
with tf.Session() as sess:
for nd in 1, 2, 3:
for ns in range(nd, 4):
ours = model.attention_mask(nd, ns, dtype=tf.int32)
theirs = tf.matrix_band_part(tf.ones([nd, ns], dtype=tf.int32), tf.cast(-1, tf.int32), ns-nd)
ours, theirs = sess.run([ours, theirs])
print(ours)
print(theirs)
assert np.all(ours == theirs)
示例13: _create_mask
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_band_part [as 别名]
def _create_mask(qlen, mlen, dtype=tf.float32, same_length=False):
"""create causal attention mask."""
attn_mask = tf.ones([qlen, qlen], dtype=dtype)
mask_u = tf.matrix_band_part(attn_mask, 0, -1)
mask_dia = tf.matrix_band_part(attn_mask, 0, 0)
attn_mask_pad = tf.zeros([qlen, mlen], dtype=dtype)
ret = tf.concat([attn_mask_pad, mask_u - mask_dia], 1)
if same_length:
mask_l = tf.matrix_band_part(attn_mask, -1, 0)
ret = tf.concat([ret[:, :qlen] + mask_l - mask_dia, ret[:, qlen:]], 1)
return ret
示例14: ones_matrix_band_part
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_band_part [as 别名]
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
示例15: attention_mask
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import matrix_band_part [as 别名]
def attention_mask(nd, ns, dtype):
"""1's in the lower triangle, counting from the lower right corner.
Same as tf.matrix_band_part(tf.ones([nd, ns]), -1, ns-nd), but doesn't produce garbage on TPUs.
"""
i = tf.range(nd)[:,None]
j = tf.range(ns)
m = i >= j - ns + nd
return tf.cast(m, dtype)