Python tensorflow.sparse_segment_sum() Examples
The following are 13
code examples of tensorflow.sparse_segment_sum().
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 also want to check out all available functions/classes of the module
tensorflow
, or try the search function
.
Example #1
Source File: ops.py From tfdeploy with MIT License | 5 votes |
def test_SparseSegmentSum(self): t = tf.sparse_segment_sum(self.random(4, 3, 2), [0, 2, 3], [0, 1, 1]) self.check(t)
Example #2
Source File: segment_reduction_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
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:])
Example #3
Source File: segment_reduction_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
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()
Example #4
Source File: segment_reduction_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
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()
Example #5
Source File: segment_reduction_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
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()
Example #6
Source File: segment_reduction_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
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()
Example #7
Source File: segment_reduction_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
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()
Example #8
Source File: segment_reduction_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
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()
Example #9
Source File: segment_reduction_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
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()
Example #10
Source File: segment_reduction_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
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()
Example #11
Source File: segment_reduction_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
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()
Example #12
Source File: neural_network.py From SIMPLE-NN with GNU General Public License v3.0 | 5 votes |
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
Example #13
Source File: neural_network.py From SIMPLE-NN with GNU General Public License v3.0 | 4 votes |
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)