问题描述
限时送ChatGPT账号..我有一个张量(shape=[batchsize]).我想以特定顺序将张量重塑为 shape=[-1,2].但我想要:
I have a tensor (shape=[batchsize]). I want to reshape the tensor in a specific order and into shape=[-1,2]. But I want to have:
[0,0] 处的元素[1,0] 处的元素[0,1] 处的元素[1,1] 处的元素[0,2] 处的元素[0,3] 处的元素[2,1] 处的元素[3,1] 处的元素等,用于未知批量大小.这是一个张量范围=(0到输入=8)的示例代码.
Here is an example code with a tensor range=(0 to input=8).
import tensorflow as tf
import numpy as np
batchsize = tf.placeholder(shape=[], dtype=tf.int32)
x = tf.range(0, batchsize, 1)
x = tf.reshape(x, shape=[2, -1])
y = tf.transpose(x)
z = tf.reshape(y, shape=[-1, 2])
input = 8
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
msg = sess.run([z], feed_dict={batchsize: input})
print(msg)
现在我的输出是:
[array([[0, 4],
[1, 5],
[2, 6],
[3, 7]], dtype=int32)]
但我希望输出是:
[array([[0, 2],
[1, 3],
[4, 6],
[5, 7]], dtype=int32)]
请记住,我不知道 batchsize 有多大,我只是出于示范原因设置 input=8.此外,在这里我想在每个第二个元素之后打破顺序.将来我也想拥有这种灵活性.在我的真实代码中,张量x"不是范围数组,而是复杂的随机数,因此您无法以任何方式进行排序 w.r.t.价值.我只是为了演示目的制作了这段代码.
Keep in mind I do not know how big batchsize is, I just set input= 8 for exemplary reason. Furthermore here I want to break the order after every 2nd element. In future I also would like to have this flexible. And in my real code the tensor ´x´ is no range array but complex random numbers, so you cannot sort in any way w.r.t. the values. I just made this code for a demonstration purpose.
推荐答案
你可以试试
tf.reshape(tf.matrix_transpose(tf.reshape(x, [-1, 2, 2])), [-1, 2])
这篇关于Tensorflow python:将输入 [batchsize] 重塑为具有特定顺序的张量 [batchsize, 2]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论