Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python tensorflow中使用tf.summary.image显示RNN时出错_Python_Python 3.x_Tensorflow_Tensorboard - Fatal编程技术网

在python tensorflow中使用tf.summary.image显示RNN时出错

在python tensorflow中使用tf.summary.image显示RNN时出错,python,python-3.x,tensorflow,tensorboard,Python,Python 3.x,Tensorflow,Tensorboard,以下是我尝试过的: 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.le

以下是我尝试过的:

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)]
multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers)
rnn_outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32)
tf.summary.histogram("outputs", rnn_outputs)
tf.summary.image("RNN",rnn_outputs)
我得到以下错误:

InvalidArgumentError: Tensor must be 4-D with last dim 1, 3, or 4, not [55413,4,100]
     [[Node: RNN_1 = ImageSummary[T=DT_FLOAT, bad_color=Tensor<type: uint8 shape: [4] values: 255 0 0...>, max_images=3, _device="/job:localhost/replica:0/task:0/device:CPU:0"](RNN_1/tag, rnn/transpose_1)]]
InvalidArgumentError:张量必须为4-D,最后一个尺寸为1、3或4,而不是[55413,4100]
[[Node:RNN_1=ImageSummary[T=DT_FLOAT,bad_color=Tensor,max_images=3,_device=“/job:localhost/replica:0/task:0/device:CPU:0”](RNN_1/tag,RNN/transpose_1)]]

请帮助我在我尝试运行的LSTM模型中获得rnn的可视化。这将有助于更准确地理解LSTM在做什么。

您的RNN输出的
tf.summary.image的形状错误。张量应为四维,其尺寸由[批次尺寸、高度、宽度、通道]给出

在您的代码中,您正在使用
rnn_输出调用
tf.summary.image
,它的形状为[55413,4100]。假设您的图像大小为55413×100像素,并且每个像素包含4个通道(RGBA),我会使用
tf.reformate
rnn_输出整形为[155413100,4]。然后您应该能够无误地调用
tf.summary.image


我不认为我能帮助你想象RNN的运作,但当我学习RNN和LSTM时,我发现非常有用

您的RNN输出的
tf.summary.image
形状错误。张量应为四维,其尺寸由[批次尺寸、高度、宽度、通道]给出

在您的代码中,您正在使用
rnn_输出调用
tf.summary.image
,它的形状为[55413,4100]。假设您的图像大小为55413×100像素,并且每个像素包含4个通道(RGBA),我会使用
tf.reformate
rnn_输出整形为[155413100,4]。然后您应该能够无误地调用
tf.summary.image


我不认为我能帮助你想象RNN的运作,但当我学习RNN和LSTM时,我发现非常有用

您可以将每个RNN输出绘制为图像,其中一个轴为时间,另一个轴为输出。下面是一个小例子:

import tensorflow as tf
import numpy as np

n_steps = 100
n_inputs = 10
n_neurons = 10
n_layers = 3

x = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
layers = [tf.contrib.rnn.LSTMCell(num_units=n_neurons,
                                  activation=tf.nn.leaky_relu, use_peepholes=True)
         for layer in range(n_layers)]
multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers)
rnn_outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, x, dtype=tf.float32)
# Time steps in horizontal axis, outputs in vertical axis, add last dimension for channel
rnn_out_imgs = tf.transpose(rnn_outputs, (0, 2, 1))[..., tf.newaxis]
out_img_sum = tf.summary.image("RNN", rnn_out_imgs, max_outputs=10)
init_op = tf.global_variables_initializer()
with tf.Session() as sess, tf.summary.FileWriter('log') as fw:
    sess.run(init_op)
    fw.add_summary(sess.run(out_img_sum, feed_dict={x: np.random.rand(10, n_steps, n_inputs)}))
您将获得如下所示的可视化效果:


在这里,更亮的像素表示更强的激活,因此,即使很难确切地说出是什么导致了什么,您至少可以看到是否出现了任何有意义的图案。

您可以将每个RNN输出绘制为图像,其中一个轴为时间,另一个轴为输出。下面是一个小例子:

import tensorflow as tf
import numpy as np

n_steps = 100
n_inputs = 10
n_neurons = 10
n_layers = 3

x = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
layers = [tf.contrib.rnn.LSTMCell(num_units=n_neurons,
                                  activation=tf.nn.leaky_relu, use_peepholes=True)
         for layer in range(n_layers)]
multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers)
rnn_outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, x, dtype=tf.float32)
# Time steps in horizontal axis, outputs in vertical axis, add last dimension for channel
rnn_out_imgs = tf.transpose(rnn_outputs, (0, 2, 1))[..., tf.newaxis]
out_img_sum = tf.summary.image("RNN", rnn_out_imgs, max_outputs=10)
init_op = tf.global_variables_initializer()
with tf.Session() as sess, tf.summary.FileWriter('log') as fw:
    sess.run(init_op)
    fw.add_summary(sess.run(out_img_sum, feed_dict={x: np.random.rand(10, n_steps, n_inputs)}))
您将获得如下所示的可视化效果:

在这里,更亮的像素表示更强烈的激活,因此即使很难判断到底是什么导致了什么,您至少可以看到是否出现了任何有意义的模式