簡體   English   中英

Tensorflow將張量數組轉換為單張量

[英]Tensorflow convert array of tensors into single tensor

我正在構建一個帶有Tensorflow的LSTM RNN,它可以執行逐像素分類(或者,更好的方法就是像素化預測?)

當我解釋標題時,請耐心等待。

網絡如下圖所示......

在此輸入圖像描述

這個想法是這樣的......大小(200,200)的輸入圖像是大小為(200,200,200)的LSTM RNN的輸入。 從LSTM張量向量(LSTM RNN中的粉紅色框)輸出的每個序列被饋送到MLP,然后MLP進行單個輸出預測 - 像素逐像素預測 (您可以看到一個輸入像素如何生成一個輸出“像素”

代碼看起來像這樣(不是所有代碼,只需要部分代碼):

...
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)
...

我已經確認RNN的輸出 - 即存儲在temp是200x200的<tf.Tensor 'Softmax_39999:0' shape=(?, 1) dtype=float32>數組<tf.Tensor 'Softmax_39999:0' shape=(?, 1) dtype=float32>

正如您所看到的,我將temp放在相同形狀的tf.placeholder中(批量大小為None ......或者我需要這個嗎?)...程序就像它完成運行一樣退出。 理想情況下,當我調試和打印pred時,我想看到的是像<tf.Tensor shape=(200,200)>

當我調試時,第一次執行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

編輯我現在也意識到我需要放置線條

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)

在第一個循環內部,以便生成新的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?

換句話說,它是不是自動遞增RNN張量名稱?

報告形狀的更方便的方法是使用tf.shape()。 在你的情況下:

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

這樣,size1_fetched就像你從NumPy得到的東西。 此外,還給出了特定feed_dict的大小。 例如,你的[無,200,200] Tensor將是[64,200,200]

另一個問題:為什么在流程圖之間有占位符? 您以后會提供預定義的圖像特征映射嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM