2323import tensorflow as tf
2424
2525
26+ def simple_scoped_fn (a , x ):
27+ """Simple function: (a, x) -> 2(x+a), but with "2" as a variable in scope."""
28+ with tf .variable_scope ("body" ):
29+ # Dummy variable, just to check that scoping works as intended.
30+ two = tf .get_variable ("two" , [], dtype = tf .int32 ,
31+ initializer = tf .constant_initializer (2 ))
32+ return tf .mul (tf .add (a , x ), two )
33+
34+
2635class FunctionalOpsTest (tf .test .TestCase ):
2736
2837 def testFoldl_Simple (self ):
@@ -36,6 +45,24 @@ def testFoldl_Simple(self):
3645 lambda a , x : tf .mul (tf .add (a , x ), 2 ), elems , initializer = 10 )
3746 self .assertAllEqual (880 , r .eval ())
3847
48+ def testFoldl_Scoped (self ):
49+ with self .test_session () as sess :
50+ with tf .variable_scope ("root" ) as varscope :
51+ elems = tf .constant ([1 , 2 , 3 , 4 , 5 , 6 ], name = "data" )
52+
53+ r = tf .foldl (simple_scoped_fn , elems )
54+ # Check that we have the one variable we asked for here.
55+ self .assertEqual (len (tf .trainable_variables ()), 1 )
56+ self .assertEqual (tf .trainable_variables ()[0 ].name , "root/body/two:0" )
57+ sess .run ([tf .initialize_all_variables ()])
58+ self .assertAllEqual (208 , r .eval ())
59+
60+ # Now let's reuse our single variable.
61+ varscope .reuse_variables ()
62+ r = tf .foldl (simple_scoped_fn , elems , initializer = 10 )
63+ self .assertEqual (len (tf .trainable_variables ()), 1 )
64+ self .assertAllEqual (880 , r .eval ())
65+
3966 def testFoldr_Simple (self ):
4067 with self .test_session ():
4168 elems = tf .constant ([1 , 2 , 3 , 4 , 5 , 6 ], name = "data" )
@@ -47,6 +74,24 @@ def testFoldr_Simple(self):
4774 lambda a , x : tf .mul (tf .add (a , x ), 2 ), elems , initializer = 10 )
4875 self .assertAllEqual (1282 , r .eval ())
4976
77+ def testFoldr_Scoped (self ):
78+ with self .test_session () as sess :
79+ with tf .variable_scope ("root" ) as varscope :
80+ elems = tf .constant ([1 , 2 , 3 , 4 , 5 , 6 ], name = "data" )
81+
82+ r = tf .foldr (simple_scoped_fn , elems )
83+ # Check that we have the one variable we asked for here.
84+ self .assertEqual (len (tf .trainable_variables ()), 1 )
85+ self .assertEqual (tf .trainable_variables ()[0 ].name , "root/body/two:0" )
86+ sess .run ([tf .initialize_all_variables ()])
87+ self .assertAllEqual (450 , r .eval ())
88+
89+ # Now let's reuse our single variable.
90+ varscope .reuse_variables ()
91+ r = tf .foldr (simple_scoped_fn , elems , initializer = 10 )
92+ self .assertEqual (len (tf .trainable_variables ()), 1 )
93+ self .assertAllEqual (1282 , r .eval ())
94+
5095 def testFold_Grad (self ):
5196 with self .test_session ():
5297 elems = tf .constant ([1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ], name = "data" )
@@ -69,6 +114,34 @@ def testMap_Simple(self):
69114 r = tf .map_fn (lambda x : tf .mul (tf .add (x , 3 ), 2 ), elems )
70115 self .assertAllEqual (np .array ([(x + 3 ) * 2 for x in nums ]), r .eval ())
71116
117+ def testMap_Scoped (self ):
118+ with self .test_session () as sess :
119+
120+ def double_scoped (x ):
121+ """2x with a dummy 2 that is scoped."""
122+ with tf .variable_scope ("body" ):
123+ # Dummy variable, just to check that scoping works as intended.
124+ two = tf .get_variable ("two" , [], dtype = tf .int32 ,
125+ initializer = tf .constant_initializer (2 ))
126+ return tf .mul (x , two )
127+
128+ with tf .variable_scope ("root" ) as varscope :
129+ elems = tf .constant ([1 , 2 , 3 , 4 , 5 , 6 ], name = "data" )
130+ doubles = np .array ([2 * x for x in [1 , 2 , 3 , 4 , 5 , 6 ]])
131+
132+ r = tf .map_fn (double_scoped , elems )
133+ # Check that we have the one variable we asked for here.
134+ self .assertEqual (len (tf .trainable_variables ()), 1 )
135+ self .assertEqual (tf .trainable_variables ()[0 ].name , "root/body/two:0" )
136+ sess .run ([tf .initialize_all_variables ()])
137+ self .assertAllEqual (doubles , r .eval ())
138+
139+ # Now let's reuse our single variable.
140+ varscope .reuse_variables ()
141+ r = tf .map_fn (double_scoped , elems )
142+ self .assertEqual (len (tf .trainable_variables ()), 1 )
143+ self .assertAllEqual (doubles , r .eval ())
144+
72145 def testMap_SimpleNotTensor (self ):
73146 with self .test_session ():
74147 nums = [1 , 2 , 3 , 4 , 5 , 6 ]
@@ -87,6 +160,26 @@ def testScan_Simple(self):
87160 lambda a , x : tf .mul (a , x ), elems , initializer = v )
88161 self .assertAllEqual ([2. , 4. , 12. , 48. , 240. , 1440. ], r .eval ())
89162
163+ def testScan_Scoped (self ):
164+ with self .test_session () as sess :
165+ with tf .variable_scope ("root" ) as varscope :
166+ elems = tf .constant ([1 , 2 , 3 , 4 , 5 , 6 ], name = "data" )
167+
168+ r = tf .scan (simple_scoped_fn , elems )
169+ # Check that we have the one variable we asked for here.
170+ self .assertEqual (len (tf .trainable_variables ()), 1 )
171+ self .assertEqual (tf .trainable_variables ()[0 ].name , "root/body/two:0" )
172+ sess .run ([tf .initialize_all_variables ()])
173+ results = np .array ([1 , 6 , 18 , 44 , 98 , 208 ])
174+ self .assertAllEqual (results , r .eval ())
175+
176+ # Now let's reuse our single variable.
177+ varscope .reuse_variables ()
178+ r = tf .scan (simple_scoped_fn , elems , initializer = 2 )
179+ self .assertEqual (len (tf .trainable_variables ()), 1 )
180+ results = np .array ([6 , 16 , 38 , 84 , 178 , 368 ])
181+ self .assertAllEqual (results , r .eval ())
182+
90183 def testScan_Control (self ):
91184 with self .test_session () as sess :
92185 s = tf .placeholder (tf .float32 , shape = [None ])
0 commit comments