def var_in_else_clause(): v2 = variables.Variable(2, name="v2") var_dict["v2"] = v2 return v2 + v0
def testVariableReadValueGradient(self): with ops.Graph().as_default(): init = constant_op.constant(100.0) var = variables.Variable(init) gradient = gradients.gradients(var.read_value(), var) self.assertIsNotNone(gradient)
def _test_mixed_precision(self, task_type, task_id, num_gpus): """Tests mixed precision works with the CollectiveAllReduceStrategy. This tests: 1. Variables are in float32, by running with a small enough learning rate that if the variables are float16, their values wouldn't change when gradients are applied. 2. The loss scale is doubled if there are no NaNs. 3. The loss scale is halved if the first worker has a NaN, even if the other works do not have NaNs. Args: task_type: A string, such as "worker", indicating the type of the replica. task_id: Zero-indexed ID of the task. num_gpus: The number of GPUs to use. """ d, master_target, config = self._get_test_object( task_type, task_id, num_gpus) # Should be set to mixed_float16 by caller. self.assertEqual(policy.global_policy().name, 'mixed_float16') with ops.Graph().as_default(), \ self.cached_session(config=config, target=master_target) as sess: # The loss on the first worker is multiplied by this value. Allows # testing the first worker having NaN loss and gradients while keeping the # other workers' losses and gradients finite. loss_multiplier_for_first_worker = variables.Variable( 1., dtype='float16', trainable=False) with d.scope(): model = keras.Sequential([ mp_test_util.MultiplyLayer(assert_type=dtypes.float16, input_shape=(1, )), ]) loss_scale = loss_scale_module.DynamicLossScale( 2**10, increment_period=1) def model_fn(): """Simple model to test mixed precision.""" x = np.ones((1, 1)) loss = model(x, training=True) if ((task_type == 'worker' and task_id == 0) or task_type is task_id is None): loss *= loss_multiplier_for_first_worker # Learning rate is small enough that if applied to a float16 variable, # the variable will not change. So this tests the learning rate is not # applied to a float16 value, but instead the float32 variable. optimizer = gradient_descent.GradientDescentOptimizer( 2**-14) optimizer = loss_scale_optimizer.MixedPrecisionLossScaleOptimizer( optimizer, loss_scale) train_op = optimizer.minimize( loss, training_util.get_or_create_global_step()) return train_op train_op = d.extended.call_for_each_replica(model_fn) train_op = d.group(d.experimental_local_results(train_op)) sess.run(variables.global_variables_initializer()) sess.run(train_op) (var, ) = model.trainable_weights # Variable starts at 1. Each worker's gradient is 2 ** -14, the learning # rate, and each worker's gradient will be subtracted from the variable. expected = 1 - d.num_replicas_in_sync * 2**-14 self.assertEqual(sess.run(var), expected) # Loss scale should double, as are gradients are finite. self.assertEqual(sess.run(loss_scale()), 2**11) # Set the first worker to have NaN loss and gradients. sess.run(loss_multiplier_for_first_worker.assign(float('NaN'))) sess.run(train_op) # Variable should not change, since first worker had NaN self.assertEqual(sess.run(var), expected) # Loss scale should halve due to NaN self.assertEqual(sess.run(loss_scale()), 2**10)
def fn(x): state.append(variables.Variable(1.0)) return state[-1] + x
def setUp(self): self.var_a = variables.Variable(42.0, name="a")
def make_y(self): if self.y is None: self.y = variables.Variable(1., name='v')
def f(): if not created_variables: created_variables.append(variables.Variable(1.)) return created_variables[0] + 1.
def testZeroSizeVarInitialized(self): with ops.Graph().as_default(), self.test_session() as sess: v = variables.Variable(array_ops.zeros([0, 2]), name="v") uninited = variables.report_uninitialized_variables() v.initializer.run() # not strictly necessary self.assertEqual(0, sess.run(uninited).size)
def function_with_create(trainable): """Creates a variable as a side effect using tf.Variable.""" variables.Variable(0, trainable=trainable) return variable_scope.get_variable( "dummy", shape=[1], initializer=init_ops.zeros_initializer())
def testRepr(self): var = variables.Variable(np.zeros((5, 5), np.float32), name="noop") self.assertEqual( "<tf.Variable 'noop:0' shape=(5, 5) dtype=float32_ref>", repr(var))
def create_variable(): with ops.name_scope("foo"): v = variables.Variable(0.0, name="bar") self.assertEqual(v.name, "foo/bar:0")
def testSession(self): with self.test_session() as sess: var = variables.Variable([1, 12]) variables.global_variables_initializer().run() self.assertAllClose([1, 12], sess.run(var))
def testOperators(self): with self.test_session(): var_f = variables.Variable([2.0]) add = var_f + 0.0 radd = 1.0 + var_f sub = var_f - 1.0 rsub = 1.0 - var_f mul = var_f * 10.0 rmul = 10.0 * var_f div = var_f / 10.0 rdiv = 10.0 / var_f lt = var_f < 3.0 rlt = 3.0 < var_f le = var_f <= 2.0 rle = 2.0 <= var_f gt = var_f > 3.0 rgt = 3.0 > var_f ge = var_f >= 2.0 rge = 2.0 >= var_f neg = -var_f abs_v = abs(var_f) var_i = variables.Variable([20]) mod = var_i % 7 rmod = 103 % var_i var_b = variables.Variable([True, False]) and_v = operator.and_(var_b, [True, True]) or_v = operator.or_(var_b, [False, True]) xor_v = operator.xor(var_b, [False, False]) invert_v = ~var_b rnd = np.random.rand(4, 4).astype("f") var_t = variables.Variable(rnd) slice_v = var_t[2, 0:0] var_m = variables.Variable([[2.0, 3.0]]) matmul = var_m.__matmul__([[10.0], [20.0]]) rmatmul = var_m.__rmatmul__([[10.0], [20.0]]) variables.global_variables_initializer().run() self.assertAllClose([2.0], add.eval()) self.assertAllClose([3.0], radd.eval()) self.assertAllClose([1.0], sub.eval()) self.assertAllClose([-1.0], rsub.eval()) self.assertAllClose([20.0], mul.eval()) self.assertAllClose([20.0], rmul.eval()) self.assertAllClose([0.2], div.eval()) self.assertAllClose([5.0], rdiv.eval()) self.assertAllClose([-2.0], neg.eval()) self.assertAllClose([2.0], abs_v.eval()) self.assertAllClose([True], lt.eval()) self.assertAllClose([False], rlt.eval()) self.assertAllClose([True], le.eval()) self.assertAllClose([True], rle.eval()) self.assertAllClose([False], gt.eval()) self.assertAllClose([True], rgt.eval()) self.assertAllClose([True], ge.eval()) self.assertAllClose([True], rge.eval()) self.assertAllClose([6], mod.eval()) self.assertAllClose([3], rmod.eval()) self.assertAllClose([True, False], and_v.eval()) self.assertAllClose([True, True], or_v.eval()) self.assertAllClose([True, False], xor_v.eval()) self.assertAllClose([False, True], invert_v.eval()) self.assertAllClose(rnd[2, 0:0], slice_v.eval()) self.assertAllClose([[80.0]], matmul.eval()) self.assertAllClose([[20.0, 30.0], [40.0, 60.0]], rmatmul.eval())
def body(i, _): zero = array_ops.zeros([], dtype=dtypes.int32) v = variables.Variable(initial_value=zero) return (i + 1, v.read_value())
def apply(self, x): if self.var is None: self.var = variables.Variable(2.0) return self.var * x
def test_variable(self): shape = tensor_shape.TensorShape([]) fn_true = lambda: variables.Variable(3.0) fn_false = lambda: variables.Variable(4.0) self._testShape(fn_true, fn_false, shape) self._testReturnValues(fn_true, fn_false, 3.0, 4.0)
def make_x(self): if self.x is None: self.x = variables.Variable(1., name='v')
def __init__(self): self.a = [1.] self.a.append(variables.Variable(2.)) self.b = {"a": variables.Variable(3.)}
def make_z(self): if self.z is None: with ops.name_scope('z_scope', skip_on_eager=False): self.z = variables.Variable(1., name='z')
def build(self, input_shape): self.c = variables.Variable(constant_op.constant( 1.0, shape=input_shape[1:]), name=self.name + '_c')
def fn(x): return variables.Variable(1.0) + x
def testContainer(self): """Set containers outside & inside of cond_v2. Make sure the containers are set correctly for both variable creation (tested by variables.Variable) and for stateful ops (tested by FIFOQueue) """ self.skipTest("b/113048653") with ops.Graph().as_default() as g: with self.session(graph=g): v0 = variables.Variable([0]) q0 = data_flow_ops.FIFOQueue(1, dtypes.float32) def container(node): return node.op.get_attr("container") self.assertEqual(compat.as_bytes(""), container(v0)) self.assertEqual(compat.as_bytes(""), container(q0.queue_ref)) def true_fn(): # When this branch is created in cond below, # the container should begin with 'l1' v1 = variables.Variable([1]) q1 = data_flow_ops.FIFOQueue(1, dtypes.float32) with ops.container("l2t"): v2 = variables.Variable([2]) q2 = data_flow_ops.FIFOQueue(1, dtypes.float32) v3 = variables.Variable([1]) q3 = data_flow_ops.FIFOQueue(1, dtypes.float32) self.assertEqual(compat.as_bytes("l1"), container(v1)) self.assertEqual(compat.as_bytes("l1"), container(q1.queue_ref)) self.assertEqual(compat.as_bytes("l2t"), container(v2)) self.assertEqual(compat.as_bytes("l2t"), container(q2.queue_ref)) self.assertEqual(compat.as_bytes("l1"), container(v3)) self.assertEqual(compat.as_bytes("l1"), container(q3.queue_ref)) return constant_op.constant(2.0) def false_fn(): # When this branch is created in cond below, # the container should begin with 'l1' v1 = variables.Variable([1]) q1 = data_flow_ops.FIFOQueue(1, dtypes.float32) with ops.container("l2f"): v2 = variables.Variable([2]) q2 = data_flow_ops.FIFOQueue(1, dtypes.float32) v3 = variables.Variable([1]) q3 = data_flow_ops.FIFOQueue(1, dtypes.float32) self.assertEqual(compat.as_bytes("l1"), container(v1)) self.assertEqual(compat.as_bytes("l1"), container(q1.queue_ref)) self.assertEqual(compat.as_bytes("l2f"), container(v2)) self.assertEqual(compat.as_bytes("l2f"), container(q2.queue_ref)) self.assertEqual(compat.as_bytes("l1"), container(v3)) self.assertEqual(compat.as_bytes("l1"), container(q3.queue_ref)) return constant_op.constant(6.0) with ops.container("l1"): cnd_true = cond_v2.cond_v2(constant_op.constant(True), true_fn, false_fn) self.assertEquals(cnd_true.eval(), 2) cnd_false = cond_v2.cond_v2(constant_op.constant(False), true_fn, false_fn) self.assertEquals(cnd_false.eval(), 6) v4 = variables.Variable([3]) q4 = data_flow_ops.FIFOQueue(1, dtypes.float32) v5 = variables.Variable([4]) q5 = data_flow_ops.FIFOQueue(1, dtypes.float32) self.assertEqual(compat.as_bytes("l1"), container(v4)) self.assertEqual(compat.as_bytes("l1"), container(q4.queue_ref)) self.assertEqual(compat.as_bytes(""), container(v5)) self.assertEqual(compat.as_bytes(""), container(q5.queue_ref))
def _create_dynamic_rnn(): with session.Session(config=config, graph=ops_lib.Graph()): inputs_t = variables_lib.Variable(inputs, trainable=False).value() _static_vs_dynamic_rnn_benchmark_dynamic(inputs_t, sequence_length)
def testOpWithNoTrainableOutputs(self): v = variables.Variable(1.) with forwardprop.ForwardAccumulator(v, 11.): v.assign_sub(0.5) self.assertAllClose(0.5, self.evaluate(v))
def testAllowsDifferentWatchesOnDifferentRuns(self): """Test watching different tensors on different runs of the same graph.""" with session.Session(config=self._no_rewrite_session_config()) as sess: u_init_val = [[5.0, 3.0], [-1.0, 0.0]] v_init_val = [[2.0], [-1.0]] # Use node names with overlapping namespace (i.e., parent directory) to # test concurrent, non-racing directory creation. u_name = "diff_Watch/u" v_name = "diff_Watch/v" u_init = constant_op.constant(u_init_val, shape=[2, 2]) u = variables.Variable(u_init, name=u_name) v_init = constant_op.constant(v_init_val, shape=[2, 1]) v = variables.Variable(v_init, name=v_name) w = math_ops.matmul(u, v, name="diff_Watch/matmul") u.initializer.run() v.initializer.run() for i in range(2): run_options = config_pb2.RunOptions(output_partition_graphs=True) run_dump_root = self._debug_dump_dir(run_number=i) debug_urls = self._debug_urls(run_number=i) if i == 0: # First debug run: Add debug tensor watch for u. debug_utils.add_debug_tensor_watch( run_options, "%s/read" % u_name, 0, debug_urls=debug_urls) else: # Second debug run: Add debug tensor watch for v. debug_utils.add_debug_tensor_watch( run_options, "%s/read" % v_name, 0, debug_urls=debug_urls) run_metadata = config_pb2.RunMetadata() # Invoke Session.run(). sess.run(w, options=run_options, run_metadata=run_metadata) self.assertEqual(self._expected_partition_graph_count, len(run_metadata.partition_graphs)) dump = debug_data.DebugDumpDir( run_dump_root, partition_graphs=run_metadata.partition_graphs) self.assertTrue(dump.loaded_partition_graphs()) # Each run should have generated only one dumped tensor, not two. self.assertEqual(1, dump.size) if i == 0: self.assertAllClose([u_init_val], dump.get_tensors("%s/read" % u_name, 0, "DebugIdentity")) self.assertGreaterEqual( dump.get_rel_timestamps("%s/read" % u_name, 0, "DebugIdentity")[0], 0) else: self.assertAllClose([v_init_val], dump.get_tensors("%s/read" % v_name, 0, "DebugIdentity")) self.assertGreaterEqual( dump.get_rel_timestamps("%s/read" % v_name, 0, "DebugIdentity")[0], 0)
def fn(x): if not state: state.append(variables.Variable(lambda: 2.0)) return state[0] * x
def testVariableAsGraphElementGradient(self): with ops.Graph().as_default() as graph: init = constant_op.constant(100.0) var = variables.Variable(init) gradient = gradients.gradients(graph.as_graph_element(var), var) self.assertIsNotNone(gradient)
def fn(x): if not state: state.append(variables.Variable(2.0 * x)) return state[0] * x
def _setup_training(self): """Sets up graph, model and trainer.""" # Create config if not given. if self._config is None: self._config = RunConfig(verbose=self.verbose) # Create new graph. self._graph = ops.Graph() self._graph.add_to_collection("IS_TRAINING", True) with self._graph.as_default(): random_seed.set_random_seed(self._config.tf_random_seed) self._global_step = variables.Variable(0, name="global_step", trainable=False) # Setting up inputs and outputs. self._inp, self._out = self._data_feeder.input_builder() # If class weights are provided, add them to the graph. # Different loss functions can use this tensor by name. if self.class_weight: self._class_weight_node = constant_op.constant( self.class_weight, name='class_weight') # Add histograms for X and y if they are floats. if self._data_feeder.input_dtype in (np.float32, np.float64): logging_ops.histogram_summary("X", self._inp) if self._data_feeder.output_dtype in (np.float32, np.float64): logging_ops.histogram_summary("y", self._out) # Create model's graph. self._model_predictions, self._model_loss = self.model_fn( self._inp, self._out) # Set up a single operator to merge all the summaries self._summaries = logging_ops.merge_all_summaries() # Create trainer and augment graph with gradients and optimizer. # Additionally creates initialization ops. learning_rate = self.learning_rate optimizer = self.optimizer if callable(learning_rate): learning_rate = learning_rate(self._global_step) if callable(optimizer): optimizer = optimizer(learning_rate) self._train = optimizers.optimize_loss( self._model_loss, self._global_step, learning_rate=learning_rate, optimizer=optimizer, clip_gradients=self.clip_gradients) # Update ops during training, e.g. batch_norm_ops self._train = control_flow_ops.group( self._train, *ops.get_collection('update_ops')) # Get all initializers for all trainable variables. self._initializers = variables.initialize_all_variables() # Create model's saver capturing all the nodes created up until now. self._saver = train.Saver( max_to_keep=self._config.keep_checkpoint_max, keep_checkpoint_every_n_hours=self._config. keep_checkpoint_every_n_hours) # Enable monitor to create validation data dict with appropriate tf placeholders self._monitor.create_val_feed_dict(self._inp, self._out) # Create session to run model with. self._session = session.Session(self._config.tf_master, config=self._config.tf_config) # Run parameter initializers. self._session.run(self._initializers)
def var_in_then_clause(): v1 = variables.Variable(1, name="v1") var_dict["v1"] = v1 return v1 + v0