Tensorflow Tensor和
之间的区别是什么Variable?我注意到在这个stackoverflow的答案中,我们可以Variable在任何Tensor可以使用的地方使用.但是,我没有尽到session.run()一个Variable:
A = tf.zeros([10]) # A is a Tensor
B = tf.Variable([111, 11, 11]) # B is a Variable
sess.run(A) # OK. Will return the values in A
sess.run(B) # Error.
Run Code Online (Sandbox Code Playgroud) 如何导入冻结的protobuf,以便对其进行重新训练?
我在网上找到的所有方法都需要检查点。有没有办法读取protobuf,以便将内核常数和偏差常数转换为变量?
编辑1:这类似于以下问题:如何在图(.pb)中重新训练模型?
我查看了DeepSpeech,该问题的答案中建议使用它。他们似乎有删除的支持的initialize_from_frozen_model。我找不到原因。
编辑2:我尝试创建一个新的GraphDef对象,在其中我用变量替换了内核和偏差:
probable_variables = [...] # kernels and biases of Conv2D and MatMul
new_graph_def = tf.GraphDef()
with tf.Session(graph=graph) as sess:
for n in sess.graph_def.node:
if n.name in probable_variables:
# create variable op
nn = new_graph_def.node.add()
nn.name = n.name
nn.op = 'VariableV2'
nn.attr['dtype'].CopyFrom(attr_value_pb2.AttrValue(type=dtype))
nn.attr['shape'].CopyFrom(attr_value_pb2.AttrValue(shape=shape))
else:
nn = new_model.node.add()
nn.CopyFrom(n)
Run Code Online (Sandbox Code Playgroud)
不知道我走的路是否正确。不知道如何设置trainable=True的NodeDef对象。
我将探讨如何在图中表示变量.我创建一个变量,初始化它并在每个动作后创建图形快照:
import tensorflow as tf
def dump_graph(g, filename):
with open(filename, 'w') as f:
print(g.as_graph_def(), file=f)
g = tf.get_default_graph()
var = tf.Variable(2)
dump_graph(g, 'data/after_var_creation.graph')
init = tf.global_variables_initializer()
dump_graph(g, 'data/after_initializer_creation.graph')
with tf.Session() as sess:
sess.run(init)
dump_graph(g, 'data/after_initializer_run.graph')
Run Code Online (Sandbox Code Playgroud)
变量创建后的图形看起来像
node {
name: "Variable/initial_value"
op: "Const"
attr {
key: "dtype"
value {
type: DT_INT32
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_INT32
tensor_shape {
}
int_val: 2
}
}
}
}
node {
name: "Variable"
op: "VariableV2"
attr {
key: …Run Code Online (Sandbox Code Playgroud) tensorflow ×3