tf.transpose()用法及参数 perm的使用原理

对于这个函数tf.transpose()这个函数 ,相信都不会陌生,刚刚上手的时候,比较 验证理解的相信是他的参数 perm[],里面究竟怎么取?
不绕沟子了,看看参数几何?

def transpose(a, perm=None, name="transpose"):

漂亮 ,就一个perm,,,,那怎么计算 呢?
举个粟子:

input_data.dims = (1, 4, 3)  #这是输入的维度
perm = [1, 2, 0]  # 这是perm取值

因为 output_data.dims[0] = input_data.dims[ perm[0] ]
因为 output_data.dims[1] = input_data.dims[ perm[1] ]
因为 output_data.dims[2] = input_data.dims[ perm[2] ]
所以得到 output_data.dims = (4, 3, 1)
output_data.dims = (4, 3, 1)
再举一下粟子,如下:
输入维度仍然是(1,4,3)
perm[2,1 0] # D to output :(3, 4, 1)
perm[1,0,2] # D to output is :(4, 1, 3)

裤子脱了一大半,就这样收尾,估计还没有到高潮。 主要的问题是如何实现转置的呢?
虽然知道 了输出的维度 ,那这有什么 用?
再举一个粟子进行说明 :

import tensorflow as tf
# 输入是维度是(2, 3, 4)的一个三维张量
x = [[[1, 2, 3, 4],
      [5, 6, 7, 8],
      [9, 10, 11, 12]],

     [[21, 22, 23, 24],
      [25, 26, 27, 28],
      [29, 30, 31, 32]]]
a = tf.transpose(x, [0, 1, 2])
b = tf.transpose(x, [0, 2, 1])
c = tf.transpose(x, [1, 0, 2])
d = tf.transpose(x, [1, 2, 0])
e = tf.transpose(x, [2, 1, 0])
f = tf.transpose(x, [2, 0, 1])

with tf.Session() as sess:
    print('a 0 1 2 ---------------')
    print(sess.run(a))
    print('b 0 2 1 ---------------')
    print(sess.run(b))
    print('c 1 0 2---------------')
    print(sess.run(c))
    print('d 1 2 0 ---------------')
    print(sess.run(d))
    print('e 2 1 0---------------')
    print(sess.run(e))
    print('f 2 0 1 ---------------')
    print(sess.run(f))

结果 如下:
我们就来看一下有没有 什么规律

a 0 1 2 --------------- #输入维度 (2,3,4)输出维度(2,3,4)原样输出 
[[[ 1  2  3  4]
  [ 5  6  7  8]
  [ 9 10 11 12]]

 [[21 22 23 24]
  [25 26 27 28]
  [29 30 31 32]]]
b 0 2 1 --------------- #输入维度 (2,3,4)输出维度(2, 4, 3)原样输出,直接把原行转置为列, 
[[[ 1  5  9]					# 可见 我们可以认为转置的条件是我们只调换行列参数 ,其他 不变 
  [ 2  6 10]
  [ 3  7 11]
  [ 4  8 12]]

 [[21 25 29]
  [22 26 30]
  [23 27 31]
  [24 28 32]]]
c 1 0 2---------------#输入维度 (2,3,4)输出维度(3, 2, 4),当行不变时,这相当于把行作为整体,
[[[ 1  2  3  4]			# 	并按原来 顺序依次放入各第三维中
  [21 22 23 24]]

 [[ 5  6  7  8]
  [25 26 27 28]]

 [[ 9 10 11 12]
  [29 30 31 32]]]
d 1 2 0 ---------------输入维度 (2,3,4)输出维度(3, 4, 2),原行未变,直接 搬至列
[[[ 1 21]
  [ 2 22]
  [ 3 23]
  [ 4 24]]

 [[ 5 25]
  [ 6 26]
  [ 7 27]
  [ 8 28]]

 [[ 9 29]
  [10 30]
  [11 31]
  [12 32]]]
e 2 1 0---------------后面的,说不一一说了
[[[ 1 21]
  [ 5 25]
  [ 9 29]]

 [[ 2 22]
  [ 6 26]
  [10 30]]

 [[ 3 23]
  [ 7 27]
  [11 31]]

 [[ 4 24]
  [ 8 28]
  [12 32]]]
f 2 0 1 ---------------
[[[ 1  5  9]
  [21 25 29]]

 [[ 2  6 10]
  [22 26 30]]

 [[ 3  7 11]
  [23 27 31]]

 [[ 4  8 12]
  [24 28 32]]]

通过上述 例子,可以 得到 一个结论,我们可以 理解 的,或者 使用的通常是维度较低的情况,更高维度的时候 ,使用比较困难。

帮我整段修改一下,改成TensorFlow2.11版本可用的:“ def TGCN(_X, _weights, _biases): ### cell_1 = tgcnCell(cell=custom_cell,gru_units=gru_units, adj=adj, num_nodes=num_nodes) cell = tf.nn.rnn_cell.MultiRNNCell([cell_1], state_is_tuple=True) _X = tf.unstack(_X, axis=1) outputs, states = tf.nn.static_rnn(cell, _X, dtype=tf.float32) m = [] for i in outputs: o = tf.reshape(i,shape=[-1,num_nodes,gru_units]) o = tf.reshape(o,shape=[-1,gru_units]) m.append(o) last_output = m[-1] output = tf.matmul(last_output, _weights['out']) + _biases['out'] output = tf.reshape(output,shape=[-1,num_nodes,pre_len]) output = tf.transpose(output, perm=[0,2,1]) output = tf.reshape(output, shape=[-1,num_nodes]) return output, m, states ###### placeholders ###### # 使用 tf.placeholder inputs = tf.compat.v1.placeholder(tf.float32, shape=[None, seq_len, num_nodes]) labels = tf.compat.v1.placeholder(tf.float32, shape=[None, pre_len, num_nodes]) # Graph weights weights = { 'out': tf.Variable(tf.random.normal([gru_units, pre_len], mean=1.0), name='weight_o')} biases = { 'out': tf.random.normal(shape=[pre_len],name='bias_o')} if model_name == 'tgcn': pred,ttts,ttto = TGCN(inputs, weights, biases) y_pred = pred ###### optimizer ###### lambda_loss = 0.0015 Lreg = lambda_loss * sum(tf.nn.l2_loss(tf_var) for tf_var in tf.trainable_variables()) label = tf.reshape(labels, [-1,num_nodes]) ##loss loss = tf.reduce_mean(tf.nn.l2_loss(y_pred-label) + Lreg) ##rmse error = tf.sqrt(tf.reduce_mean(tf.square(y_pred-label))) optimizer = tf.train.AdamOptimizer(lr).minimize(loss) ###### Initialize session ###### variables = tf.global_variables() saver = tf.train.Saver(tf.global_variables()) #sess = tf.Session() gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) sess.run(tf.global_variables_initializer())”
最新发布
04-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值