我通过 Jupyter Notebook 写了一个非常简单的模型
n_input = 3
X = tf.placeholder(tf.float32, [None, n_input], name="X")
decoder = tf.matmul(X, [[2.0,3.0],[2.0,3.0],[2.0,3.0]], name='decoder')
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
values = sess.run(decoder, feed_dict={X: df_norm[:10]})
graph = tf.get_default_graph()
tf.train.write_graph(graph, './model/','saved_model.pbtxt', as_text=False)
然后我用 TensorflowSharp 加载它
using (var graph = new TFGraph())
{
var bytes = File.ReadAllBytes(@".\model\saved_model.pbtxt");
graph.Import(bytes);
var session = new TFSession(graph);
var runner = session.GetRunner();
runner.AddInput(graph["X"][0], new float[] { 176.75f, 7.95f, 40397.00f });
runner.Fetch(graph["decoder"][0]);
var output = runner.Run();
// Fetch the results from output:
TFTensor result = output[0];
}
最后,我得到了以下异常:
TensorFlow.TFException HResult=0x80131500 메시지=您必须为占位符张量“X_1”提供一个值,其 dtype 为 float 和 shape [?,3]
[[{{node X_1}} = Placeholderdtype=DT_FLOAT, shape=[?,3], _device="/job:localhost/replica:0/task:0/device:CPU:0"]] 소스=TensorFlowSharp StackTrace: at TensorFlow.TFStatus.CheckMaybeRaise(TFStatus incomingStatus, Boolean last) at TensorFlow.TFSession.Run(TFOutput [] 输入,TFTensor[] inputValues,TFOutput[] 输出,TFOperation[] targetOpers,TFBuffer runMetadata,TFBuffer runOptions,TFStatus status) at TensorFlow.TFSession.Runner.Run(TFStatus status) at tensorflowsharp_model_restore.Program.Main(String[] args) 在 G:\tensorflow\tensorflowsharp\tensorflowsharp_model_restore\tensorflowsharp_model_restore\Program.cs:line 29