自学内容网 自学内容网

tensorflow数据相关总结----学习笔记(四)

标量

标量由只有一个元素的张量表示。 下面的代码将实例化两个标量,并执行一些熟悉的算术运算,即加法、乘法、除法和指数。

import tensorflow as tf

x = tf.constant(3.0)
y = tf.constant(2.0)

print(x + y)
print(x * y)
print(x / y)
print(x ** y)
"""
tf.Tensor(5.0, shape=(), dtype=float32)
tf.Tensor(6.0, shape=(), dtype=float32)
tf.Tensor(1.5, shape=(), dtype=float32)
tf.Tensor(9.0, shape=(), dtype=float32)
"""

向量

向量可以被视为标量值组成的列表。 这些标量值被称为向量的元素(element)或分量(component)。 当向量表示数据集中的样本时,它们的值具有一定的现实意义。

import tensorflow as tf

x = tf.range(4)
print(x)
"""
tf.Tensor([0 1 2 3], shape=(4,), dtype=int32)
"""
# 可以通过下标引用向量的任一元素
x[n]

长度、维度和形状

向量只是一个数字数组,就像每个数组都有一个长度一样,每个向量也是如此。
形状(shape)是一个元素组,列出了张量沿每个轴的长度(维数)。 对于只有一个轴的张量,形状只有一个元素。
维度(dimension)这个词在不同上下文时往往会有不同的含义,这经常会使人感到困惑。 为了清楚起见,我们在此明确一下: 向量或轴的维度被用来表示向量或轴的长度,即向量或轴的元素数量。 然而,张量的维度用来表示张量具有的轴数。 在这个意义上,张量的某个轴的维数就是这个轴的长度。

import tensorflow as tf

x = tf.range(4)
print(len(x))
print(x.shape)
"""
4
(4,)
"""

矩阵

正如向量将标量从零阶推广到一阶,矩阵将向量从一阶推广到二阶。 矩阵,我们通常用粗体、大写字母来表示 (例如,XYZ), 在代码中表示为具有两个轴的张量。

import tensorflow as tf

A = tf.reshape(tf.range(20), (5, 4))
print(A)
"""
tf.Tensor(
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]], shape=(5, 4), dtype=int32)
"""
#可以通过下班获取矩阵中的元素
print(A[1][2])
"""
tf.Tensor(6, shape=(), dtype=int32)
"""
# 可以使用transpose进行矩阵转置,改变矩阵的行和列
print(tf.transpose(A))
"""
tf.Tensor(
[[ 0  4  8 12 16]
 [ 1  5  9 13 17]
 [ 2  6 10 14 18]
 [ 3  7 11 15 19]], shape=(4, 5), dtype=int32)
"""

张量

就像向量是标量的推广,矩阵是向量的推广一样,我们可以构建具有更多轴的数据结构。 张量(张量”指代数对象)是描述具有任意数量轴的n维数组的通用方法。

import tensorflow as tf

X = tf.reshape(tf.range(24), (2, 3, 4))
print(X)
"""
tf.Tensor(
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]], shape=(2, 3, 4), dtype=int32)
"""

将张量乘以或加上一个标量不会改变张量的形状,其中张量的每个元素都将与标量相加或相乘。

import tensorflow as tf

a = 2
X = tf.reshape(tf.range(24), (2, 3, 4))
print(a + X)
print((a * X))
"""
tf.Tensor(
[[[ 2  3  4  5]
  [ 6  7  8  9]
  [10 11 12 13]]

 [[14 15 16 17]
  [18 19 20 21]
  [22 23 24 25]]], shape=(2, 3, 4), dtype=int32)
tf.Tensor(
[[[ 0  2  4  6]
  [ 8 10 12 14]
  [16 18 20 22]]

 [[24 26 28 30]
  [32 34 36 38]
  [40 42 44 46]]], shape=(2, 3, 4), dtype=int32)
"""

降维

我们可以对任意张量进行的一个有用的操作是计算其元素的和。

import tensorflow as tf

x = tf.range(4, dtype=tf.float32)
print(x)
print(tf.reduce_sum(x))
"""
tf.Tensor([0. 1. 2. 3.], shape=(4,), dtype=float32)
tf.Tensor(6.0, shape=(), dtype=float32)
"""

默认情况下,调用求和函数会沿所有的轴降低张量的维度,使它变为一个标量。 我们还可以指定张量沿哪一个轴来通过求和降低维度。
指定axis=0。 由于输入矩阵沿0轴降维以生成输出向量,因此输入轴0的维数在输出形状中消失。

import tensorflow as tf

A = tf.reshape(tf.range(20, dtype=tf.float32), (5, 4))
A_sum_axis0 = tf.reduce_sum(A, axis=0)
print(A)
print(A_sum_axis0)
print(A_sum_axis0.shape)
"""
tf.Tensor(
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]
 [12. 13. 14. 15.]
 [16. 17. 18. 19.]], shape=(5, 4), dtype=float32)
tf.Tensor([40. 45. 50. 55.], shape=(4,), dtype=float32)
(4,)
"""

指定axis=1将通过汇总所有列的元素降维(轴1)。因此,输入轴1的维数在输出形状中消失。

import tensorflow as tf

A = tf.reshape(tf.range(20, dtype=tf.float32), (5, 4))
A_sum_axis0 = tf.reduce_sum(A, axis=1)
print(A)
print(A_sum_axis0)
print(A_sum_axis0.shape)
"""
tf.Tensor(
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]
 [12. 13. 14. 15.]
 [16. 17. 18. 19.]], shape=(5, 4), dtype=float32)
tf.Tensor([ 6. 22. 38. 54. 70.], shape=(5,), dtype=float32)
(5,)
"""

沿着行和列对矩阵求和,等价于对矩阵的所有元素进行求和。

import tensorflow as tf

A = tf.reshape(tf.range(20, dtype=tf.float32), (5, 4))
A_sum_axis = tf.reduce_sum(A, axis=[0, 1])  # 结果和tf.reduce_sum(A)相同
A_sum = tf.reduce_sum(A)
print(A_sum_axis)
print(A_sum)
"""
tf.Tensor(190.0, shape=(), dtype=float32)
"""

非降维求和

在使用函数计算总和时需要保持轴数不变。

import tensorflow as tf

A = tf.reshape(tf.range(20, dtype=tf.float32), (5, 4))
sum_A = tf.reduce_sum(A, axis=1, keepdims=True)
print(A)
print(sum_A)
"""
tf.Tensor(
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]
 [12. 13. 14. 15.]
 [16. 17. 18. 19.]], shape=(5, 4), dtype=float32)
tf.Tensor(
[[ 6.]
 [22.]
 [38.]
 [54.]
 [70.]], shape=(5, 1), dtype=float32)
"""

如果我们想沿某个轴计算A元素的累积总和, 比如axis=0(按行计算),可以调用cumsum函数。 此函数不会沿任何轴降低输入张量的维度。

import tensorflow as tf

A = tf.reshape(tf.range(20, dtype=tf.float32), (5, 4))
print(A)
print(tf.cumsum(A, axis=0))
"""
tf.Tensor(
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]
 [12. 13. 14. 15.]
 [16. 17. 18. 19.]], shape=(5, 4), dtype=float32)
tf.Tensor(
[[ 0.  1.  2.  3.]
 [ 4.  6.  8. 10.]
 [12. 15. 18. 21.]
 [24. 28. 32. 36.]
 [40. 45. 50. 55.]], shape=(5, 4), dtype=float32)
"""

原文地址:https://blog.csdn.net/zhangkexin_z/article/details/140687075

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!