Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/util/variable_utils.py: 37%

19 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

1# Copyright 2022 The TensorFlow Authors. All Rights Reserved. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14# ============================================================================== 

15"""Utility to manipulate resource variables.""" 

16 

17from tensorflow.python.framework import composite_tensor 

18from tensorflow.python.framework import ops 

19from tensorflow.python.util import _pywrap_utils 

20from tensorflow.python.util import nest 

21 

22 

23def convert_variables_to_tensors(values): 

24 """Converts `ResourceVariable`s in `values` to `Tensor`s. 

25 

26 If an object is a `CompositeTensor` and overrides its 

27 `_convert_variables_to_tensors` method, its `ResourceVariable` components 

28 will also be converted to `Tensor`s. Objects other than `ResourceVariable`s 

29 in `values` will be returned unchanged. 

30 

31 Args: 

32 values: A nested structure of `ResourceVariable`s, or any other objects. 

33 

34 Returns: 

35 A new structure with `ResourceVariable`s in `values` converted to `Tensor`s. 

36 """ 

37 def _convert_resource_variable_to_tensor(x): 

38 if _pywrap_utils.IsResourceVariable(x): 

39 return ops.convert_to_tensor(x) 

40 elif isinstance(x, composite_tensor.CompositeTensor): 

41 return composite_tensor.convert_variables_to_tensors(x) 

42 else: 

43 return x 

44 

45 return nest.map_structure(_convert_resource_variable_to_tensor, values) 

46 

47 

48def replace_variables_with_atoms(values): 

49 """Replaces `ResourceVariable`s in `values` with tf.nest atoms. 

50 

51 This function is mostly for backward compatibility. Historically, 

52 `ResourceVariable`s are treated as tf.nest atoms. This is no 

53 longer the case after `ResourceVariable` becoming `CompositeTensor`. 

54 Unfortunately, tf.nest doesn't allow customization of what objects 

55 are treated as atoms. Calling this function to manually convert 

56 `ResourceVariable`s to atoms to avoid breaking tf.assert_same_structure 

57 with inputs of a `ResourceVariable` and an atom, like a `Tensor`. 

58 

59 The specific implementation uses 0 as the tf.nest atom, but other tf.nest 

60 atoms could also serve the purpose. Note, the `TypeSpec` of None is not a 

61 tf.nest atom. 

62 

63 Objects other than `ResourceVariable`s in `values` will be returned unchanged. 

64 

65 Note: this function does not look into `CompositeTensor`s. Replacing 

66 `ResourceVariable`s in a `CompositeTensor` with atoms will change the 

67 `TypeSpec` of the `CompositeTensor`, which violates the semantics of 

68 `CompositeTensor` and tf.nest. So `ResourceVariable`s in `CompositeTensor`s 

69 will be returned as they are. 

70 

71 Args: 

72 values: A nested structure of `ResourceVariable`s, or any other objects. 

73 

74 Returns: 

75 A new structure with `ResourceVariable`s in `values` converted to atoms. 

76 """ 

77 def _replace_resource_variable_with_atom(x): 

78 if _pywrap_utils.IsResourceVariable(x): 

79 return 0 # tf.nest treats 0 or tf.constant(0) as an atom. 

80 else: 

81 return x 

82 

83 return nest.map_structure(_replace_resource_variable_with_atom, values)