Skip to main content
 首页 » 编程设计

python之tensorflow 和预测中的恢复模型

2024年10月17日24zdz8207

我在神经网络的 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 变量中写什么。

请您参考如下方法:

我回答晚了,但也许它仍然有用。 feed_dict 用于为 tensorflow 提供您希望占位符采用的值。 fetches(run 的第一个参数)是您想要的结果列表。 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') 也行,我没试过。

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