【问题标题】:Tensorflow Model Placeholder RestorationTensorFlow 模型占位符恢复
【发布时间】:2018-07-02 04:57:48
【问题描述】:

我想在 Tensorflow 中恢复、修改和重用一个(相当)复杂的模型,但是在整理使用占位符时如何正确传递 feed_dict 时遇到了一些困难。代码如下:

input_dir = "parallel_win_10_40_conv_3l_rnn"
input_file = "parallel_win_10_40_conv_3l_rnn"
saver = tf.train.import_meta_graph("./result/cnn_rnn_parallel/tune_rnn_layer/"+input_dir+"/model_"+input_file+".meta")

# # Method 1
# all_placeholders = [x for x in tf.get_default_graph().get_operations() if x.type == "Placeholder"]
# cnn_in, rnn_in, Y = all_placeholders[0], all_placeholders[1], all_placeholders[2]
# keep_prob, phase_train = all_placeholders[3], all_placeholders[4]

# Method 2
cnn_in = tf.placeholder(tf.float32, shape=[None, input_height, input_width, input_channel_num], name='cnn_in')
rnn_in = tf.placeholder(tf.float32, shape=[None, n_time_step, n_input_ele], name='rnn_in')
Y = tf.placeholder(tf.float32, shape=[None, n_labels], name = 'Y')
keep_prob = tf.placeholder(tf.float32, name='keep_prob')
phase_train = tf.placeholder(tf.bool, name='phase_train')

with tf.Session() as session:
    saver.restore(session, "./result/cnn_rnn_parallel/tune_rnn_layer/"+input_dir+"/model_"+input_file)

    test_cnn_batch = np.zeros(shape=[accuracy_batch_size], dtype=float)
    test_rnn_batch = np.zeros(shape=[accuracy_batch_size], dtype=float)

    offset = (accuracy_batch_size) % (test_y.shape[0] - accuracy_batch_size)
    test_cnn_batch = cnn_test_x[offset:(offset + accuracy_batch_size), :, :, :, :]
    test_cnn_batch = test_cnn_batch.reshape(len(test_cnn_batch) * window_size, input_height, input_width, 1)
    test_rnn_batch = rnn_test_x[offset:(offset + accuracy_batch_size), :, :]
    test_batch_y = test_y[offset:(offset + accuracy_batch_size), :]

    print(session.run('fin_m:0', feed_dict={cnn_in: test_cnn_batch, rnn_in: test_rnn_batch,
                                        Y: test_batch_y, keep_prob: 1.0, phase_train: False}))

当我使用方法 1 时,我得到一个错误:

TypeError:无法将 feed_dict 键解释为张量:无法将操作转换为张量。

当我使用方法 2 时,我得到一个不同的错误:

InvalidArgumentError(参见上面的回溯):您必须为占位符张量“cnn_in”提供一个值,其 dtype 为 float

这两个错误都让我感到困惑,因为占位符的定义在模型保存之前完全相同相同,所以它们不应该具有相同的类型(操作或张量)吗?对于第二种方法, test_cnn_batch 是一个 ndarray,其中包含浮点值。我认为这可能是由于模型中的 cnn_in 是在 saver = tf.train.import_meta_graph 行中定义的(根据错误消息)。我认为之后重新定义它可能会有所帮助,但没有骰子。

这里发生了什么?这样做的正确方法是什么?我已经阅读了许多相关的问题,但它们并没有直接解决这些问题。

感谢任何帮助。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    您的方法 1 是错误的,因为您需要获取 张量,而不是定义占位符的操作。由于您正在循环 get_operations() 的结果,因此您得到的是操作,而不是张量。

    我们的方法 2 也是错误的,因为您没有在图中获取占位符,而是定义了未连接到计算图其余部分的新占位符。

    您必须做的是找到占位符的名称,然后按名称从图表中获取它们。 错误代码已经向您显示了您需要的名称之一:

    InvalidArgumentError(参见上面的回溯):您必须提供一个值 对于具有 dtype 浮点数的占位符张量“cnn_in

    你可以这样做:

    cnn_in = tf.get_default_graph().get_tensor_by_name('cnn_in')
    

    注意:您可能需要在张量名称后附加:0

    对图表中需要的所有占位符重复相同的过程。

    【讨论】:

    • 是的,就是这样!我确实需要在张量名称中添加 :0 ,因为显然只有操作被命名为“cnn_in”。
    猜你喜欢
    • 2018-09-06
    • 2016-05-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多