【发布时间】:2017-06-26 00:41:24
【问题描述】:
我正在尝试从磁盘加载张量流模型并预测值。
代码
def get_value(row):
print("**********************************************")
graph = tf.Graph()
rowkey = row[0]
checkpoint_file = "/home/sahil/Desktop/Relation_Extraction/data/1485336002/checkpoints/model-300"
print("Loading model................................")
with graph.as_default():
session_conf = tf.ConfigProto(
allow_soft_placement=allow_soft_placement,
log_device_placement=log_device_placement)
sess = tf.Session(config=session_conf)
with sess.as_default():
# Load the saved meta graph and restore variables
saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))
saver.restore(sess, checkpoint_file)
input_x = graph.get_operation_by_name("X_train").outputs[0]
dropout_keep_prob = graph.get_operation_by_name("dropout_keep_prob").outputs[0]
predictions = graph.get_operation_by_name("output/predictions").outputs[0]
batch_predictions = sess.run(predictions, {input_x: [row[1]], dropout_keep_prob: 1.0})
print(batch_predictions)
return (rowkey, batch_predictions)
我有一个 RDD,它由一个元组(rowkey,input_vector)组成。我想使用加载的模型来预测输入的分数/类别。
调用 get_value() 的代码
result = data_rdd.map(lambda iter: get_value(iter))
result.foreach(print)
问题是我每次调用地图时,每次都为每个元组加载模型,这需要很多时间。
我正在考虑使用 mapPartitions 加载模型,然后使用 map 调用 get_value 函数。 我不知道如何将代码转换为 mapPartition,每个分区只加载一次 tensorflow 模型并减少运行时间。
提前致谢。
【问题讨论】:
标签: python tensorflow pyspark