TensorFlow之tf.nn.bias_add()以及如何看张量的行列通道

博客介绍了三维张量的行、列、通道概念,如[2,3,2]张量,第一维代表行,第二维代表列,第三维代表通道。还讲解了tf.nn.bias_add()函数,它会将偏置加到图片张量上,偏置维度需和图片第4维相同,并给出示例代码展示其效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://blog.youkuaiyun.com/qq_37174526/article/details/80905693

张量的行、列、通道

[[[1  4]
  [2  5]
  [3  6]]

  [[7 10]
   [8 11]
   [9 12]]]

上图是一个三维张量[2,3,2],第一个维度是2,于是最外面的中括号里面套了2个子张量,也就是2个3*2的张量,我们把其中一个提取出来。

[[1  4]
 [2  5]
 [3  6]]

它是一个3*2的张量,按照上面说的方法,最外面的中括号里面套了3个子张量,也就是3个2维的向量。
对于一个三维张量来说,第一维代表行(axis=0),第二维代表列(axis=1),第三维代表通道(axis=2)。
也就是说,对于上面那个例子,去掉最外面那个括号后的两个子张量就是两行,如下:

[[1  4]
 [2  5]                     
 [3  6]][[7 10]
 [8 11]
 [9 12]]

再去掉一个子张量的最外面一个括号,就是3列:

[1  4]
[2  5]
[3  6]

其中每一列又有2个通道。比如4这个数字的位置就是第1行第1列第2通道。

tf.nn.bias_add()

图片的张量一般是4维的,比如[2,3,3,2]表示2张3x3的2通道图片,tf.nn.bias_add()会将偏置加到3x3的图片上(第一维不变),biases的维度要和图片第4维相同(也就是有几个卷积核,就有几个biases)
示例代码:

import tensorflow as tf
import numpy as np

#定义一个[2,3,3,2]图片张量,表示2张3x3的2通道图片
a = tf.Variable(tf.zeros([2,3,3,2]))
#bias向量有两个元素,表示加到不同通道上
b = tf.constant([1,-1],dtype=tf.float32)
add = tf.nn.bias_add(a,b)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    print(sess.run(a))
    print(sess.run(add))

输出a:

[[[[0. 0.]
   [0. 0.]
   [0. 0.]]

  [[0. 0.]
   [0. 0.]
   [0. 0.]]

  [[0. 0.]
   [0. 0.]
   [0. 0.]]]


 [[[0. 0.]
   [0. 0.]
   [0. 0.]]

  [[0. 0.]
   [0. 0.]
   [0. 0.]]

  [[0. 0.]
   [0. 0.]
   [0. 0.]]]]

输出add:

[[[[ 1. -1.]
   [ 1. -1.]
   [ 1. -1.]]

  [[ 1. -1.]
   [ 1. -1.]
   [ 1. -1.]]

  [[ 1. -1.]
   [ 1. -1.]
   [ 1. -1.]]]


 [[[ 1. -1.]
   [ 1. -1.]
   [ 1. -1.]]

  [[ 1. -1.]
   [ 1. -1.]
   [ 1. -1.]]

  [[ 1. -1.]
   [ 1. -1.]
   [ 1. -1.]]]]

可以看到,1分别加到了第一张和第二张图片的第一通道上,-1分别加到了第一张和第二张图片的第二通道上。

按照TensorFlow2.11的写法修改这段代码:“class tgcnCell(RNN): """Temporal Graph Convolutional Network """ def call(self, inputs, **kwargs): pass def __init__(self, num_units, adj, num_nodes, input_size=None, act=tf.nn.tanh, reuse=None): super(tgcnCell, self).__init__(units=num_units,_reuse=reuse) self._act = act self._nodes = num_nodes self._units = num_units self._adj = [] self._adj.append(calculate_laplacian(adj)) @property def state_size(self): return self._nodes * self._units @property def output_size(self): return self._units def __call__(self, inputs, state, scope=None): with tf.variable_scope(scope or "tgcn"): with tf.variable_scope("gates"): value = tf.nn.sigmoid( self._gc(inputs, state, 2 * self._units, bias=1.0, scope=scope)) r, u = tf.split(value=value, num_or_size_splits=2, axis=1) with tf.variable_scope("candidate"): r_state = r * state c = self._act(self._gc(inputs, r_state, self._units, scope=scope)) new_h = u * state + (1 - u) * c return new_h, new_h def _gc(self, inputs, state, output_size, bias=0.0, scope=None): inputs = tf.expand_dims(inputs, 2) state = tf.reshape(state, (-1, self._nodes, self._units)) x_s = tf.concat([inputs, state], axis=2) input_size = x_s.get_shape()[2].value x0 = tf.transpose(x_s, perm=[1, 2, 0]) x0 = tf.reshape(x0, shape=[self._nodes, -1]) scope = tf.get_variable_scope() with tf.variable_scope(scope): for m in self._adj: x1 = tf.sparse_tensor_dense_matmul(m, x0) x = tf.reshape(x1, shape=[self._nodes, input_size,-1]) x = tf.transpose(x,perm=[2,0,1]) x = tf.reshape(x, shape=[-1, input_size]) weights = tf.get_variable( 'weights', [input_size, output_size], initializer=tf.contrib.layers.xavier_initializer()) x = tf.matmul(x, weights) # (batch_size * self._nodes, output_size) biases = tf.get_variable( "biases", [output_size], initializer=tf.constant_initializer(bias, dtype=tf.float32)) x = tf.nn.bias_add(x, biases) x = tf.reshape(x, shape=[-1, self._nodes, output_size]) x = tf.reshape(x, shape=[-1, self._nodes * output_size]) return x”
最新发布
04-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值