TensorFlow guarantee_const () 函数:保证张量为常量的使用教程


发布日期 : 2020-10-29 09:25:41 UTC

访问量: 10 次浏览

Python – tensorflow.guarantee\_const()

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

guarantee\_const()
用于保证TensorFlow运行时输入张量是恒定的。

语法:
tensorflow.guarantee_const( input, name)

参数:

  • input : 它是一个张量。
  • name (可选):它定义了该操作的名称

返回:
它返回一个与输入张量相同的张量。

示例 1:

# Importing the library
import tensorflow as tf

# Initializing the Tensor
x = tf.guarantee_const(5)

# Printing the result
print("x: ", x)

输出:

x: tf.Tensor(5, shape=(), dtype=int32)

示例 2:

# Importing the library
import tensorflow as tf

# Initializing the Tensor
x = tf.Variable(2.0, name ="x")
z = tf.Variable(4.0, name ="z")

# Using guarantee_const
y = tf.guarantee_const([x, z])

# Printing the result
print("y: ", y)

输出:

y: tf.Tensor([2. 4.], shape=(2, ), dtype=float32)