在张量流和预测中恢复模型

时间:2016-12-15 09:03:58

标签: python machine-learning neural-network tensorflow

我在神经网络的张量流中创建了模型。 我保存了模型并将其恢复到另一个python文件中。

代码如下:

def restoreModel():
    prediction = neuralNetworkModel(x)
    tf_p = tensorFlow.nn.softmax(prediction)
    temp = np.array([2,1,541,161124,3,3])
    temp = np.vstack(temp)

    with tensorFlow.Session() as sess:
        new_saver = tensorFlow.train.import_meta_graph('model.ckpt.meta')
        new_saver.restore(sess, tensorFlow.train.latest_checkpoint('./'))
        all_vars = tensorFlow.trainable_variables()

        tensorFlow.initialize_all_variables().run()
        sess.run(tensorFlow.initialize_all_variables())
        predict = sess.run([tf_p], feed_dict={
            tensorFlow.transpose(x): temp,
            y : ***
        })

当" temp"在我想要预测的变量! X是矢量形状,我转换"它匹配形状。 我不明白我需要在feed_dict变量中写什么。

1 个答案:

答案 0 :(得分:2)

我回答的很晚,但也许它仍然有用。 feed_dict用于为张量流提供您希望占位符占用的值。 fetchesrun的第一个参数)是您想要的结果列表。 feed_dict的键和fetches的元素必须是张量的名称(我没试过)或者你可以得到的变量

graph = tf.get_default_graph()
var = graph.get_operation_by_name('name_of_operation').outputs[0]

也许graph.get_tensor_by_name('name_of_operation:0')也有效,我没试过。

默认情况下,占位符的名称只是“占位符”,“占位符_1”等,遵循图表定义中的创建顺序。

相关问题