【问题标题】:Tensorflow convert array of tensors into single tensorTensorflow 将张量数组转换为单个张量
【发布时间】:2016-10-15 23:09:34
【问题描述】:

我正在使用 Tensorflow 构建一个 LSTM RNN,它执行逐像素分类(或者,也许更好的说法是逐像素预测?)

请耐心等待我解释标题。

网络如下图...

这个想法是这样的......大小为 (200,200) 的输入图像是大小为 (200,200,200) 的 LSTM RNN 的输入。从 LSTM 张量向量(LSTM RNN 中的粉红色框)输出的每个序列都被输入到 MLP,然后 MLP 进行单个输出预测 -- ergo pixel-wise prediction(你可以看到一个输入像素如何生成一个输出“像素”

代码看起来像这样(不是所有的代码,只是需要的部分):

...
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中存储的是<tf.Tensor 'Softmax_39999:0' shape=(?, 1) dtype=float32>的200x200数组

如您所见,我将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?

也就是说,不是自动递增 RNN 张量名称吗?

【问题讨论】:

    标签: python machine-learning neural-network artificial-intelligence tensorflow


    【解决方案1】:

    报告形状的更方便的方法是使用 tf.shape()。在你的情况下:

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

    这样,size1_fetched 就像您从 NumPy 获得的一样。此外,还给出了该特定 feed_dict 的尺寸。例如,您的 [None, 200, 200] 张量将是 [64, 200, 200]

    另一个问题:为什么在流程图之间有占位符?您稍后会提供预定义的图像特征图吗?

    【讨论】:

    • 我做什么tf.shape(temp) 它返回一个张量...<tf.Tensor 'Shape_1:0' shape=(4,) dtype=int32> 为什么它是(4,),为什么它是一个int32?如果数组本身的形状为 (200,200) 来自您的代码,您只需将模型直接输入到会话中,而没有任何类型的损失或优化器功能。我不想那样做
    猜你喜欢
    • 2023-02-15
    • 2021-11-12
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 2016-06-14
    • 2017-06-14
    • 1970-01-01
    相关资源
    最近更新 更多