如何将张量摘要恢复为变量

时间:2017-02-12 18:30:00

标签: python tensorflow

我训练了我的网络,并将最终的权重保存为这样的摘要:

# ...
weights_summaries.append(tf.summary.tensor_summary('out-weights', weights['out'])) 
# ... write summary

现在我想使用我的分类器。我试图从摘要中加载权重:

for e in tf.train.summary_iterator(summary_file):
    for v in e.summary.value:
        # ...
        # found the node
        elif v.node_name == 'out-weights':
            weights['out'] = tf.Variable(v.tensor)
            # it doesn't work!
            weights['out'] = tf.Variable.from_proto(v)
            # assert!
            weights['out'] = tf.Variable.from_proto(v.tensor)
            # assert!
            weights['out'] = tf.Variable(tf.Tensor.from_proto(v.tensor))
            # Tensor.from_proto is not defined!

那么,我应该如何加载重量?我知道"全球"模型保护程序,但我宁愿只保存我需要的数据。

提前致谢, 亚历山大

2 个答案:

答案 0 :(得分:0)

如果您想要该模型的子集,我建议您使用var_list constructor中的tf.train.Saver参数。

答案 1 :(得分:0)

最后,我找到了解决方案:

def tensor_summary_value_to_variable(value):
    fb = numpy.frombuffer(v.tensor.tensor_content, dtype = numpy.float32)
    shape = []
    for d in v.tensor.tensor_shape.dim:
        shape.append(d.size)
    fb = fb.reshape(shape)
    var = tf.Variable(fb)
    return var