Machine learning Tensorflow网格3LSTMcell可视化

Machine learning Tensorflow网格3LSTMcell可视化,machine-learning,neural-network,artificial-intelligence,tensorflow,lstm,Machine Learning,Neural Network,Artificial Intelligence,Tensorflow,Lstm,我很难想象这个Tensorflow类创建了什么。我想实现一个处理3D数据的LSTM RNN class Grid3LSTMCell(GridRNNCell): """3D BasicLSTM cell This creates a 2D cell which receives input and gives output in the first dimension. The first dimension can optionally be non-recurrent if

我很难想象这个Tensorflow类创建了什么。我想实现一个处理3D数据的LSTM RNN

class Grid3LSTMCell(GridRNNCell):
  """3D BasicLSTM cell
    This creates a 2D cell which receives input and gives output in the first dimension.
    The first dimension can optionally be non-recurrent if `non_recurrent_fn` is specified.
    The second and third dimensions are LSTM.
  """
  def __init__(self, num_units, tied=False, non_recurrent_fn=None,
               use_peepholes=False, forget_bias=1.0):
    super(Grid3LSTMCell, self).__init__(num_units=num_units, num_dims=3,
                                        input_dims=0, output_dims=0, priority_dims=0, tied=tied,
                                        non_recurrent_dims=None if non_recurrent_fn is None else 0,
                                        cell_fn=lambda n, i: rnn_cell.LSTMCell(
                                          num_units=n, input_size=i, forget_bias=forget_bias,
                                          use_peepholes=use_peepholes),
                                        non_recurrent_fn=non_recurrent_fn)

The class is found in `from tensorflow.contrib.grid_rnn.python.ops import grid_rnn_cell`.
这很难解释,所以我提供了一张图纸。这是我想要它做的

然而,这条评论听起来似乎并没有这样做。注释使RNN听起来像是一个平面RNN,其中第一个维度输出到通常称为
输出的
变量(见下文)。第二个维度输出到RNN中的下一个步骤,第三个维度输出到下一个隐藏层

输出,状态=rnn.rnn(lstm\u单元格,x,dtype=tf.float32)

如果是这样的话,拥有第一维度和第二维度有什么意义?它们本质上不是一回事吗?
BasicLSTMCell
将下一步的输出发送到
输出
——换句话说,它们是同一个单元中的一个

清晰

作为参考,这里是我的示例代码

import tensorflow as tf
from tensorflow.python.ops import rnn, rnn_cell
from tensorflow.contrib.grid_rnn.python.ops import grid_rnn_cell
import numpy as np

#define parameters
learning_rate = 0.01
batch_size = 2

n_input_x = 10
n_input_y = 10
n_input_z = 10

n_hidden = 128
n_classes = 2
n_output = n_input_x * n_classes

x = tf.placeholder("float", [n_input_x, n_input_y, n_input_z])
y = tf.placeholder("float", [n_input_x, n_input_y, n_input_z, n_classes])

weights = {}
biases = {}

for i in xrange(n_input_y * n_input_z):
    weights[i] = tf.Variable(tf.random_normal([n_hidden, n_output]))
    biases[i] = tf.Variable(tf.random_normal([n_output]))

#generate random data
input_data = np.random.rand(n_input_x, n_input_y, n_input_z)
ground_truth = np.random.rand(n_input_x, n_input_y, n_input_z, n_classes)

#build GridLSTM
def GridLSTM_network(x):

    x = tf.reshape(x, [-1,n_input_x])
    x = tf.split(0, n_input_y * n_input_z, x)

    lstm_cell = grid_rnn_cell.Grid3LSTMCell(n_hidden)

    outputs, states = rnn.rnn(lstm_cell, x, dtype=tf.float32)

    output = []
    for i in xrange(n_input_y * n_input_z):
        output.append(tf.matmul(outputs[i], weights[i]) + biases[i])

    return output

#initialize network, cost, optimizer and all variables
pred = GridLSTM_network(x)
# import pdb
# pdb.set_trace()

pred = tf.pack(pred)
pred = tf.transpose(pred,[1,0,2])
pred= tf.reshape(pred, [-1, n_input_x, n_input_y, n_input_z, n_classes])
temp_pred = tf.reshape(pred, [-1,n_classes])
temp_y = tf.reshape(y,[-1, n_classes])

cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(temp_pred, temp_y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# Evaluate model
correct_pred = tf.equal(0,tf.cast(tf.sub(tf.nn.sigmoid(temp_pred),temp_y), tf.int32))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))


# Initializing the variables
init = tf.initialize_all_variables()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)
    step = 0
    while 1:
        print step
        step = step + 1
        # pdb.set_trace
        sess.run(optimizer, feed_dict={x: input_data, y: ground_truth})