将tensorflow.Variable中的某些元素取出组成一个新的矩阵示例

 更新时间:2020年4月30日 12:07  点击:1324

在神经网络计算过程中,经常会遇到需要将矩阵中的某些元素取出并且单独进行计算的步骤(例如MLE,Attention等操作)。那么在 tensorflow 的 Variable 类型中如何做到这一点呢?

首先假设 Variable 是一个一维数组 A:

import numpy as np

import tensorflow as tf

a = np.array([1, 2, 3, 4, 5, 6, 7, 8])

A = tf.Variable(a)

我们把我们想取出的元素的索引存到 B 中,如果我们只想取出数组 A 中的某一个元素,则 B 的设定为:

b = np.array([3])

B = tf.placeholder(dtype=tf.int32, shape=[1])

由于我们的索引坐标只有一维,所以 shape=1。

取出元素然后组合成tensor C 的操作如下:

C = tf.gather_nd(A, B)

运行:

init = tf.global_variables_initializer()

with tf.Session() as sess:
  init.run()
  feed_dict = {B: b}
  result = sess.run([C], feed_dict=feed_dict)
  print result

得到:

[4]

如果我们想取出一维数组中的多个元素,则需要把每一个想取出的元素索引都单独放一行:

b = np.array([[3], [2], [5], [0]])

B = tf.placeholder(dtype=tf.int32, shape=[4, 1])

此时由于我们想要从一维数组中索引 4 个数,所以 shape=[4, 1]

再次运行得到:

[4 3 6 1]

////////////////////////////////////////////////////////////////////////////////////华丽丽的分割线

假设 Variable 是一个二维矩阵 A:

a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

A = tf.Variable(a)

首先我们先取出 A 中的一个元素,需要给定该元素的行列坐标,存到 B 中:

b = np.array([2,3])

B = tf.placeholder(dtype=tf.int32, shape=[2])

注意由于我们输入的索引坐标变成了二维,所以shape也变为2。

取出元素然后组合成tensor C:

C = tf.gather_nd(A, B)

运行:

init = tf.global_variables_initializer()

with tf.Session() as sess:
  init.run()
  feed_dict = {B: b}
  result = sess.run([C], feed_dict=feed_dict)
  print result

得到:

[12]

同样的,如果我们想取出二维矩阵中的多个元素,则需要把每一个想取出的元素的索引都单独放一行:

b = np.array([[2, 3], [1, 0], [2, 2], [0, 1]])

B = tf.placeholder(dtype=tf.int32, shape=[4, 2])

此时由于我们想要从二维矩阵中索引出 4 个数,所以 shape=[4, 2]

再次运行得到:

[12 5 11 2]

////////////////////////////////////////////////////////////////////////////////////华丽丽的分割线

推广到 n 维矩阵中:

假设 A 是 Variable 类型的 n 维矩阵,我们想取出矩阵中的 m 个元素,那么首先每个元素的索引坐标要表示成列表的形式:

index = [x1, x2, x3, ..., xn]

其中 xj 代表该元素在 n 维矩阵中第 j 维的位置。

其次每个坐标要单独占索引矩阵的一行:

index_matrix = [[x11, x12, x13, ..., x1n],

               [x21, x22, x23, ..., x2n],

               [x31, x32, x33, ..., x3n],

               .......................................,

               [xm1, xm2, xm3, ..., xmn]]

最后用 tf.gather_nd() 函数替换即可:

result = tf.gather_nd(A, index_matrix)

////////////////////////////////////////////////////////////////////////////////////华丽丽的分割线

[注] 问题出自:https://stackoverflow.com/questions/44793286/slicing-tensorflow-tensor-with-tensor

以上这篇将tensorflow.Variable中的某些元素取出组成一个新的矩阵示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章