def testEvalExpr(self): x = symbolic.Symbol('x') y = symbolic.Symbol('y') xy = x * y # Without symbol-to-value map. self.assertEqual(xy, symbolic.ToStatic(xy)) self.assertEqual(xy, symbolic.ToTensor(xy)) with symbolic.SymbolToValueMap(symbolic.STATIC_VALUES, {x: 2, y: 3}): self.assertEqual(symbolic.ToStatic(xy), 6) # The inner map overrides the outer map. with symbolic.SymbolToValueMap(symbolic.STATIC_VALUES, {x: 5, y: 6}): self.assertEqual(symbolic.ToStatic(xy), 30) # Back to the outer map. self.assertEqual(symbolic.ToStatic(xy), 6) # EvalExpr can also evaluate a symbolic expression to a # Tensor. a = tf.placeholder(tf.float32) b = tf.placeholder(tf.float32) with symbolic.SymbolToValueMap(symbolic.TENSOR_VALUES, {x: a, y: b}): with symbolic.SymbolToValueMap(symbolic.STATIC_VALUES, {x: 2, y: 3}): # Value maps of different types do not affect each other. self.assertEqual(symbolic.ToStatic(xy), 6) ab = symbolic.ToTensor(xy) self.assertIsInstance(ab, tf.Tensor) with self.session() as sess: self.assertEqual(12, sess.run(ab, {a: 3, b: 4})) # EvalExpr supports partial evaluation. with symbolic.SymbolToValueMap(symbolic.STATIC_VALUES, {y: 3}): x3 = symbolic.ToStatic(xy) with symbolic.SymbolToValueMap(symbolic.STATIC_VALUES, {x: 9}): self.assertEqual(27, symbolic.ToStatic(x3))
def FProp(self, theta, inputs): """Apply projection to inputs. Args: theta: A NestedMap object containing weights' values of this layer and its children layers. inputs: The inputs tensor. Shaped [..., input_dims]. Returns: Projected inputs. """ p = self.params with tf.name_scope(p.name): computation_cost.Add( self, 'flops', tf.reduce_prod(tf.cast(tf.shape(inputs)[:-1], tf.int64)) * tf.cast(symbolic.ToTensor(p.input_dims * p.output_dims), tf.int64) * 2) return py_utils.ProjectLastDim(inputs, theta.w, p.input_dims, p.output_dims)