Tensorflow python:将输入 [batchsize] 重塑为具有特定顺序的张量 [batchsize, 2]

编程入门 行业动态 更新时间:2024-10-09 03:25:58
本文介绍了Tensorflow python:将输入 [batchsize] 重塑为具有特定顺序的张量 [batchsize, 2]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-05-01 05:50:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405380.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:张量   顺序   Tensorflow   python   batchsize

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!