Python TensorFlow ``get_static_value()`` 函数与静态值提取实例


发布日期 : 2021-06-23 09:57:52 UTC

访问量: 9 次浏览

Python – tensorflow.get\_static\_value()

TensorFlow是谷歌设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

get\_static\_value()是用来计算张量的静态值。如果静态值不能被计算出来,它将返回无。

语法:
tensorflow.get_static_value(tensor, partial)

参数:

  • tensor: 它是需要计算其静态值的输入张量。
  • partial: 要么是True,要么是False,默认值为False。它被设置为True,允许返回的数组有部分计算值。

返回:
它返回一个包含计算结果的
numpy
ndarray。

示例 1:

# Importing the library
import tensorflow as tf

# Initializing the input
data = tf.constant([[1, 2, 3], [3, 4, 5], [5, 6, 7]])

# Printing the input
print('data: ',data)

# Calculating result
res = tf.get_static_value(data)

# Printing the result
print('res: ',res)

输出:

data: tf.Tensor(
[[1 2 3]
[3 4 5]
[5 6 7]], shape=(3, 3), dtype=int32)
res: [[1 2 3]
[3 4 5]
[5 6 7]]

示例 2:

# Importing the library
import tensorflow as tf

# Initializing the input
data = tf.Variable([[1, 2, 3], [3, 4, 5], [5, 6, 7]])

# Printing the input
print('data: ',data)

# Calculating result
res = tf.get_static_value(data)

# Printing the result
print('res: ',res)

输出:

data: <tf.Variable 'Variable:0' shape=(3, 3) dtype=int32, numpy=
array([[1, 2, 3],
[3, 4, 5],
[5, 6, 7]], dtype=int32)>
res: None