访问量: 10 次浏览
tensorflow.constant()TensorFlow是谷歌设计的开源Python库,用于开发机器学习模型和深度学习神经网络。
constant()用于从类似张量的对象中创建张量,如list。
语法:
tensorflow.constant( value, dtype, shape, name )
参数:
value : 它是需要被转换为张量的值。dtype(可选): 它定义了输出张量的类型。shape(可选): 它定义了输出张量的维度。name(optiona): 它定义了操作的名称。返回:它返回一个张量。
例子1:来自Python列表
# Importing the library
import tensorflow as tf
# Initializing the input
l = [1, 2, 3, 4]
# Printing the input
print('l: ', l)
# Calculating result
x = tf.constant(l)
# Printing the result
print('x: ', x)
输出:
l: [1, 2, 3, 4]
x: tf.Tensor([1 2 3 4], shape=(4, ), dtype=int32)
例子2:来自Python元组
# Importing the library
import tensorflow as tf
# Initializing the input
l = (1, 2, 3, 4)
# Printing the input
print('l: ', l)
# Calculating result
x = tf.constant(l, dtype = tf.float64)
# Printing the result
print('x: ', x)
输出:
l: (1, 2, 3, 4)
x: tf.Tensor([1. 2. 3. 4.], shape=(4, ), dtype=float64)