张量的相关操作

学习目标:

  • 初始化 TensorFlow 变量并赋值
  • 创建和操控张量
  • 回忆线性代数中的加法和乘法知识(如果这些内容对您来说很陌生,请参阅矩阵加法乘法简介)
  • 熟悉基本的 TensorFlow 数学和数组运算


矢量加法

您可以对张量执行很多典型数学运算 (TF API)。以下代码会创建和操控两个矢量(一维张量),每个矢量正好六个元素:

with tf.Graph().as_default():
  # Create a six-element vector (1-D tensor).
  primes = tf.constant([2, 3, 5, 7, 11, 13], dtype=tf.int32)

  # Create another six-element vector. Each element in the vector will be
  # initialized to 1. The first argument is the shape of the tensor (more
  # on shapes below).
  ones = tf.ones([6], dtype=tf.int32)

  # Add the two vectors. The resulting tensor is a six-element vector.
  just_beyond_primes = tf.add(primes, ones)

  # Create a session to run the default graph.
  with tf.Session() as sess:

    print just_beyond_primes.eval()

张量形状

形状用于描述张量维度的大小和数量。张量的形状表示为列表,其中第 i 个元素表示维度 i 的大小。列表的长度表示张量的阶(即维数)。

有关详情,请参阅 TensorFlow 文档

以下是一些基本示例:

with tf.Graph().as_default():
  # A scalar (0-D tensor).
  scalar = tf.zeros([])

  # A vector with 3 elements.
  vector = tf.zeros([3])

  # A matrix with 2 rows and 3 columns.
  matrix = tf.zeros([2, 3])

  with tf.Session() as sess:
    print 'scalar has shape', scalar.get_shape(), 'and value:\n', scalar.eval()
    print 'vector has shape', vector.get_shape(), 'and value:\n', vector.eval()
    print 'matrix has shape', matrix.get_shape(), 'and value:\n', matrix.eval()

广播

在数学中,您只能对形状相同的张量执行元素级运算(例如,相加等于)。不过,在 TensorFlow 中,您可以对张量执行传统意义上不可行的运算。TensorFlow 支持广播(一种借鉴自 Numpy 的概念)。利用广播,元素级运算中的较小数组会增大到与较大数组具有相同的形状。例如,通过广播:

  • 如果指令需要大小为 [6] 的张量,则大小为 [1] 或 [] 的张量可以作为运算数。
  • 如果指令需要大小为 [4, 6] 的张量,则以下任何大小的张量都可以作为运算数。
    • [1, 6]
    • [6]
    • []

矩阵乘法

在线性代数中,当两个矩阵相乘时,第一个矩阵的数必须等于第二个矩阵的数。

  • 3x4 矩阵乘以 4x2 矩阵是有效的,可以得出一个 3x2 矩阵。
  • 4x2 矩阵乘以 3x4 矩阵是无效的。
with tf.Graph().as_default():
  # Create a matrix (2-d tensor) with 3 rows and 4 columns.
  x = tf.constant([[5, 2, 4, 3], [5, 1, 6, -2], [-1, 3, -1, -2]],
                  dtype=tf.int32)

  # Create a matrix with 4 rows and 2 columns.
  y = tf.constant([[2, 2], [3, 5], [4, 5], [1, 6]], dtype=tf.int32)

  # Multiply `x` by `y`.
  # The resulting matrix will have 3 rows and 2 columns.
  matrix_multiply_result = tf.matmul(x, y)

  with tf.Session() as sess:

    print matrix_multiply_result.eval()

张量变形

由于张量加法和矩阵乘法均对运算数施加了限制条件,TensorFlow 编程者肯定会频繁改变张量的形状。

您可以使用 tf.reshape 方法改变张量的形状。 例如,您可以将 8x2 张量变形为 2x8 张量或 4x4 张量:

with tf.Graph().as_default():
  # Create an 8x2 matrix (2-D tensor).
  matrix = tf.constant([[1,2], [3,4], [5,6], [7,8],
                        [9,10], [11,12], [13, 14], [15,16]], dtype=tf.int32)
  # Reshape the 8x2 matrix into a 2x8 matrix.
  reshaped_2x8_matrix = tf.reshape(matrix, [2,8])
  
  # Reshape the 8x2 matrix into a 4x4 matrix
  reshaped_4x4_matrix = tf.reshape(matrix, [4,4])
  with tf.Session() as sess:
    print "Original matrix (8x2):"
    print matrix.eval()
    print "Reshaped matrix (2x8):"
    print reshaped_2x8_matrix.eval()
    print "Reshaped matrix (4x4):"
    print reshaped_4x4_matrix.eval()

变量、初始化和赋值

到目前为止,我们执行的所有运算都是针对静态值 (tf.constant) 进行的;调用 eval() 始终返回同一结果。在 TensorFlow 中可以定义 Variable 对象,它的值是可以更改的。

创建变量时,您可以明确设置一个初始值,也可以使用初始化程序(例如分布):

g = tf.Graph()
with g.as_default():
  # Create a variable with the initial value 3.
  v = tf.Variable([3])
  # Create a variable of shape [1], with a random initial value,
  # sampled from a normal distribution with mean 1 and standard deviation 0.35.
  w = tf.Variable(tf.random_normal([1], mean=1.0, stddev=0.35))

要初始化变量,最简单的方式是调用 global_variables_initializer。请注意 Session.run() 的用法(与 eval() 的用法大致相同)

参考:谷歌机器学习速成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值