Skip to content

Commit 0287191

Browse files
committed
create ResourceVariable, still with buggy.
1 parent 53ccd78 commit 0287191

File tree

14 files changed

+229
-12
lines changed

14 files changed

+229
-12
lines changed

TensorFlow.NET.sln

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,102 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Hub", "src\Te
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Datasets", "src\TensorFlowNET.Datasets\TensorFlowNET.Datasets.csproj", "{494D6CAD-2C0D-4C0B-90E2-B097DB039383}"
1717
EndProject
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
23+
Debug|x64 = Debug|x64
24+
Publish|Any CPU = Publish|Any CPU
25+
Publish|x64 = Publish|x64
2126
Release|Any CPU = Release|Any CPU
27+
Release|x64 = Release|x64
2228
EndGlobalSection
2329
GlobalSection(ProjectConfigurationPlatforms) = postSolution
2430
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2531
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Debug|x64.ActiveCfg = Debug|Any CPU
33+
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Debug|x64.Build.0 = Debug|Any CPU
34+
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Publish|Any CPU.ActiveCfg = Release|Any CPU
35+
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Publish|Any CPU.Build.0 = Release|Any CPU
36+
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Publish|x64.ActiveCfg = Release|Any CPU
37+
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Publish|x64.Build.0 = Release|Any CPU
2638
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
2739
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Release|Any CPU.Build.0 = Release|Any CPU
40+
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Release|x64.ActiveCfg = Release|Any CPU
41+
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Release|x64.Build.0 = Release|Any CPU
2842
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2943
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|Any CPU.Build.0 = Debug|Any CPU
44+
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|x64.ActiveCfg = Debug|Any CPU
45+
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|x64.Build.0 = Debug|Any CPU
46+
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Publish|Any CPU.ActiveCfg = Release|Any CPU
47+
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Publish|Any CPU.Build.0 = Release|Any CPU
48+
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Publish|x64.ActiveCfg = Release|Any CPU
49+
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Publish|x64.Build.0 = Release|Any CPU
3050
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.ActiveCfg = Release|Any CPU
3151
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.Build.0 = Release|Any CPU
52+
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|x64.ActiveCfg = Release|Any CPU
53+
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|x64.Build.0 = Release|Any CPU
3254
{D03F94CF-B283-4730-B177-21A57641061F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
3355
{D03F94CF-B283-4730-B177-21A57641061F}.Debug|Any CPU.Build.0 = Debug|Any CPU
56+
{D03F94CF-B283-4730-B177-21A57641061F}.Debug|x64.ActiveCfg = Debug|Any CPU
57+
{D03F94CF-B283-4730-B177-21A57641061F}.Debug|x64.Build.0 = Debug|Any CPU
58+
{D03F94CF-B283-4730-B177-21A57641061F}.Publish|Any CPU.ActiveCfg = Release|Any CPU
59+
{D03F94CF-B283-4730-B177-21A57641061F}.Publish|Any CPU.Build.0 = Release|Any CPU
60+
{D03F94CF-B283-4730-B177-21A57641061F}.Publish|x64.ActiveCfg = Release|Any CPU
61+
{D03F94CF-B283-4730-B177-21A57641061F}.Publish|x64.Build.0 = Release|Any CPU
3462
{D03F94CF-B283-4730-B177-21A57641061F}.Release|Any CPU.ActiveCfg = Release|Any CPU
3563
{D03F94CF-B283-4730-B177-21A57641061F}.Release|Any CPU.Build.0 = Release|Any CPU
64+
{D03F94CF-B283-4730-B177-21A57641061F}.Release|x64.ActiveCfg = Release|Any CPU
65+
{D03F94CF-B283-4730-B177-21A57641061F}.Release|x64.Build.0 = Release|Any CPU
3666
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
3767
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Debug|Any CPU.Build.0 = Debug|Any CPU
68+
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Debug|x64.ActiveCfg = Debug|Any CPU
69+
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Debug|x64.Build.0 = Debug|Any CPU
70+
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Publish|Any CPU.ActiveCfg = Release|Any CPU
71+
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Publish|Any CPU.Build.0 = Release|Any CPU
72+
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Publish|x64.ActiveCfg = Release|Any CPU
73+
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Publish|x64.Build.0 = Release|Any CPU
3874
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Release|Any CPU.ActiveCfg = Release|Any CPU
3975
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Release|Any CPU.Build.0 = Release|Any CPU
76+
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Release|x64.ActiveCfg = Release|Any CPU
77+
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Release|x64.Build.0 = Release|Any CPU
4078
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
4179
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Debug|Any CPU.Build.0 = Debug|Any CPU
80+
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Debug|x64.ActiveCfg = Debug|Any CPU
81+
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Debug|x64.Build.0 = Debug|Any CPU
82+
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Publish|Any CPU.ActiveCfg = Release|Any CPU
83+
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Publish|Any CPU.Build.0 = Release|Any CPU
84+
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Publish|x64.ActiveCfg = Release|Any CPU
85+
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Publish|x64.Build.0 = Release|Any CPU
4286
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Release|Any CPU.ActiveCfg = Release|Any CPU
4387
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Release|Any CPU.Build.0 = Release|Any CPU
88+
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Release|x64.ActiveCfg = Release|Any CPU
89+
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Release|x64.Build.0 = Release|Any CPU
4490
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
4591
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Debug|Any CPU.Build.0 = Debug|Any CPU
92+
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Debug|x64.ActiveCfg = Debug|Any CPU
93+
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Debug|x64.Build.0 = Debug|Any CPU
94+
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Publish|Any CPU.ActiveCfg = Release|Any CPU
95+
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Publish|Any CPU.Build.0 = Release|Any CPU
96+
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Publish|x64.ActiveCfg = Release|Any CPU
97+
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Publish|x64.Build.0 = Release|Any CPU
4698
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Release|Any CPU.ActiveCfg = Release|Any CPU
4799
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Release|Any CPU.Build.0 = Release|Any CPU
100+
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Release|x64.ActiveCfg = Release|Any CPU
101+
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Release|x64.Build.0 = Release|Any CPU
102+
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
103+
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
104+
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Debug|x64.ActiveCfg = Debug|x64
105+
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Debug|x64.Build.0 = Debug|x64
106+
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Publish|Any CPU.ActiveCfg = Publish|Any CPU
107+
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Publish|Any CPU.Build.0 = Publish|Any CPU
108+
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Publish|x64.ActiveCfg = Publish|x64
109+
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Publish|x64.Build.0 = Publish|x64
110+
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
111+
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Release|Any CPU.Build.0 = Release|Any CPU
112+
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Release|x64.ActiveCfg = Release|x64
113+
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Release|x64.Build.0 = Release|x64
48114
EndGlobalSection
49115
GlobalSection(SolutionProperties) = preSolution
50116
HideSolutionNode = FALSE

src/TensorFlowNET.Core/Binding.Util.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public static void tf_with(IObjectLife py, Action<IObjectLife> action)
113113
}
114114
}
115115

116+
[DebuggerStepThrough]
116117
[DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
117118
public static void tf_with<T>(T py, Action<T> action) where T : IObjectLife
118119
{

src/TensorFlowNET.Core/Graphs/Graph.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public Operation create_op(string op_type, Tensor[] inputs, TF_DataType[] dtypes
266266
name = op_type;
267267
// If a names ends with a '/' it is a "name scope" and we use it as-is,
268268
// after removing the trailing '/'.
269-
name = name.EndsWith("/") ? ops._name_from_scope_name(name) : unique_name(name);
269+
name = name.EndsWith("/") ? ops.name_from_scope_name(name) : unique_name(name);
270270
var node_def = ops._NodeDef(op_type, name, device: "", attrs: attrs);
271271

272272
var input_ops = inputs.Select(x => x.op).ToArray();
@@ -341,7 +341,7 @@ public string name_scope(string name)
341341
if (string.IsNullOrEmpty(name))
342342
new_stack = "";
343343
else if (name.EndsWith("/"))
344-
new_stack = ops._name_from_scope_name(name);
344+
new_stack = ops.name_from_scope_name(name);
345345
else
346346
new_stack = unique_name(name);
347347

src/TensorFlowNET.Core/Keras/Utils/base_layer_utils.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public static VariableV1 make_variable(string name,
4949
var v = tf.VariableV1(init_val,
5050
use_resource: use_resource,
5151
dtype: dtype,
52-
shape: shape);
52+
shape: shape,
53+
name: name);
5354

5455
return v;
5556
}

src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,12 @@ public static Operation assign_variable_op(Tensor resource, Tensor value, string
2626

2727
return _op;
2828
}
29+
30+
public static Tensor var_is_initialized_op(Tensor resource, string name = null)
31+
{
32+
var _op = _op_def_lib._apply_op_helper("VarIsInitializedOp", name, new { resource });
33+
34+
return _op;
35+
}
2936
}
3037
}

src/TensorFlowNET.Core/Operations/resource_variable_ops.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,29 @@ public static bool is_resource_variable(VariableV1 var)
7474
return var is ResourceVariable;
7575
}
7676

77+
/// <summary>
78+
/// Creates a variable handle with information to do shape inference.
79+
/// </summary>
80+
/// <param name="initial_value"></param>
81+
/// <param name="shape"></param>
82+
/// <param name="shared_name"></param>
83+
/// <param name="name"></param>
84+
/// <param name="graph_mode"></param>
85+
/// <returns></returns>
86+
public static Tensor eager_safe_variable_handle(Tensor initial_value, TensorShape shape,
87+
string shared_name, string name, bool graph_mode)
88+
{
89+
var dtype = initial_value.dtype.as_base_dtype();
90+
return variable_handle_from_shape_and_dtype(
91+
shape, dtype, shared_name, name, graph_mode, initial_value);
92+
}
93+
94+
public static Tensor variable_handle_from_shape_and_dtype(TensorShape shape, TF_DataType dtype,
95+
string shared_name, string name, bool graph_mode, Tensor extra_handle_data = null)
96+
{
97+
throw new NotImplementedException("");
98+
}
99+
77100
/// <summary>
78101
/// Represents a future for a read of a variable.
79102
/// Pretends to be the tensor if anyone looks.

src/TensorFlowNET.Core/TensorFlowNET.Core.csproj

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<AssemblyName>TensorFlow.NET</AssemblyName>
66
<RootNamespace>Tensorflow</RootNamespace>
77
<TargetTensorFlow>1.14.0</TargetTensorFlow>
8-
<Version>0.11.5</Version>
8+
<Version>0.11.6</Version>
99
<Authors>Haiping Chen, Meinrad Recheis, Eli Belash</Authors>
1010
<Company>SciSharp STACK</Company>
1111
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
@@ -17,7 +17,7 @@
1717
<PackageTags>TensorFlow, NumSharp, SciSharp, MachineLearning, TensorFlow.NET, C#</PackageTags>
1818
<Description>Google's TensorFlow full binding in .NET Standard.
1919
Docs: https://tensorflownet.readthedocs.io</Description>
20-
<AssemblyVersion>0.11.5.0</AssemblyVersion>
20+
<AssemblyVersion>0.11.6.0</AssemblyVersion>
2121
<PackageReleaseNotes>Changes since v0.10.0:
2222
1. Upgrade NumSharp to v0.20.3.
2323
2. Add DisposableObject class to manage object lifetime.
@@ -29,9 +29,11 @@ Docs: https://tensorflownet.readthedocs.io</Description>
2929
8. Add tf.random_normal, tf.constant, tf.pad, tf.shape, tf.image.resize_nearest_neighbor.
3030
9. MultiThread is safe.
3131
10. Support n-dim indexing for tensor.
32-
11. Add RegisterNoGradient</PackageReleaseNotes>
32+
11. Add RegisterNoGradients
33+
12. Add CumsumGrad, BroadcastToGrad.
34+
13. Return VariableV1 instead of RefVariable.</PackageReleaseNotes>
3335
<LangVersion>7.3</LangVersion>
34-
<FileVersion>0.11.5.0</FileVersion>
36+
<FileVersion>0.11.6.0</FileVersion>
3537
<PackageLicenseFile>LICENSE</PackageLicenseFile>
3638
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
3739
<SignAssembly>true</SignAssembly>
@@ -63,12 +65,15 @@ Docs: https://tensorflownet.readthedocs.io</Description>
6365

6466
<ItemGroup>
6567
<PackageReference Include="Google.Protobuf" Version="3.5.1" />
66-
<PackageReference Include="NumSharp" Version="0.20.4" />
6768
</ItemGroup>
6869

6970
<ItemGroup>
7071
<Folder Include="Distribute\" />
7172
<Folder Include="Keras\Initializers\" />
7273
<Folder Include="Models\" />
7374
</ItemGroup>
75+
76+
<ItemGroup>
77+
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
78+
</ItemGroup>
7479
</Project>

src/TensorFlowNET.Core/Variables/RefVariable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private void _init_from_args(object initial_value,
143143
// Use attr_scope and device(None) to simulate the behavior of
144144
// colocate_with when the variable we want to colocate with doesn't
145145
// yet exist.
146-
string true_name = ops._name_from_scope_name(name);
146+
string true_name = ops.name_from_scope_name(name);
147147
var attr = new AttrValue
148148
{
149149
List = new AttrValue.Types.ListValue()

src/TensorFlowNET.Core/Variables/ResourceVariable.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17+
using Google.Protobuf;
1718
using System;
1819
using System.Collections.Generic;
1920
using static Tensorflow.Binding;
@@ -92,7 +93,59 @@ private void _init_from_args(object initial_value = null,
9293
var init_from_fn = initial_value.GetType().Name == "Func`1";
9394
if(collections == null)
9495
collections = new List<string>() { tf.GraphKeys.GLOBAL_VARIABLES };
95-
96+
_trainable = trainable;
97+
_graph_key = ops.get_default_graph().graph_key;
98+
99+
ops.init_scope();
100+
_in_graph_mode = true;
101+
tf_with(ops.name_scope(name, "Variable"), scope =>
102+
{
103+
name = scope;
104+
var handle_name = ops.name_from_scope_name(name);
105+
var shared_name = handle_name;
106+
var unique_id = shared_name;
107+
108+
var attr = new AttrValue();
109+
attr.List = new AttrValue.Types.ListValue();
110+
attr.List.S.Add(ByteString.CopyFromUtf8($"loc:{handle_name}"));
111+
tf_with(ops.name_scope("Initializer"), delegate
112+
{
113+
initial_value = ops.convert_to_tensor(init_from_fn ? (initial_value as Func<Tensor>)() : initial_value,
114+
name: "initial_value",
115+
dtype: dtype);
116+
});
117+
_shape = shape ?? (initial_value as Tensor).TensorShape;
118+
_handle = resource_variable_ops.eager_safe_variable_handle(
119+
initial_value: _initial_value,
120+
shape: _shape,
121+
shared_name: shared_name,
122+
name: name,
123+
graph_mode: _in_graph_mode);
124+
_unique_id = unique_id;
125+
_initial_value = initial_value as Tensor;
126+
_handle_name = handle_name + ":0";
127+
_dtype = _initial_value.dtype.as_base_dtype();
128+
// _constraint = constraint;
129+
130+
if (_in_graph_mode)
131+
{
132+
tf_with(ops.name_scope("IsInitialized"), delegate
133+
{
134+
_is_initialized_op = gen_resource_variable_ops.var_is_initialized_op(_handle);
135+
});
136+
if(initial_value != null)
137+
{
138+
tf_with(ops.name_scope("Assign"), scope1 =>
139+
{
140+
string n = scope1;
141+
_initializer_op = gen_resource_variable_ops.assign_variable_op(_handle,
142+
variables._try_guard_against_uninitialized_dependencies(name, _initial_value),
143+
name: n);
144+
});
145+
}
146+
}
147+
});
148+
96149
throw new NotImplementedException("");
97150
}
98151

src/TensorFlowNET.Core/Variables/VariableV1.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ public abstract class VariableV1
3535
public virtual Operation op { get; }
3636
public virtual Operation initializer { get; }
3737
public Tensor _variable;
38+
protected string _graph_key;
3839
public Graph graph => _variable.graph;
40+
41+
public Tensor _is_initialized_op { get; set; }
42+
3943
public VariableV1(object initial_value = null,
4044
bool trainable = true,
4145
List<string> collections = null,

0 commit comments

Comments
 (0)