def testDeprecatedFunctionEndpoint(self, mock_warning): array = tf.IndexedSlices( tf.compat.v1.convert_to_tensor(np.array([1, 2])), tf.compat.v1.convert_to_tensor(np.array([0, 2]))) mask_indices = tf.compat.v1.convert_to_tensor(np.array([2])) self.assertEqual(0, mock_warning.call_count) tf.sparse.mask(array, mask_indices) self.assertEqual(0, mock_warning.call_count) tf.sparse_mask(array, mask_indices) self.assertEqual(1, mock_warning.call_count) self.assertRegexpMatches(mock_warning.call_args[0][1], "deprecation_test.py:") self.assertRegexpMatches(mock_warning.call_args[0][2], r"sparse_mask") self.assertRegexpMatches(mock_warning.call_args[0][3], "sparse.mask") tf.sparse_mask(array, mask_indices) self.assertEqual(1, mock_warning.call_count)
def testDeprecatedFunctionEndpoint(self, mock_warning): array = tf.IndexedSlices( tf.compat.v1.convert_to_tensor(np.array([1, 2])), tf.compat.v1.convert_to_tensor(np.array([0, 2]))) mask_indices = tf.compat.v1.convert_to_tensor(np.array([2])) self.assertEqual(0, mock_warning.call_count) tf.sparse.mask(array, mask_indices) self.assertEqual(0, mock_warning.call_count) tf.sparse_mask(array, mask_indices) self.assertEqual(1, mock_warning.call_count) self.assertRegexpMatches( mock_warning.call_args[0][1], "deprecation_test.py:") self.assertRegexpMatches( mock_warning.call_args[0][2], r"sparse_mask") self.assertRegexpMatches( mock_warning.call_args[0][3], "sparse.mask") tf.sparse_mask(array, mask_indices) self.assertEqual(1, mock_warning.call_count)
def testBasic(self): values = np.random.rand(4, 4).astype(np.single) indices = np.array([0, 2, 3, 4], dtype=np.int32) mask_indices = np.array([0], dtype=np.int32) out_values = values[1:, :] out_indices = np.array([2, 3, 4], dtype=np.int32) with self.test_session() as sess: values_tensor = tf.convert_to_tensor(values) indices_tensor = tf.convert_to_tensor(indices) mask_indices_tensor = tf.convert_to_tensor(mask_indices) t = tf.IndexedSlices(values_tensor, indices_tensor) masked_t = tf.sparse_mask(t, mask_indices_tensor) tf_out_values, tf_out_indices = sess.run( [masked_t.values, masked_t.indices]) self.assertAllEqual(tf_out_values, out_values) self.assertAllEqual(tf_out_indices, out_indices)
def testBasic(self): values = np.random.rand(4, 4).astype(np.single) indices = np.array([0, 2, 3, 4], dtype=np.int32) mask_indices = np.array([0], dtype=np.int32) out_values = values[1:, :] out_indices = np.array([2, 3, 4], dtype=np.int32) with self.test_session() as sess: values_tensor = tf.convert_to_tensor(values) indices_tensor = tf.convert_to_tensor(indices) mask_indices_tensor = tf.convert_to_tensor(mask_indices) t = tf.IndexedSlices(values_tensor, indices_tensor) masked_t = tf.sparse_mask(t, mask_indices_tensor) tf_out_values, tf_out_indices = sess.run([masked_t.values, masked_t.indices]) self.assertAllEqual(tf_out_values, out_values) self.assertAllEqual(tf_out_indices, out_indices)
def optimize_graph(self, loss, freeze_vars=None, train_vars=None): """Build the graph to optimize the loss function.""" # Global step self.global_step = tf.Variable(0, name="global_step") lr = self.learning_rate * tf.exp( -tf.cast(self.global_step, tf.float32) * self.lr_decay) # Instead of running optimizer.minimize directly, call compute gradients # and process returned gradients optimizer = tf.train.AdagradOptimizer(lr) grads_and_vars = optimizer.compute_gradients(loss) # Remove frozen indices from gradients processes_grads_and_vars = [] for (g, v) in grads_and_vars: if freeze_vars and (v in freeze_vars): freeze_indices = freeze_vars[v] # Remove all gradients for this variable if freeze_indices == True: g = None # Process dense gradients elif isinstance(g, tf.Tensor): print("Freezing {} indicies of variable '{}' [D]".format( len(freeze_indices), v.name)) update_shape = [len(freeze_indices)] + list( g.get_shape()[1:]) gradient_mask = tf.zeros(update_shape, dtype=g.dtype) g = tf.scatter_mul(g, freeze_indices, gradient_mask) # Process sparse gradients elif isinstance(g, tf.IndexedSlices): print("Freezing {} indicies of variable '{}' [S]".format( len(freeze_indices), v.name)) # Remove frozen indices from gradient g = tf.sparse_mask(g, freeze_indices) if train_vars and (v in train_vars): trainable_indices = train_vars[v] # Process dense gradients if isinstance(g, tf.Tensor): print("Training only on {} indicies of variable '{}' [D]". format(len(freeze_indices), v.name)) gradient_mask = tf.scatter_nd( tf.reshape(trainable_indices, [-1, 1]), tf.ones(tf.get_shape(trainable_indices)), [g.get_shape()[0], 1]) g = tf.multiply(g, gradient_mask) # Process sparse gradients elif isinstance(g, tf.IndexedSlices): print("Training only on {} indicies of variable '{}' [S]". format(len(freeze_indices), v.name)) raise RuntimeError processes_grads_and_vars.append((g, v)) train = optimizer.apply_gradients(processes_grads_and_vars, global_step=self.global_step, name="train") tf.summary.scalar("Learning rate", lr) return train
tf.to_int32() tf.to_int64() tf.trace() tf.trainable_variables() tf.transpose() tf.truncated_normal() tf.truediv() tf.sparse_transpose() tf.sparse_tensor_dense_matmul() tf.sparse_accumulator_apply_gradient() tf.sparse_accumulator_take_gradient() tf.sparse_add() tf.sparse_concat() tf.sparse_conditional_accumulator() tf.sparse_mask() tf.sparse_matmul() tf.sparse_maximum() tf.sparse_merge() tf.sparse_minimum() tf.sparse_reduce_max() tf.sparse_reduce_max_sparse() tf.reduce_all() tf.reduce_any() tf.reduce_join() tf.reduce_logsumexp() tf.reduce_max() tf.reduce_mean() tf.reduce_min()