tf.unstack\tf.unstack

tf.unstack

原型:

unstack(
value,
num=None,
axis=0,
name='unstack' )

官方解释:https://tensorflow.google.cn/api_docs/python/tf/unstack

解释:这是一个对矩阵进行分解的函数,以下为关键参数解释:

value:代表需要分解的矩阵变量(其实就是一个多维数组,一般为二维);

axis:指明对矩阵的哪个维度进行分解。

要理解tf.unstack函数,我们不妨先来看看tf.stack函数。Tf.stack刚好是与tf.unstack函数相反,前者是对矩阵进行拼接,后者则对矩阵进行分解。

Tf.stack用法举例:假如现在有两个变量,a=[1, 2, 3],b=[4, 5, 6],现在我要使用tf.stack对他们进行拼接,变成一个二维矩阵[ [1, 2, 3], [4, 5, 6] ]。代码【示例1】如下:

【示例1】



import tensorflow as tf

a = tf.constant([1, 2, 3]) b = tf.constant([4, 5, 6]) c = tf.stack( [a,b], axis=0) with tf.Session() as sess: print(sess.run(c)) 

输出结果是:



[[1 2 3]

 [4 5 6]] 

此时,我如果把【示例1】里面的tf.stack参数axis=0改成1,运行结果如下:


[[1 4]

 [2 5]

 [3 6]] 

可以理解,axis作用就是指明以何种方式对矩阵进行拼接,说白了,就是对原矩阵的哪个维度进行拼接。

理解了tf.stack,tf.unstack也就不难理解了。比如说现在有变量c,如下:

c=[[1 2 3]

[4 5 6]]

现在要对c进行分解,代码如下:



import tensorflow as tf

c = tf.constant([[1, 2, 3], [4, 5, 6]]) d = tf.unstack(c, axis=0) e = tf.unstack(c, axis=1) with tf.Session() as sess: print(sess.run(d)) print(sess.run(e)) 

结果如下:


[array([1, 2, 3]), array([4, 5, 6])] [array([1, 4]), array([2, 5]), array([3, 6])] 

可以看出来,tf.unstack其实就是在做与tf.stack相反的事情。这样一来,你是不是恍然大悟了呢?



作者:JempChou
链接:https://www.jianshu.com/p/25706575f8d4
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

转载于:https://www.cnblogs.com/fpzs/p/10290259.html

帮我整段修改一下,改成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、付费专栏及课程。

余额充值