Python tensorflow.python.keras.backend.get_value() Examples

The following are 13 code examples of tensorflow.python.keras.backend.get_value(). 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.python.keras.backend , or try the search function .
Example #1
Source Project: nupic.tensorflow   Author: numenta   File: k_winners.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def get_config(self):
        config = {
            "percent_on": self.percent_on,
            "k_inference_factor": self.k_inference_factor,
            "boost_strength": K.get_value(self.boost_strength),
            "boost_strength_factor": self.boost_strength_factor,
            "duty_cycle_period": self.duty_cycle_period,
        }
        config.update(super(KWinnersBase, self).get_config())
        return config 
Example #2
Source Project: nupic.tensorflow   Author: numenta   File: k_winners.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def call(self, inputs, training=None, **kwargs):
        inputs = super().call(inputs, **kwargs)
        k = K.in_test_phase(x=self.k_inference, alt=self.k, training=training)
        kwinners = compute_kwinners(
            x=inputs,
            k=k,
            duty_cycles=self.duty_cycles,
            boost_strength=K.get_value(self.boost_strength),
        )

        duty_cycles = K.in_train_phase(
            lambda: self.compute_duty_cycle(kwinners),
            self.duty_cycles,
            training=training,
        )
        self.add_update(self.duty_cycles.assign(duty_cycles, read_value=False))

        increment = K.in_train_phase(K.shape(inputs)[0], 0, training=training)
        self.add_update(
            self.learning_iterations.assign_add(increment, read_value=False)
        )

        return kwinners 
Example #3
Source Project: kfac   Author: tensorflow   File: keras_optimizers_test.py    License: Apache License 2.0 6 votes vote down vote up
def testSetTFVariableHyper(self, name, val):
    kwargs = {'learning_rate': 0.01, 'damping': 0.001}
    kwargs[name] = tf.Variable(45.0)
    opt = optimizers.Kfac(model=_simple_mlp(), loss='mse', **kwargs)
    setattr(opt, name, val)

    with self.subTest(name='AssignedCorrectly'):
      self.assertEqual(backend.get_value(getattr(opt, name)), val)
      if hasattr(opt.optimizer, name):
        self.assertEqual(backend.get_value(getattr(opt.optimizer, name)), val)

    with self.subTest(name='SetError'):
      with self.assertRaisesRegex(ValueError, 'Dynamic reassignment only.*'):
        setattr(opt, name, tf.convert_to_tensor(2))
      with self.assertRaisesRegex(ValueError, 'Dynamic reassignment only.*'):
        setattr(opt, name, tf.Variable(2)) 
Example #4
Source Project: kfac   Author: tensorflow   File: keras_optimizers_test.py    License: Apache License 2.0 6 votes vote down vote up
def testSetFloatHyper(self, name, val):
    kwargs = {'learning_rate': 0.01, 'damping': 0.001}
    kwargs[name] = 45.0
    opt = optimizers.Kfac(model=_simple_mlp(), loss='mse', **kwargs)
    setattr(opt, name, val)

    with self.subTest(name='AssignedCorrectly'):
      self.assertEqual(backend.get_value(getattr(opt, name)), val)
      if hasattr(opt.optimizer, name):
        self.assertEqual(backend.get_value(getattr(opt.optimizer, name)), val)

    with self.subTest(name='SetError'):
      with self.assertRaisesRegex(ValueError, 'Dynamic reassignment only.*'):
        setattr(opt, name, tf.convert_to_tensor(2))
      with self.assertRaisesRegex(ValueError, 'Dynamic reassignment only.*'):
        setattr(opt, name, tf.Variable(2)) 
Example #5
Source Project: kfac   Author: tensorflow   File: keras_optimizers_test.py    License: Apache License 2.0 6 votes vote down vote up
def testInferredBatchSize(self):
    dataset = tf.data.Dataset.from_tensors(([1.], [1.]))
    dataset = dataset.repeat().batch(11, drop_remainder=True)
    train_batch = dataset.make_one_shot_iterator().get_next()

    model = tf.keras.Sequential([tf.keras.layers.Dense(1, input_shape=(1,))])
    loss = 'mse'
    train_batch = dataset.make_one_shot_iterator().get_next()
    optimizer = optimizers.Kfac(damping=10.,
                                train_batch=train_batch,
                                model=model,
                                adaptive=True,
                                loss=loss,
                                qmodel_update_rescale=0.01)
    model.compile(optimizer, loss)
    model.train_on_batch(train_batch[0], train_batch[1])
    self.assertEqual(
        tf.keras.backend.get_value(optimizer.optimizer._batch_size), 11) 
Example #6
Source Project: kfac   Author: tensorflow   File: keras_optimizers_test.py    License: Apache License 2.0 5 votes vote down vote up
def testGettingHyper(self, hyper_ctor):
    kwarg_values = {'learning_rate': 3, 'damping': 20, 'momentum': 13}
    kwargs = {k: hyper_ctor(v) for k, v in kwarg_values.items()}
    opt = optimizers.Kfac(model=_simple_mlp(), loss='mse', **kwargs)
    get_value = backend.get_value
    tf_opt = opt.optimizer
    with self.subTest(name='MatchesFloat'):
      for name, val in kwarg_values.items():
        self.assertEqual(get_value(getattr(opt, name)), val)
    with self.subTest(name='MatchesTfOpt'):
      self.assertEqual(get_value(opt.lr), get_value(tf_opt.learning_rate))
      self.assertEqual(get_value(opt.damping), get_value(tf_opt.damping))
      self.assertEqual(get_value(opt.momentum), get_value(tf_opt.momentum)) 
Example #7
Source Project: kfac   Author: tensorflow   File: keras_optimizers_test.py    License: Apache License 2.0 5 votes vote down vote up
def testGettingVariableHyperFails(self):
    self.skipTest('This is not fixed in TF 1.14 yet.')
    opt = optimizers.Kfac(model=_simple_mlp(),
                          loss='mse',
                          learning_rate=tf.Variable(0.1),
                          damping=tf.Variable(0.1))
    with self.assertRaisesRegex(tf.errors.FailedPreconditionError,
                                '.*uninitialized.*'):
      backend.get_value(opt.learning_rate) 
Example #8
Source Project: kfac   Author: tensorflow   File: keras_optimizers_test.py    License: Apache License 2.0 5 votes vote down vote up
def testModifyingTensorHypersFails(self, name, val):
    kwargs = {'learning_rate': 3, 'damping': 5, 'momentum': 7}
    kwargs[name] = tf.convert_to_tensor(val)
    opt = optimizers.Kfac(model=_simple_mlp(), loss='mse', **kwargs)
    with self.subTest(name='AssignedCorrectly'):
      self.assertEqual(backend.get_value(getattr(opt, name)), val)
    with self.subTest(name='RaisesError'):
      with self.assertRaisesRegex(AttributeError,
                                  "Can't set attribute: {}".format(name)):
        setattr(opt, name, 17) 
Example #9
Source Project: MimickNet   Author: Ouwen   File: multi_reducelronplateau.py    License: Apache License 2.0 5 votes vote down vote up
def get_lr(self):
        return K.get_value(self.training_models[0].optimizer.lr) 
Example #10
Source Project: text   Author: tensorflow   File: todense_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_ragged_input_pad_and_mask(self):
    input_data = ragged_factory_ops.constant([[1, 2, 3, 4, 5], []])
    expected_mask = np.array([True, False])

    output = ToDense(pad_value=-1, mask=True)(input_data)
    self.assertTrue(hasattr(output, "_keras_mask"))
    self.assertIsNot(output._keras_mask, None)
    self.assertAllEqual(K.get_value(output._keras_mask), expected_mask) 
Example #11
Source Project: text   Author: tensorflow   File: todense_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_sparse_input_pad_and_mask(self):
    input_data = sparse_tensor.SparseTensor(
        indices=[[0, 0], [1, 2]], values=[1, 2], dense_shape=[3, 4])

    expected_mask = np.array([True, True, False])

    output = ToDense(pad_value=-1, mask=True)(input_data)
    self.assertTrue(hasattr(output, "_keras_mask"))
    self.assertIsNot(output._keras_mask, None)
    self.assertAllEqual(K.get_value(output._keras_mask), expected_mask) 
Example #12
Source Project: multi-label-classification   Author: zheng-yuwei   File: radam.py    License: MIT License 5 votes vote down vote up
def get_config(self):
        config = {
            'lr': float(K.get_value(self.lr)),
            'beta_1': float(K.get_value(self.beta_1)),
            'beta_2': float(K.get_value(self.beta_2)),
            'decay': float(K.get_value(self.decay)),
            'epsilon': self.epsilon,
            'amsgrad': self.amsgrad
        }
        base_config = super(RAdam, self).get_config()
        return dict(list(base_config.items()) + list(config.items())) 
Example #13
Source Project: keras_imagenet   Author: jkjung-avt   File: adamw.py    License: MIT License 5 votes vote down vote up
def get_config(self):
        config = {'lr': float(K.get_value(self.lr)),
                  'beta_1': float(K.get_value(self.beta_1)),
                  'beta_2': float(K.get_value(self.beta_2)),
                  'decay': float(K.get_value(self.decay)),
                  'weight_decay': float(K.get_value(self.wd)),
                  'epsilon': self.epsilon}
        base_config = super(AdamW, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))