from tensorflow.python.framework import function as tf_function
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_shape
+from tensorflow.python.layers import convolutional
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import clip_ops
from tensorflow.python.ops import control_flow_ops
+from tensorflow.python.ops import init_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import resource_variable_ops
from tensorflow.python.ops import variable_scope
matmul = function.defun(math_ops.matmul)
pair = collections.namedtuple('pair', ['a', 'b'])
+
def a_times_b(inputs):
return matmul(inputs.a['a'], inputs.b['b'])
x = variable_scope.get_variable(
'v', initializer=constant_op.constant(1.0))
return x * constant_op.constant(2.0)
+
with self.assertRaisesRegexp(ValueError,
'No trainable variables were accessed'):
backprop.implicit_val_and_grad(f)()
with ops.name_scope('foo'):
v = resource_variable_ops.ResourceVariable(0.0, name='bar')
self.assertEqual(v.name, 'foo/bar:0')
+
create_variable()
def testVariableNamesRespectNameScopesWithDefunInGraph(self):
with ops.name_scope('foo'):
v = resource_variable_ops.ResourceVariable([1.0, 2.0], name='bar')
self.assertEqual(v.name, 'foo/bar:0')
+
with ops.get_default_graph().as_default():
create_variable()
+ def testLayerInDefun(self):
+ conv = convolutional.Conv2D(
+ filters=1,
+ kernel_size=2,
+ kernel_initializer=init_ops.ones_initializer(),
+ bias_initializer=init_ops.zeros_initializer())
+
+ @function.defun
+ def model(x):
+ return conv(x)
+
+ x = array_ops.ones([1, 2, 2, 1])
+ y = model(x)
+ self.assertAllEqual([[[[4.0]]]], y.numpy())
+
class AutomaticControlDependenciesTest(test.TestCase):