Skip to content

Commit 4252952

Browse files
committed
Fix the error that loaded concrete function does not work.
1 parent 3943375 commit 4252952

4 files changed

Lines changed: 32 additions & 17 deletions

File tree

‎src/TensorFlowNET.Core/Functions/ConcreteFunction.cs‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Tensorflow.Framework.Models;
77
using Tensorflow.Graphs;
88
using Tensorflow.Train;
9+
using Tensorflow.Util;
910
using static Tensorflow.Binding;
1011

1112
namespace Tensorflow.Functions
@@ -21,6 +22,7 @@ public class ConcreteFunction: Trackable
2122
protected Dictionary<string, string> _attrs;
2223
protected FunctionSpec _function_spec;
2324
protected FunctionSpec _pre_initialized_function_spec = null;
25+
protected EagerDefinedFunction _inference_function;
2426
internal ForwardBackwardCall forward_backward;
2527
public Tensor[] Inputs => func_graph.Inputs;
2628
public Tensor[] CapturedInputs => func_graph.external_captures;
@@ -39,6 +41,7 @@ public ConcreteFunction(string name)
3941
_captured_inputs = func_graph.external_captures;
4042
_attrs= new Dictionary<string, string>();
4143
_delayed_rewrite_functions = new DelayedRewriteGradientFunctions(func_graph, _attrs);
44+
_inference_function = _delayed_rewrite_functions.Forward();
4245
}
4346

4447
public ConcreteFunction(FuncGraph graph, Dictionary<string, string> attrs = null)
@@ -49,6 +52,7 @@ public ConcreteFunction(FuncGraph graph, Dictionary<string, string> attrs = null
4952
//ToGraph(graph.Inputs, graph.Outputs.Where(x => x != null).ToArray());
5053
_attrs = attrs;
5154
_delayed_rewrite_functions = new DelayedRewriteGradientFunctions(func_graph, _attrs);
55+
_inference_function = _delayed_rewrite_functions.Forward();
5256
}
5357

5458
public ConcreteFunction(Func<Tensor, Tensor> func, TF_DataType dtype)
@@ -69,6 +73,7 @@ public ConcreteFunction(Func<Tensor, Tensor> func, TF_DataType dtype)
6973
_captured_inputs = func_graph.external_captures;
7074
_attrs = new Dictionary<string, string>();
7175
_delayed_rewrite_functions = new DelayedRewriteGradientFunctions(func_graph, _attrs);
76+
_inference_function = _delayed_rewrite_functions.Forward();
7277
}
7378

7479
public ConcreteFunction(Func<Tensor, IDatasetV2> func, TF_DataType dtype)
@@ -92,6 +97,7 @@ public ConcreteFunction(Func<Tensor, IDatasetV2> func, TF_DataType dtype)
9297
_captured_inputs = func_graph.external_captures;
9398
_attrs = new Dictionary<string, string>();
9499
_delayed_rewrite_functions = new DelayedRewriteGradientFunctions(func_graph, _attrs);
100+
_inference_function = _delayed_rewrite_functions.Forward();
95101
}
96102

97103
/*public ConcreteFunction(Func<Tensors, Tensors> func,
@@ -154,9 +160,10 @@ public Tensors CallFlat(Tensor[] args, Tensor[] captured_inputs)
154160
{
155161
tensor_inputs.Add(arg);
156162
// If we're graph building, shape inference is on.
157-
if (!executing_eagerly)
158-
{
159-
}
163+
}
164+
if (!executing_eagerly)
165+
{
166+
160167
}
161168
tensor_inputs.AddRange(captured_inputs);
162169

@@ -166,12 +173,13 @@ public Tensors CallFlat(Tensor[] args, Tensor[] captured_inputs)
166173
// No tape is watching; skip to running the function.
167174
if (possible_gradient_type == 0 && executing_eagerly)
168175
{
169-
var attrs = new object[]
170-
{
171-
"executor_type", "",
172-
"config_proto", tf.Context.FunctionCallOptions.config_proto_serialized()
173-
};
174-
return tf.Runner.Execute(tf.Context, func_graph.FuncName, func_graph.Outputs.Length, args, attrs);
176+
return _build_call_outputs(_inference_function.Call(args));
177+
//var attrs = new object[]
178+
//{
179+
// "executor_type", "",
180+
// "config_proto", tf.Context.FunctionCallOptions.config_proto_serialized()
181+
//};
182+
//return tf.Runner.Execute(tf.Context, func_graph.FuncName, func_graph.Outputs.Length, args, attrs);
175183
}
176184

177185
if (forward_backward == null)
@@ -184,10 +192,11 @@ public Tensors CallFlat(Tensor[] args, Tensor[] captured_inputs)
184192
}
185193
else
186194
{
195+
// TODO(Rinne): add `default_graph._override_gradient_function`.
187196
flat_outputs = forward_function.Call(args_with_tangents);
188197
}
189198
forward_backward.Record(flat_outputs);
190-
return flat_outputs;
199+
return _build_call_outputs(flat_outputs);
191200
}
192201

193202
public void AddTograph(Graph? g = null)
@@ -262,6 +271,13 @@ internal void _initialize_function_spec()
262271
};
263272
}
264273

274+
private Tensors _build_call_outputs(Tensors result)
275+
{
276+
// TODO(Rinne): dwal with `func_graph.structured_outputs`
277+
278+
return result;
279+
}
280+
265281
public override string ToString()
266282
=> Name;
267283
}

‎src/TensorFlowNET.Keras/Engine/Sequential.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ public void add(ILayer layer)
124124
if (set_inputs || _is_graph_network)
125125
{
126126
_init_graph_network(inputs, outputs);
127-
_is_graph_network = true;
127+
_graph_initialized = true;
128128
}
129129
else
130130
{
131131
_self_tracked_trackables.add(layer);
132+
// TODO(Rinne): self._handle_deferred_layer_dependencies([layer])
132133
}
133134
}
134135

‎src/TensorFlowNET.Keras/Saving/KerasObjectLoader.cs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,8 @@ private void _reconstruct_model(int model_id, Model model, List<Layer> layers)
189189
}
190190

191191
// `model.__init__(layers, config["name"])`InitLayers(layers);
192-
s = new Sequential(new SequentialArgs(){
193-
Layers = layers.Select(x => x as ILayer).ToList(),
194-
Name = config["name"].ToObject<string>()
195-
});
196-
//s.Name = config["name"].ToObject<string>();
192+
s.InitLayers(layers.Select(x => x as ILayer));
193+
s.Name = config["name"].ToObject<string>();
197194
if(s.inputs is null || s.inputs.Length == 0)
198195
{
199196
var first_layer = _get_child_layer_node_ids(model_id)[0];

‎test/TensorFlowNET.Keras.UnitTest/SaveModel/SequentialModelLoad.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ public void AlexnetFromSequential()
6262
[TestMethod]
6363
public void Temp()
6464
{
65-
var model = tf.keras.models.load_model(@"C:\Work\tf.net\tf_test\python_func");
65+
var model = tf.keras.models.load_model(@"D:\development\tf.net\tf_test\python_func");
6666
model.summary();
6767

6868
var x = tf.ones((2, 10));
6969
var y = model.Apply(x);
70+
Console.WriteLine(y);
7071
}
7172
}

0 commit comments

Comments
 (0)