【问题标题】:tensorflow - resize dimensiontensorflow - 调整尺寸
【发布时间】:2017-10-13 17:29:26
【问题描述】:

我是 tensorflow 的新手,想知道是否可以在张量中调整单个维度的大小。

让我有一个给定的张量 t:

t = [[1, 10], [2, 20]]
shape(t) = [2, 2]

现在我想修改这个张量的形状,这样:

shape(t) = [2, 3]

到目前为止,我只是找到了功能:

  • reshape --> 这个函数能够以这样一种方式重塑张量,使总维数保持不变(据我所知)

    shape(t) = [1, 3] | [3, 1] | [4]
    
  • expand_dims --> 这个函数可以添加一个新的一维维度

    shape(t) = [1, 2, 2] | [2, 1, 2] | [2, 2, 1]
    

用于我描述的目的的功能是否到位?如果不是:为什么? (也许有这样的功能没有意义?)

亲切的问候

【问题讨论】:

  • 从 (2,2) 到 (2,3),张量中还有两个元素。它们应该来自哪里或应该如何计算?
  • 您能否描述一下您这样做的目的是什么,以及哪些数据应该填充额外的空间?如果你想用零填充额外的空间,你可以像这样使用 tf.pad:tf.pad(t, [[0, 0], [0, 1]])
  • 谢谢,tf.pad 是我搜索的那个。

标签: tensorflow tensor


【解决方案1】:

使用tf.concat 可以做到。这是一个例子。

import tensorflow as tf

t = tf.constant([[1, 10], [2, 20]], dtype=tf.int32)
# the new tensor w/ the shape of [2]
TBA_a = tf.constant([3,30], dtype=tf.int32)
# reshape TBA_a to [2,1], then concat it to t on axis 1 (column)
new_t = tf.concat([t, tf.reshape(TBA_a, [2,1])], axis=1)

sess = tf.InteractiveSession()
print(new_t.eval())

它会给我们

[[ 1 10  3]
 [ 2 20 30]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 2022-07-19
    • 1970-01-01
    相关资源
    最近更新 更多