本文整理汇总了Python中tensorflow.sparse_segment_sum方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.sparse_segment_sum方法的具体用法?Python tensorflow.sparse_segment_sum怎么用?Python tensorflow.sparse_segment_sum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.sparse_segment_sum方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_SparseSegmentSum
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_segment_sum [as 别名]
def test_SparseSegmentSum(self):
t = tf.sparse_segment_sum(self.random(4, 3, 2), [0, 2, 3], [0, 1, 1])
self.check(t)
示例2: testValues
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_segment_sum [as 别名]
def testValues(self):
dtypes = [tf.float32,
tf.float64,
tf.int64,
tf.int32]
mean_dtypes = [tf.float32,
tf.float64]
# Each item is np_op1, np_op2, tf_op
ops_list = [(np.add, None, tf.sparse_segment_sum),
(self._mean_cum_op, self._mean_reduce_op,
tf.sparse_segment_mean)]
n = 400
shape = [n, 2]
segment_indices = []
for i in range(20):
for _ in range(i + 1):
segment_indices.append(i)
num_indices = len(segment_indices)
for dtype in dtypes:
with self.test_session(use_gpu=False):
tf_indices, np_indices, tf_x, np_x = self._sparse_input(shape,
num_indices,
dtype=dtype)
for np_op1, np_op2, tf_op in ops_list:
if tf_op == tf.sparse_segment_mean and dtype not in mean_dtypes:
continue
np_ans = self._sparseSegmentReduce(np_x, np_indices, segment_indices,
np_op1, np_op2)
s = tf_op(data=tf_x, indices=tf_indices, segment_ids=segment_indices)
tf_ans = s.eval()
self._assertAllClose(segment_indices, np_ans, tf_ans)
# NOTE(mrry): The static shape inference that computes
# `tf_ans.shape` can only infer that sizes from dimension 1
# onwards, because the size of dimension 0 is data-dependent
# and may therefore vary dynamically.
self.assertAllEqual(np_ans.shape[1:], tf_ans.shape[1:])
示例3: testValid
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_segment_sum [as 别名]
def testValid(self):
# Baseline for the test*Invalid* methods below.
tf_x, _ = self._input([10, 4], dtype=tf.float32)
ops_list = [tf.sparse_segment_sum, tf.sparse_segment_mean]
segment_indices = [0, 1, 2, 2]
tf_indices = [8, 3, 0, 9]
with self.test_session(use_gpu=False):
for tf_op in ops_list:
s = tf_op(data=tf_x, indices=tf_indices, segment_ids=segment_indices)
s.eval()
示例4: testIndicesInvalid1
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_segment_sum [as 别名]
def testIndicesInvalid1(self):
tf_x, _ = self._input([10, 4], dtype=tf.float32)
ops_list = [tf.sparse_segment_sum, tf.sparse_segment_mean]
segment_indices = [0, 1, 2, 2]
tf_indices = [8, -1, 0, 9]
with self.test_session(use_gpu=False):
for tf_op in ops_list:
s = tf_op(data=tf_x, indices=tf_indices, segment_ids=segment_indices)
with self.assertRaisesOpError(
r"indices\[1\] == -1 out of range \[0, 10\)"):
s.eval()
示例5: testIndicesInvalid2
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_segment_sum [as 别名]
def testIndicesInvalid2(self):
tf_x, _ = self._input([10, 4], dtype=tf.float32)
ops_list = [tf.sparse_segment_sum, tf.sparse_segment_mean]
segment_indices = [0, 1, 2, 2]
tf_indices = [8, 3, 0, 10]
with self.test_session(use_gpu=False):
for tf_op in ops_list:
s = tf_op(data=tf_x, indices=tf_indices, segment_ids=segment_indices)
with self.assertRaisesOpError(
r"indices\[3\] == 10 out of range \[0, 10\)"):
s.eval()
示例6: testSegmentsInvalid1
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_segment_sum [as 别名]
def testSegmentsInvalid1(self):
tf_x, _ = self._input([10, 4], dtype=tf.float32)
ops_list = [tf.sparse_segment_sum, tf.sparse_segment_mean]
segment_indices = [0, 2, 2, 2]
tf_indices = [8, 3, 0, 9]
with self.test_session(use_gpu=False):
for tf_op in ops_list:
s = tf_op(data=tf_x, indices=tf_indices, segment_ids=segment_indices)
with self.assertRaisesOpError("segment ids are not increasing by 1"):
s.eval()
示例7: testSegmentsInvalid3
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_segment_sum [as 别名]
def testSegmentsInvalid3(self):
tf_x, _ = self._input([10, 4], dtype=tf.float32)
ops_list = [tf.sparse_segment_sum, tf.sparse_segment_mean]
segment_indices = [0, 1, 2, 0]
tf_indices = [8, 3, 0, 9]
with self.test_session(use_gpu=False):
for tf_op in ops_list:
s = tf_op(data=tf_x, indices=tf_indices, segment_ids=segment_indices)
with self.assertRaisesOpError(
r"Segment id 1 out of range \[0, 1\), probably because "
"'segment_ids' input is not sorted"):
s.eval()
示例8: testSegmentsInvalid4
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_segment_sum [as 别名]
def testSegmentsInvalid4(self):
tf_x, _ = self._input([10, 4], dtype=tf.float32)
ops_list = [tf.sparse_segment_sum, tf.sparse_segment_mean]
segment_indices = [-1, 0, 1, 1]
tf_indices = [8, 3, 0, 9]
with self.test_session(use_gpu=False):
for tf_op in ops_list:
s = tf_op(data=tf_x, indices=tf_indices, segment_ids=segment_indices)
with self.assertRaisesOpError("segment ids do not start at 0"):
s.eval()
示例9: testSegmentsInvalid5
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_segment_sum [as 别名]
def testSegmentsInvalid5(self):
tf_x, _ = self._input([10, 4], dtype=tf.float32)
ops_list = [tf.sparse_segment_sum, tf.sparse_segment_mean]
segment_indices = [1, 2, 2, 2]
tf_indices = [8, 3, 0, 9]
with self.test_session(use_gpu=False):
for tf_op in ops_list:
s = tf_op(data=tf_x, indices=tf_indices, segment_ids=segment_indices)
with self.assertRaisesOpError("segment ids do not start at 0"):
s.eval()
示例10: testSegmentsInvalid6
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_segment_sum [as 别名]
def testSegmentsInvalid6(self):
tf_x, _ = self._input([10, 4], dtype=tf.float32)
ops_list = [tf.sparse_segment_sum, tf.sparse_segment_mean]
segment_indices = [0, 0, 0, -1]
tf_indices = [8, 3, 0, 9]
with self.test_session(use_gpu=False):
for tf_op in ops_list:
s = tf_op(data=tf_x, indices=tf_indices, segment_ids=segment_indices)
with self.assertRaisesOpError("segment ids must be >= 0"):
s.eval()
示例11: testSegmentsInvalid7
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_segment_sum [as 别名]
def testSegmentsInvalid7(self):
tf_x, _ = self._input([10, 4], dtype=tf.float32)
ops_list = [tf.sparse_segment_sum, tf.sparse_segment_mean]
segment_indices = [0, 0, 0, -2]
tf_indices = [8, 3, 0, 9]
with self.test_session(use_gpu=False):
for tf_op in ops_list:
s = tf_op(data=tf_x, indices=tf_indices, segment_ids=segment_indices)
with self.assertRaisesOpError("segment ids must be >= 0"):
s.eval()
示例12: _calc_output
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_segment_sum [as 别名]
def _calc_output(self):
self.E = self.F = self.S = 0
for i, item in enumerate(self.parent.inputs['atom_types']):
zero_cond = tf.equal(tf.reduce_sum(self.next_elem['N_'+item]), 0)
self.E += tf.cond(zero_cond,
lambda: tf.cast(0., tf.float64),
lambda: tf.sparse_segment_sum(self.ys[item], self.next_elem['sparse_indices_'+item], self.next_elem['seg_id_'+item],
num_segments=self.next_elem['num_seg'])[1:])
if self.inputs['use_force']:
tmp_force = self.next_elem['dx_'+item] * \
tf.expand_dims(\
tf.expand_dims(self.dys[item], axis=2),
axis=3)
tmp_force = tf.reduce_sum(\
tf.sparse_segment_sum(tmp_force, self.next_elem['sparse_indices_'+item], self.next_elem['seg_id_'+item],
num_segments=self.next_elem['num_seg'])[1:],
axis=1)
self.F -= tf.cond(zero_cond,
lambda: tf.cast(0., tf.float64),
lambda: tf.dynamic_partition(tf.reshape(tmp_force, [-1,3]),
self.next_elem['partition'], 2)[1])
if self.inputs['use_stress']:
tmp_stress = self.next_elem['da_'+item] * \
tf.expand_dims(\
tf.expand_dims(self.dys[item], axis=2),
axis=3)
tmp_stress = tf.cond(zero_cond,
lambda: tf.cast(0., tf.float64) * tmp_stress,
lambda: tf.sparse_segment_sum(tmp_stress, self.next_elem['sparse_indices_'+item], self.next_elem['seg_id_'+item],
num_segments=self.next_elem['num_seg'])[1:])
self.S -= tf.reduce_sum(tmp_stress, axis=[1,2])/units.GPa*10
示例13: _get_loss
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_segment_sum [as 别名]
def _get_loss(self, use_gdf=False, atomic_weights=None):
if self.inputs['E_loss'] == 1:
self.e_loss = tf.square((self.next_elem['E'] - self.E) / self.next_elem['tot_num']) * self.next_elem['tot_num']
elif self.inputs['E_loss'] == 2:
self.e_loss = tf.square(self.next_elem['E'] - self.E)
else:
self.e_loss = tf.square((self.next_elem['E'] - self.E) / self.next_elem['tot_num'])
self.sw_e_loss = self.e_loss * self.next_elem['struct_weight']
self.e_loss = tf.reshape(self.e_loss, [-1])
self.str_e_loss = tf.unsorted_segment_mean(self.e_loss, self.next_elem['struct_ind'], tf.size(self.next_elem['struct_type_set']))
self.str_e_loss = tf.reshape(self.str_e_loss, [-1])
self.e_loss = tf.reduce_mean(self.e_loss)
self.sw_e_loss = tf.reduce_mean(self.sw_e_loss)
self.total_loss = self.sw_e_loss * self.energy_coeff
self.str_num_batch_atom = tf.reshape(tf.unsorted_segment_sum(self.next_elem['tot_num'], self.next_elem['struct_ind'], tf.size(self.next_elem['struct_type_set'])), [-1])
if self.inputs['use_force']:
self.f_loss = tf.reshape(tf.square(self.next_elem['F'] - self.F), [-1, 3])
ind = tf.reshape(repeat(self.next_elem['struct_ind'],
tf.cast(tf.reshape(self.next_elem['tot_num'], shape=[-1]), tf.int32)), [-1])
self.str_f_loss = tf.unsorted_segment_mean(self.f_loss, ind, tf.size(self.next_elem['struct_type_set']))
self.str_f_loss = tf.reduce_mean(self.str_f_loss, axis=1)
if self.parent.descriptor.inputs['atomic_weights']['type'] is not None:
self.aw_f_loss = self.f_loss * self.next_elem['atomic_weights']
if self.inputs['F_loss'] == 1:
self.f_loss = tf.reduce_mean(self.f_loss)
self.aw_f_loss = tf.reduce_mean(self.aw_f_loss)
else:
self.f_loss = tf.reduce_mean(tf.sparse_segment_sum(self.f_loss, self.next_elem['sparse_indices_'], self.next_elem['seg_id_'],
num_segments=self.next_elem['num_seg'])[1:])
self.aw_f_loss = tf.reduce_mean(tf.sparse_segment_sum(self.aw_f_loss, self.next_elem['sparse_indices_'], self.next_elem['seg_id_'],
num_segments=self.next_elem['num_seg'])[1:])
self.total_loss += self.aw_f_loss * self.force_coeff
else:
if self.inputs['F_loss'] == 1:
self.f_loss = tf.reduce_mean(self.f_loss)
else:
self.f_loss = tf.reduce_mean(tf.sparse_segment_sum(self.f_loss, self.next_elem['sparse_indices_'], self.next_elem['seg_id_'],
num_segments=self.next_elem['num_seg'])[1:])
self.total_loss += self.f_loss * self.force_coeff
if self.inputs['use_stress']:
self.ax_s_loss = tf.square(self.next_elem['S'] - self.S)
self.s_loss = tf.reduce_mean(self.ax_s_loss, axis=1, keepdims=True)
self.sw_s_loss = self.s_loss * self.next_elem['struct_weight']
self.s_loss = tf.reshape(self.s_loss,[-1])
self.str_s_loss = tf.unsorted_segment_mean(self.s_loss, self.next_elem['struct_ind'], tf.size(self.next_elem['struct_type_set']))
self.str_s_loss = tf.reshape(self.str_s_loss, [-1])
self.s_loss = tf.reduce_mean(self.s_loss)
self.sw_s_loss = tf.reduce_mean(self.sw_s_loss)
self.total_loss += self.sw_s_loss * self.stress_coeff
if self.inputs['regularization']['type'] is not None:
# FIXME: regularization_loss, which is float32, is casted into float64.
self.total_loss += tf.cast(tf.losses.get_regularization_loss(), tf.float64)