TensorFlow 基础(二)变量


TensorFlow 变量是用于表示程序处理的共享持久状态的推荐方法(the recommended way to represent shared, persistent state your program manipulates)。下面会介绍在 TensorFlow 中如何创建、更新和管理 tf.Variable 的实例。

变量通过 tf.Variable 类进行创建和跟踪。tf.Variable 表示张量,对它执行运算可以改变其值。利用特定运算可以读取和修改此张量的值。

import tensorflow as tf

Create a variable

创建变量时,需要提供一个初始值tf.Variable 与初始值的 dtype 相同:

my_tensor = tf.constant([[1.0, 2.0], [3.0, 4.0]])
my_variable = tf.Variable(my_tensor)

#  Variables can be all kinds of types, just like tensors
bool_variable = tf.Variable([False, False, False, True])
complex_variable = tf.Variable([5 + 4j, 6 + 1j])

变量与张量的定义方式和操作行为都十分相似。实际上,它们都是 tf.Tensor 支持的一种数据结构。变量也有 dtype 和形状,并且可以导出至 NumPy:

print("Shape: ", my_variable.shape)
print("DType: ", my_variable.dtype)
print("NumPy format: ", my_variable.numpy())
"""
Shape:  (2, 2)
DType:  <dtype: 'float32'>
NumPy format:  [[1. 2.]
 [3. 4.]]
"""

大部分张量运算在变量上也可以按预期运行:

print("A variable:", my_variable)
print("\nViewed as a tensor:", tf.convert_to_tensor(my_variable))
print("\nIndex of highest value:", tf.math.argmax(my_variable))
"""
A variable: <tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[1., 2.],
       [3., 4.]], dtype=float32)>

Viewed as a tensor: tf.Tensor(
[[1. 2.]
 [3. 4.]], shape=(2, 2), dtype=float32)

Index of highest value: tf.Tensor([1 1], shape=(2,), dtype=int64)
"""

变量无法重构形状tf.reshape 函数会创建一个新的张量:

tf.reshape(my_variable, [1, 4])
"""
<tf.Tensor: shape=(1, 4), dtype=float32, numpy=array([[1., 2., 3., 4.]], dtype=float32)>
"""

从上面的例子中,我们应该有了这样的概念:变量由张量提供支持。我们可以使用 tf.Variable.assign 方法重新分配张量,调用 assign 方法通常不会分配新张量,而会重用现有张量的内存。例如下面的例子中,变量 a 的 dtype 仍旧为 float32 而不是整型:

a = tf.Variable([2.0, 5.0])
a.assign([4, 8])
a
"""
<tf.Variable 'Variable:0' shape=(2,) dtype=float32, numpy=array([4., 8.], dtype=float32)>
"""

从现有变量创建新变量会复制支持张量。两个变量不能共享同一内存空间

a = tf.Variable([2.0, 5.0])
b = tf.Variable(a)
a.assign([4, 8])

print(a.numpy())
print(b.numpy())
"""
[4. 8.]
[2. 5.]
"""

Lifecycles, naming, and watching

在基于 Python 的 TensorFlow 中,tf.Variable 实例与其他 Python 对象的生命周期相同。如果没有对变量的引用,则会自动将其解除分配。

为了便于跟踪和调试,我们还可以对变量进行命名。两个变量可以使用相同的名称,但它们的支持张量是不同的

a = tf.Variable(my_tensor, name="Mars")
b = tf.Variable(my_tensor + 1, name="Mars")
a == b
"""
<tf.Tensor: shape=(2, 2), dtype=bool, numpy=
array([[False, False],
       [False, False]])>
"""

保存和加载模型时会保留变量名。默认情况下,模型中的变量会自动获得唯一变量名,我们一般不需要自行命名。


Placing variables and tensors

为了提高性能,TensorFlow 会尝试将张量和变量放在与其 dtype 兼容的最快设备上。这意味着如果有 GPU,那么大部分变量都会放置在 GPU 上。但我们也可以手动放置变量:

with tf.device('CPU:0'):
    
  # Create some tensors
    a = tf.Variable([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
    b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
    c = tf.matmul(a, b)

print(c)
"""
tf.Tensor(
[[22. 28.]
 [49. 64.]], shape=(2, 2), dtype=float32)
"""

我们还可以将变量或张量的位置设置在一个设备上,然后在另一个设备上执行运算。但这样会产生延迟,因为需要在两个设备之间复制数据。

with tf.device('CPU:0'):
    a = tf.Variable([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
    b = tf.Variable([[1.0, 2.0, 3.0]])

with tf.device('GPU:0'):
    k = a * b

print(k)
"""
tf.Tensor(
[[ 1.  4.  9.]
 [ 4. 10. 18.]], shape=(2, 3), dtype=float32)
"""

⭐:由于 tf.config.set_soft_device_placement 默认处于打开状态,所以,即使在没有 GPU 的设备上运行此代码,它也会运行,只不过乘法步骤会在 CPU 上执行。


References

TensorFlow 官方网站,https://tensorflow.google.cn/guide/variable#lifecycles_naming_and_watching.

猜你喜欢

转载自blog.csdn.net/myDarling_/article/details/128626142