tensorflow(一)---张量

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_43546676/article/details/102609732

tensorflow----张量

一、张量
1.定义:

张量广义上表示任意形式的“数据0”.张量可以理解为0阶(rank)标量、1阶向量、二阶矩阵在高维空间的推广,张量的阶描述它表示数据的最大纬度。
张量和矢量有点类似,但是张量包含的信息比矢量的信息多,也可以理解为矢量的以一个推广。

2.张量的两个重要属性

(1)(相同的)数据类型
(2)矩阵的形状

3.tensorflow中常见的张量

(1)tf.constant 常量
(2)tf.placeholder 占位符
(3)tf.Variable 变量
解释:tf.constant就是里面的元素都是常数,已经确定了就无法改变。tf.placeholder就是只是定义了形状的躯壳,没有数据,需要后续填充数据。tf.Variable定义了形状,并且里面有值,后续值可以被覆盖。

注意:常量与占位符建立的张量,在一次迭代后就会将空间释放,而变量不会,变量是一种特殊的张量,他会一直放在内存中,直到运算完毕!

4.张量的建立

(1)零阶张量:

a = tf.Variable('aaa', tf.string)
b = tf.constant(1, tf.int32)
print(a)
print(b)

# 结果:
<tf.Variable 'Variable:0' shape=() dtype=string_ref>
Tensor("Const:0", shape=(), dtype=int32)

(2)一阶张量

a = tf.Variable(['aaa', 'a'], tf.string)
b = tf.constant([1, 2], tf.int32)
c = tf.placeholder(tf.float16, (3,))
print(a)
print(b)
print(c)

# 结果:
<tf.Variable 'Variable_1:0' shape=(2,) dtype=string_ref>
Tensor("Const_1:0", shape=(2,), dtype=int32)
Tensor("Placeholder:0", shape=(3,), dtype=float16)

(3)二阶张量

a = tf.Variable([['aaa'], ['a']], tf.string)
b = tf.constant([[1], [2]], tf.int32)
c = tf.placeholder(tf.float16, (3, 3))
print(a)
print(b)
print(c)

# 结果:
<tf.Variable 'Variable_2:0' shape=(2, 1) dtype=string_ref>
Tensor("Const_2:0", shape=(2, 1), dtype=int32)
Tensor("Placeholder_1:0", shape=(3, 3), dtype=float16)

猜你喜欢

转载自blog.csdn.net/qq_43546676/article/details/102609732