使用到了tensorflow中的DataFormatVecPermute()算子,他有四个形参,请问这四个形参该怎么设置?
以下是我写的代码:
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
a = tf.constant([1, 2, 3, 4], name='a')
sess = tf.compat.v1.Session()
print(sess.run(a))
y = tf.raw_ops.DataFormatVecPermute(a, 'NHWC', 'NCHW', name='None')
print(y)
目的是将x由‘NHWC'格式转为‘NCHW’格式
产生的错误如下:
TypeError: DataFormatVecPermute only takes keyword args (possible keys: ['x', 'src_format', 'dst_format', 'name']). Please pass these args as kwargs instead.
DataFormatVecPermute()算子是 TensorFlow 中的向量转置算子,它有四个参数,分别是
input、
perm、
type_t和
N`。
input
:表示要转置的张量,数据类型为 Tensor
。perm
:表示转置的维度顺序,数据类型为 Tensor
,长度必须与 input
张量的维度相同,每个元素表示在转置后的维度顺序中,原来的这个维度在哪个位置,比如 perm=[1, 0, 2]
表示将原来的二维张量按照列优先的方式转置。type_t
:表示转置后的张量的数据类型,数据类型为 tf.DType
,默认与 input
张量的数据类型相同。N
:表示转置后的张量的阶数,数据类型为整数,取值范围为 [2, 8]
,默认为 input
张量的阶数。例如,假设你有一个二维张量 input
,要将其转置为行优先的顺序,代码如下:
import tensorflow as tf
input = tf.constant([[1, 2, 3], [4, 5, 6]])
perm = tf.constant([1, 0])
output = tf.raw_ops.DataFormatVecPermute(input=input, perm=perm, type_t=None, N=None)
print(output)
输出:
tf.Tensor(
[[1 4]
[2 5]
[3 6]], shape=(3, 2), dtype=int32)
其中,output
表示转置后的张量。