Skip to content

Commit 363b58b

Browse files
committed
add _init_from_proto to create variable.
1 parent 5c2818e commit 363b58b

3 files changed

Lines changed: 67 additions & 9 deletions

File tree

‎src/TensorFlowNET.Core/Framework/meta_graph.py.cs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Google.Protobuf;
2+
using System;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Linq;
@@ -59,7 +60,7 @@ public static (RefVariable[], string[]) import_scoped_meta_graph_with_return_ele
5960
return_elements: return_elements);
6061

6162
// Restores all the other collections.
62-
var variable_objects = new Dictionary<string, RefVariable>();
63+
var variable_objects = new Dictionary<ByteString, RefVariable>();
6364
foreach(var col in meta_graph_def.CollectionDef.OrderBy(x => x.Key))
6465
{
6566
// Don't add unbound_inputs to the new graph.
@@ -81,8 +82,15 @@ public static (RefVariable[], string[]) import_scoped_meta_graph_with_return_ele
8182
{
8283
foreach (var value in col.Value.BytesList.Value)
8384
{
84-
var proto = VariableDef.Parser.ParseFrom(value);
85-
throw new NotImplementedException("import_scoped_meta_graph_with_return_elements");
85+
RefVariable variable = null;
86+
if (!variable_objects.ContainsKey(value))
87+
{
88+
var proto = VariableDef.Parser.ParseFrom(value);
89+
variable = new RefVariable(variable_def: proto, import_scope: scope_to_prepend_to_names);
90+
variable_objects[value] = variable;
91+
}
92+
93+
graph.add_to_collection(col.Key, variable);
8694
}
8795
}
8896
else

‎src/TensorFlowNET.Core/Variables/RefVariable.cs‎

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,68 @@ public partial class RefVariable : VariableV1
2323

2424
public string name => _variable.name;
2525

26-
public RefVariable(object initial_value,
26+
public RefVariable(object initial_value = null,
2727
bool trainable = true,
2828
List<string> collections = null,
2929
bool validate_shape = true,
3030
string caching_device = "",
3131
string name = "",
32-
TF_DataType dtype = TF_DataType.DtInvalid) :
33-
base(initial_value, trainable, collections, validate_shape, caching_device, name, dtype)
32+
VariableDef variable_def = null,
33+
TF_DataType dtype = TF_DataType.DtInvalid,
34+
string import_scope = "") : base(initial_value,
35+
trainable,
36+
collections,
37+
validate_shape,
38+
caching_device,
39+
name,
40+
dtype)
3441
{
3542
_in_graph_mode = true;
3643

37-
_init_from_args(initial_value, trainable, collections, validate_shape, caching_device, name, dtype);
44+
if (variable_def != null)
45+
{
46+
if (initial_value != null)
47+
throw new ValueError("variable_def and initial_value are mutually exclusive.");
48+
_init_from_proto(variable_def, import_scope: import_scope);
49+
}
50+
else
51+
{
52+
_init_from_args(initial_value, trainable, collections, validate_shape, caching_device, name, dtype);
53+
}
54+
}
55+
56+
private void _init_from_proto(VariableDef variable_def, string import_scope = "")
57+
{
58+
var g = ops.get_default_graph();
59+
60+
_variable = g.as_graph_element(
61+
ops.prepend_name_scope(variable_def.VariableName,
62+
import_scope: import_scope)) as Tensor;
63+
64+
_initializer_op = g.as_graph_element(
65+
ops.prepend_name_scope(variable_def.InitializerName,
66+
import_scope: import_scope)) as Operation;
67+
68+
// Tests whether initial_value_name exists first for backwards compatibility.
69+
if (!string.IsNullOrEmpty(variable_def.InitialValueName))
70+
_initial_value = g.as_graph_element(
71+
ops.prepend_name_scope(variable_def.InitialValueName,
72+
import_scope: import_scope)) as Tensor;
73+
else
74+
_initial_value = null;
75+
76+
_trainable = variable_def.Trainable;
77+
_snapshot = g.as_graph_element(
78+
ops.prepend_name_scope(variable_def.SnapshotName,
79+
import_scope: import_scope)) as Tensor;
80+
81+
if (variable_def.SaveSliceInfoDef != null)
82+
throw new NotImplementedException("save_slice_info_def");
83+
else
84+
;// _save_slice_info = null;
85+
86+
//_caching_device = null;
87+
//_constraint = null;
3888
}
3989

4090
private void _init_from_args(object initial_value,

‎src/TensorFlowNET.Core/Variables/VariableV1.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Tensorflow
1616
/// </summary>
1717
public class VariableV1
1818
{
19-
public VariableV1(object initial_value,
19+
public VariableV1(object initial_value = null,
2020
bool trainable = true,
2121
List<string> collections = null,
2222
bool validate_shape = true,

0 commit comments

Comments
 (0)