python - 在 tensorflow 中放大张量

标签 python tensorflow

我正在寻找一种 tensorflow Python方法来放大(调整大小)张量,以将每个特征图中的每个元素沿两个轴加倍,例如:

 ([[1, 2, 3],
   [4, 5, 6],
   [7, 8, 9]])

=>

 ([[1, 1, 2, 2, 3, 3],
   [1, 1, 2, 2, 3, 3],
   [4, 4, 5, 5, 6, 6],
   [4, 4, 5, 5, 6, 6],
   [7, 7, 8, 8, 9, 9],
   [7, 7, 8, 8, 9, 9]])

我看到了tf.tiletf.pad但我不知道如何使用此方法来获得该结果。

感谢您的任何提示!

更新:

感谢 sygi 提供的有用提示,这是使用 python3 内核在 jupyter 笔记本中工作的与形状无关的解决方案:

import tensorflow as tf
import numpy as np

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

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

k = np.array([[1, 2],
              [3, 4],
              [5, 6]])

a = tf.placeholder(tf.int64, shape=(None, None))
a_shape = tf.shape(a)

b = tf.reshape(a, [a_shape[0], a_shape[1], 1])
c = tf.tile(b, [1, 1, 2])
d = tf.reshape(c, [a_shape[0], a_shape[1]*2])

e = tf.reshape(d, [a_shape[0], a_shape[1]*2, 1])
f = tf.tile(e, [1, 1, 2])
g = tf.transpose(f, [0, 2, 1])

h = tf.reshape(g, [a_shape[0]*2, a_shape[1]*2])

session = tf.InteractiveSession()
session.run(tf.initialize_all_variables())

print(h.eval(feed_dict={a: i}))
print(h.eval(feed_dict={a: j}))
print(h.eval(feed_dict={a: k}))

session.close()

结果

[[1 1 2 2 3 3]
 [1 1 2 2 3 3]
 [4 4 5 5 6 6]
 [4 4 5 5 6 6]
 [7 7 8 8 9 9]
 [7 7 8 8 9 9]]

[[ 1  1  2  2  3  3  4  4]
 [ 1  1  2  2  3  3  4  4]
 [ 5  5  6  6  7  7  8  8]
 [ 5  5  6  6  7  7  8  8]
 [ 9  9 10 10 11 11 12 12]
 [ 9  9 10 10 11 11 12 12]]

[[1 1 2 2]
 [1 1 2 2]
 [3 3 4 4]
 [3 3 4 4]
 [5 5 6 6]
 [5 5 6 6]]

最佳答案

a = tf.convert_to_tensor([[1, 2, 3],
                          [4, 5, 6],
                          [7, 8, 9]])
b = tf.reshape(a, [3, 3, 1])
c = tf.tile(b, [1, 1, 2])
d = tf.reshape(c, [3, 6])
print(d.eval())
array([[1, 1, 2, 2, 3, 3],
       [4, 4, 5, 5, 6, 6],
       [7, 7, 8, 8, 9, 9]], dtype=int32)

e = tf.reshape(d, [3, 6, 2])
f = tf.tile(e, [1, 1, 2])
g = tf.transpose(f, [0, 2, 1])
print(g.eval())
array([[[1, 1, 2, 2, 3, 3],
        [1, 1, 2, 2, 3, 3]],

       [[4, 4, 5, 5, 6, 6],
        [4, 4, 5, 5, 6, 6]],

       [[7, 7, 8, 8, 9, 9],
        [7, 7, 8, 8, 9, 9]]], dtype=int32)

h = tf.reshape(g, [6, 6])
print(h.eval())
array([[1, 1, 2, 2, 3, 3],
       [1, 1, 2, 2, 3, 3],
       [4, 4, 5, 5, 6, 6],
       [4, 4, 5, 5, 6, 6],
       [7, 7, 8, 8, 9, 9],
       [7, 7, 8, 8, 9, 9]], dtype=int32)

您可以使用以下方法获取a 张量(如果已定义)的形状:

shape = a.get_shape().as_list()

关于python - 在 tensorflow 中放大张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40608163/

相关文章:

python - 如何在 Tensorflow 中进行列求和?

python - TensorFlow - 返回多维张量的不同子张量

python - 使用 python 解析 CSV 文件并转换为 Pandas Dataframe 以绘制 Django 模板的图形

python - 在不区分大小写的字符串 find 之前和之后插入

python - 如何获得一个并不总是存在的捕获组?

python - 如何在Python click模块生成的使用消息末尾添加多个空行?

python - 为什么我的 tensorflow 的 model.evaluate() 打印成这样

python - 仅读取 CSV 文件中的一个随机行并移动到另一个 CSV

tensorflow - 带有 tensorflow 的 keras 运行良好,直到我添加回调

python - Tensorflow 运行时字典查找,处理 tf.parse_single_sequence_example 的输出