简体   繁体   English

Tensorflow将张量数组转换为单张量

[英]Tensorflow convert array of tensors into single tensor

I'm building a LSTM RNN with Tensorflow that performs pixel-wise classification (or, maybe a better way to put it is, pixel-wise prediction?) 我正在构建一个带有Tensorflow的LSTM RNN,它可以执行逐像素分类(或者,更好的方法就是像素化预测?)

Bear with me as I explain the title. 当我解释标题时,请耐心等待。

The network looks like the following drawing... 网络如下图所示......

在此输入图像描述

The idea goes like this... an input image of size (200,200) is the input into a LSTM RNN of size (200,200,200). 这个想法是这样的......大小(200,200)的输入图像是大小为(200,200,200)的LSTM RNN的输入。 Each sequence output from the LSTM tensor vector (the pink boxes in the LSTM RNN) is fed into a MLP, and then the MLP makes a single output prediction -- ergo pixel-wise prediction (you can see how one input pixel generates one output "pixel" 从LSTM张量向量(LSTM RNN中的粉红色框)输出的每个序列被馈送到MLP,然后MLP进行单个输出预测 - 像素逐像素预测 (您可以看到一个输入像素如何生成一个输出“像素”

The code looks like this (not all of the code, just parts that are needed): 代码看起来像这样(不是所有代码,只需要部分代码):

...
n_input_x = 200
n_input_y = 200

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

def RNN(x):
    x = tf.transpose(x, [1, 0, 2])
    x = tf.reshape(x, [-1, n_input_x])
    x = tf.split(0, n_steps, x)

    lstm_cell = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)
    outputs, states = rnn.rnn(lstm_cell, x, dtype=tf.float32)

    output_matrix = []
    for i in xrange(200):
        temp_vector = []
        for j in xrange(200):
            lstm_vector = outputs[j]
            pixel_pred = multilayer_perceptron(lstm_vector, mlp_weights, mlp_biases)
            temp_vector.append(pixel_pred)
        output_matrix.append(temp_vector)
        print i

    return output_matrix

temp = RNN(x)
pred = tf.placeholder(temp, [None, n_input_x, n_input_y])
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
...

I have confirmed that the output of RNN -- that is, what is stored in temp is a 200x200 array of <tf.Tensor 'Softmax_39999:0' shape=(?, 1) dtype=float32> 我已经确认RNN的输出 - 即存储在temp是200x200的<tf.Tensor 'Softmax_39999:0' shape=(?, 1) dtype=float32>数组<tf.Tensor 'Softmax_39999:0' shape=(?, 1) dtype=float32>

As you can see, I place temp in a tf.placeholder of the same shape ( None for the batch size ... or do I need this?)... and the program just exits as if it completed running. 正如您所看到的,我将temp放在相同形状的tf.placeholder中(批量大小为None ......或者我需要这个吗?)...程序就像它完成运行一样退出。 Ideally what I want to see when I debug and print pred is something like <tf.Tensor shape=(200,200)> 理想情况下,当我调试和打印pred时,我想看到的是像<tf.Tensor shape=(200,200)>

When I debug, the first time I execute pred = tf.placeholder(temp, [None, n_input_x, n_input_y]) I get TypeError: TypeErro...32>]].",) and then it returns and I try again, and it says Exception AttributeError: "'NoneType' object has no attribute 'path'" in <function _remove at 0x7f1ab77c26e0> ignored 当我调试时,第一次执行pred = tf.placeholder(temp, [None, n_input_x, n_input_y])我得到TypeError: TypeErro...32>]].",)然后它returns ,我再试一次,并且它说Exception AttributeError: "'NoneType' object has no attribute 'path'" in <function _remove at 0x7f1ab77c26e0> ignored

EDIT I also now realize that I need to place the lines 编辑我现在也意识到我需要放置线条

lstm_cell = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)
outputs, states = rnn.rnn(lstm_cell, x, dtype=tf.float32)

Inside the first loop so that new 2D LSTM RNN are generated, however I'm getting an error about variable reusing ValueError: Variable RNN/BasicLSTMCell/Linear/Matrix does not exist, disallowed. Did you mean to set reuse=None in VarScope? 在第一个循环内部,以便生成新的2D​​ LSTM RNN,但是我收到关于变量重用ValueError: Variable RNN/BasicLSTMCell/Linear/Matrix does not exist, disallowed. Did you mean to set reuse=None in VarScope?的错误ValueError: Variable RNN/BasicLSTMCell/Linear/Matrix does not exist, disallowed. Did you mean to set reuse=None in VarScope? ValueError: Variable RNN/BasicLSTMCell/Linear/Matrix does not exist, disallowed. Did you mean to set reuse=None in VarScope?

So in other words, is it isn't auto incrementing the RNN tensor name? 换句话说,它是不是自动递增RNN张量名称?

A more convenient way to report shapes is with tf.shape(). 报告形状的更方便的方法是使用tf.shape()。 In your case: 在你的情况下:

size1 = tf.shape(temp)
sess = tf.Session()
size1_fetched = sess.run(size1, feed_dict = your_feed_dict)

That way, the size1_fetched is something like you would get from NumPy. 这样,size1_fetched就像你从NumPy得到的东西。 Moreover, also your sizes for that particular feed_dict are given. 此外,还给出了特定feed_dict的大小。 For example, your [None, 200, 200] Tensor would be [64, 200, 200] 例如,你的[无,200,200] Tensor将是[64,200,200]

Another question: why do you have the placeholder in between your flow-graph? 另一个问题:为什么在流程图之间有占位符? Will you later on feed pre-defined images feature-maps? 您以后会提供预定义的图像特征映射吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM