gpt4 book ai didi

python - “MultiRNNCell”对象不可迭代 Python Tensorflow

转载 作者:行者123 更新时间:2023-12-01 01:38:12 25 4
gpt4 key购买 nike

我正在尝试根据层将权重和偏差添加到张量板。我尝试过以下方法:

tf.reset_default_graph()

X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.float32, [None,n_outputs])


layers = [tf.contrib.rnn.LSTMCell(num_units=n_neurons,
activation=tf.nn.leaky_relu, use_peepholes = True)
for layer in range(n_layers)]
# for i, layer in enumerate(layers):
# tf.summary.histogram('layer{0}'.format(i), tf.convert_to_tensor(layer))


multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers)
for index,one_lstm_cell in enumerate(multi_layer_cell):
one_kernel, one_bias = one_lstm_cell.variables
# I think TensorBoard handles summaries with the same name fine.
tf.summary.histogram("Kernel", one_kernel)
tf.summary.histogram("Bias", one_bias)
rnn_outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32)

stacked_rnn_outputs = tf.reshape(rnn_outputs, [-1, n_neurons])
stacked_outputs = tf.layers.dense(stacked_rnn_outputs, n_outputs)
outputs = tf.reshape(stacked_outputs, [-1, n_steps, n_outputs])
outputs = outputs[:,n_steps-1,:] # keep only last output of sequence

但我收到以下错误:

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-43-761df6e116a7> in <module>()
44
45 multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers)
---> 46 for index,one_lstm_cell in enumerate(multi_layer_cell):
47 one_kernel, one_bias = one_lstm_cell.variables
48 # I think TensorBoard handles summaries with the same name fine.

TypeError: 'MultiRNNCell' object is not iterable

我想知道我错过了什么,以便我可以在张量板中添加用于可视化的变量。请帮助我。

最佳答案

MultiRNNCell确实是不可迭代的。对于您的情况,首先,在您调用 tf.nn.dynamic_rnn 之前,不会创建 RNN 变量。 ,因此您应该尝试在此之后检索它们。其次,使用 use_peephole ,您拥有的不仅仅是内核变量和偏差变量。要检索它们,您可以从 multi_layer_cell.variables 访问所有 RNN 变量,或者通过存储在 layers 中的单元对象访问每个层自己的集合:

multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers)
rnn_outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32)
for index, one_lstm_cell in enumerate(layers):
one_kernel, one_bias, *one_peepholes = one_lstm_cell.variables
tf.summary.histogram("Kernel", one_kernel)
tf.summary.histogram("Bias", one_bias)

关于python - “MultiRNNCell”对象不可迭代 Python Tensorflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52182255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com