📜  tensorflow tf.constant (1)

📅  最后修改于: 2023-12-03 14:47:54.182000             🧑  作者: Mango

TensorFlow之tf.constant介绍

简介

TensorFlow是一个强大的开源深度学习框架,其中tf.constant是一个非常重要的常量张量操作,其返回一个常量值的张量。

语法
tf.constant(
    value,
    dtype=None,
    shape=None,
    name='Const',
    verify_shape=False
)

参数介绍:

  • value: 常量的值,可以是数值,字符串,布尔类型等。

  • dtype: 张量类型,默认为图形推断出的类型。

  • shape: 张量维度列表。

  • name: 张量名称。

  • verify_shape: 是否开启形状校验,默认为False。

例子

下面是一个简单的例子,展示如何使用tf.constant来创建一个常量张量:

import tensorflow as tf

# 创建一个形状为[2, 3]的二维常量张量
a = tf.constant([[1, 2, 3], [4, 5, 6]])

# 创建一个标量常量张量
b = tf.constant(10)

with tf.Session() as sess:
    print(sess.run(a))
    print(sess.run(b))

输出结果:

[[1 2 3]
 [4 5 6]]
10
总结

tf.constant是TensorFlow中用于创建常量张量的操作之一,它非常容易使用,并且可以根据需要设置形状、类型和名称等参数。在实际应用中,它可以用于创建许多不同形状和类型的常量张量,为我们的深度学习模型提供有力的支持。