tf.reduce_mean() || tf.contrib.rnn.BasicRnnCell || tf.contrib.rnn.BasicLSTMCell || tf.reshape()

本文详细介绍了TensorFlow中tf.reduce_mean()与tf.reduce_max()函数的使用方法,并通过实例展示了如何进行多维度的数据处理。此外,还介绍了BasicRnnCell与BasicLSTMCell的基本用法及其参数设置。

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

tf.reduce_mean()

(或tf.reduce_max()一个是求平均值,一个是求最大值)

# 'x' is [[1., 2.]
#         [3., 4.]]

x是一个2维数组,分别调用reduce_*函数如下:

首先求平均值:

tf.reduce_mean(x) ==> 2.5 #如果不指定第二个参数,那么就在所有的元素中取平均值
tf.reduce_mean(x, 0) ==> [2.,  3.] #指定第二个参数为0,则第一维的元素取平均值,即每一列求平均值
tf.reduce_mean(x, 1) ==> [1.5,  3.5] #指定第二个参数为1,则第二维的元素取平均值,即每一行求平均值

同理,还可用tf.reduce_max()求最大值等

 

tf.contrib.rnn.BasicRnnCell

BasicRNNCell是最基本的RNN cell单元。 
输入参数:
num_units:RNN层神经元的个数 
input_size(该参数已被弃用) 
activation: 内部状态之间的激活函数 
reuse: Python布尔值, 描述是否重用现有作用域中的变量

 

 

tf.contrib.rnn.BasicLSTMCell

BasicLSTMCell类是最基本的LSTM循环神经网络单元。 
输入参数:
num_units: LSTM cell层中的单元数 
forget_bias: forget gates中的偏置 
state_is_tuple: 还是设置为True吧, 返回 (c_state , m_state)的二元组 
activation: 状态之间转移的激活函数 
reuse: Python布尔值, 描述是否重用现有作用域中的变量

 

 

tf.reshape()

reshape即把矩阵的形状变一下
看一下例子:

tensor = tf.constant([1, 2, 3, 4, 5, 6, 7,8])

 sess.run(tf.initialize_all_variables())
#[1 2 3 4 5 6 7 8]

tensorReshape = tf.reshape(tensor,[2,4])
#[[1 2 3 4]
#[5 6 7 8]]

tensorReshape = tf.reshape(tensor,[1,2,4])
#[[[1 2 3 4]
#[5 6 7 8]]]

tensorReshape = tf.reshape(tensor,[-1,2,2])
#[[[1 2]
#[3 4]]

#[[5 6]
#[7 8]]]
#所以-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、付费专栏及课程。

余额充值