Browse Source

create ResourceVariable, still with buggy.

tags/v0.12
Oceania2018 6 years ago
parent
commit
02871910c1
14 changed files with 229 additions and 12 deletions
  1. +66
    -0
      TensorFlow.NET.sln
  2. +1
    -0
      src/TensorFlowNET.Core/Binding.Util.cs
  3. +2
    -2
      src/TensorFlowNET.Core/Graphs/Graph.cs
  4. +2
    -1
      src/TensorFlowNET.Core/Keras/Utils/base_layer_utils.cs
  5. +7
    -0
      src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs
  6. +23
    -0
      src/TensorFlowNET.Core/Operations/resource_variable_ops.cs
  7. +10
    -5
      src/TensorFlowNET.Core/TensorFlowNET.Core.csproj
  8. +1
    -1
      src/TensorFlowNET.Core/Variables/RefVariable.cs
  9. +54
    -1
      src/TensorFlowNET.Core/Variables/ResourceVariable.cs
  10. +4
    -0
      src/TensorFlowNET.Core/Variables/VariableV1.cs
  11. +54
    -0
      src/TensorFlowNET.Core/Variables/variables.py.cs
  12. +1
    -1
      src/TensorFlowNET.Core/ops.cs
  13. +1
    -1
      src/TensorFlowNET.Hub/TensorFlowNET.Hub.csproj
  14. +3
    -0
      test/TensorFlowNET.UnitTest/Keras/EmbeddingTest.cs

+ 66
- 0
TensorFlow.NET.sln View File

@@ -15,36 +15,102 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Hub", "src\Te
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Datasets", "src\TensorFlowNET.Datasets\TensorFlowNET.Datasets.csproj", "{494D6CAD-2C0D-4C0B-90E2-B097DB039383}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Datasets", "src\TensorFlowNET.Datasets\TensorFlowNET.Datasets.csproj", "{494D6CAD-2C0D-4C0B-90E2-B097DB039383}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Publish|Any CPU = Publish|Any CPU
Publish|x64 = Publish|x64
Release|Any CPU = Release|Any CPU Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Debug|Any CPU.Build.0 = Debug|Any CPU {029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Debug|x64.ActiveCfg = Debug|Any CPU
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Debug|x64.Build.0 = Debug|Any CPU
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Publish|Any CPU.ActiveCfg = Release|Any CPU
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Publish|Any CPU.Build.0 = Release|Any CPU
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Publish|x64.ActiveCfg = Release|Any CPU
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Publish|x64.Build.0 = Release|Any CPU
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Release|Any CPU.ActiveCfg = Release|Any CPU {029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Release|Any CPU.Build.0 = Release|Any CPU {029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Release|Any CPU.Build.0 = Release|Any CPU
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Release|x64.ActiveCfg = Release|Any CPU
{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}.Release|x64.Build.0 = Release|Any CPU
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|Any CPU.Build.0 = Debug|Any CPU {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|x64.ActiveCfg = Debug|Any CPU
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|x64.Build.0 = Debug|Any CPU
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Publish|Any CPU.ActiveCfg = Release|Any CPU
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Publish|Any CPU.Build.0 = Release|Any CPU
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Publish|x64.ActiveCfg = Release|Any CPU
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Publish|x64.Build.0 = Release|Any CPU
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.ActiveCfg = Release|Any CPU {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.Build.0 = Release|Any CPU {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.Build.0 = Release|Any CPU
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|x64.ActiveCfg = Release|Any CPU
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|x64.Build.0 = Release|Any CPU
{D03F94CF-B283-4730-B177-21A57641061F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D03F94CF-B283-4730-B177-21A57641061F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D03F94CF-B283-4730-B177-21A57641061F}.Debug|Any CPU.Build.0 = Debug|Any CPU {D03F94CF-B283-4730-B177-21A57641061F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D03F94CF-B283-4730-B177-21A57641061F}.Debug|x64.ActiveCfg = Debug|Any CPU
{D03F94CF-B283-4730-B177-21A57641061F}.Debug|x64.Build.0 = Debug|Any CPU
{D03F94CF-B283-4730-B177-21A57641061F}.Publish|Any CPU.ActiveCfg = Release|Any CPU
{D03F94CF-B283-4730-B177-21A57641061F}.Publish|Any CPU.Build.0 = Release|Any CPU
{D03F94CF-B283-4730-B177-21A57641061F}.Publish|x64.ActiveCfg = Release|Any CPU
{D03F94CF-B283-4730-B177-21A57641061F}.Publish|x64.Build.0 = Release|Any CPU
{D03F94CF-B283-4730-B177-21A57641061F}.Release|Any CPU.ActiveCfg = Release|Any CPU {D03F94CF-B283-4730-B177-21A57641061F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D03F94CF-B283-4730-B177-21A57641061F}.Release|Any CPU.Build.0 = Release|Any CPU {D03F94CF-B283-4730-B177-21A57641061F}.Release|Any CPU.Build.0 = Release|Any CPU
{D03F94CF-B283-4730-B177-21A57641061F}.Release|x64.ActiveCfg = Release|Any CPU
{D03F94CF-B283-4730-B177-21A57641061F}.Release|x64.Build.0 = Release|Any CPU
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {904472F8-40E1-4650-AA6F-C7F209B3691B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Debug|Any CPU.Build.0 = Debug|Any CPU {904472F8-40E1-4650-AA6F-C7F209B3691B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Debug|x64.ActiveCfg = Debug|Any CPU
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Debug|x64.Build.0 = Debug|Any CPU
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Publish|Any CPU.ActiveCfg = Release|Any CPU
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Publish|Any CPU.Build.0 = Release|Any CPU
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Publish|x64.ActiveCfg = Release|Any CPU
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Publish|x64.Build.0 = Release|Any CPU
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Release|Any CPU.ActiveCfg = Release|Any CPU {904472F8-40E1-4650-AA6F-C7F209B3691B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Release|Any CPU.Build.0 = Release|Any CPU {904472F8-40E1-4650-AA6F-C7F209B3691B}.Release|Any CPU.Build.0 = Release|Any CPU
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Release|x64.ActiveCfg = Release|Any CPU
{904472F8-40E1-4650-AA6F-C7F209B3691B}.Release|x64.Build.0 = Release|Any CPU
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Debug|Any CPU.Build.0 = Debug|Any CPU {4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Debug|x64.ActiveCfg = Debug|Any CPU
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Debug|x64.Build.0 = Debug|Any CPU
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Publish|Any CPU.ActiveCfg = Release|Any CPU
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Publish|Any CPU.Build.0 = Release|Any CPU
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Publish|x64.ActiveCfg = Release|Any CPU
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Publish|x64.Build.0 = Release|Any CPU
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Release|Any CPU.ActiveCfg = Release|Any CPU {4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Release|Any CPU.Build.0 = Release|Any CPU {4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Release|Any CPU.Build.0 = Release|Any CPU
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Release|x64.ActiveCfg = Release|Any CPU
{4EAFAE19-C832-47C6-B01E-0F4268C9072C}.Release|x64.Build.0 = Release|Any CPU
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Debug|Any CPU.Build.0 = Debug|Any CPU {494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Debug|Any CPU.Build.0 = Debug|Any CPU
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Debug|x64.ActiveCfg = Debug|Any CPU
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Debug|x64.Build.0 = Debug|Any CPU
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Publish|Any CPU.ActiveCfg = Release|Any CPU
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Publish|Any CPU.Build.0 = Release|Any CPU
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Publish|x64.ActiveCfg = Release|Any CPU
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Publish|x64.Build.0 = Release|Any CPU
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Release|Any CPU.ActiveCfg = Release|Any CPU {494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Release|Any CPU.ActiveCfg = Release|Any CPU
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Release|Any CPU.Build.0 = Release|Any CPU {494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Release|Any CPU.Build.0 = Release|Any CPU
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Release|x64.ActiveCfg = Release|Any CPU
{494D6CAD-2C0D-4C0B-90E2-B097DB039383}.Release|x64.Build.0 = Release|Any CPU
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Debug|x64.ActiveCfg = Debug|x64
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Debug|x64.Build.0 = Debug|x64
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Publish|Any CPU.ActiveCfg = Publish|Any CPU
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Publish|Any CPU.Build.0 = Publish|Any CPU
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Publish|x64.ActiveCfg = Publish|x64
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Publish|x64.Build.0 = Publish|x64
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Release|Any CPU.Build.0 = Release|Any CPU
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Release|x64.ActiveCfg = Release|x64
{9249BCC4-3FEB-4EF5-8AB9-789FFE4040B4}.Release|x64.Build.0 = Release|x64
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE


+ 1
- 0
src/TensorFlowNET.Core/Binding.Util.cs View File

@@ -113,6 +113,7 @@ namespace Tensorflow
} }
} }


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


+ 2
- 2
src/TensorFlowNET.Core/Graphs/Graph.cs View File

@@ -266,7 +266,7 @@ namespace Tensorflow
name = op_type; name = op_type;
// If a names ends with a '/' it is a "name scope" and we use it as-is, // If a names ends with a '/' it is a "name scope" and we use it as-is,
// after removing the trailing '/'. // after removing the trailing '/'.
name = name.EndsWith("/") ? ops._name_from_scope_name(name) : unique_name(name);
name = name.EndsWith("/") ? ops.name_from_scope_name(name) : unique_name(name);
var node_def = ops._NodeDef(op_type, name, device: "", attrs: attrs); var node_def = ops._NodeDef(op_type, name, device: "", attrs: attrs);


var input_ops = inputs.Select(x => x.op).ToArray(); var input_ops = inputs.Select(x => x.op).ToArray();
@@ -341,7 +341,7 @@ namespace Tensorflow
if (string.IsNullOrEmpty(name)) if (string.IsNullOrEmpty(name))
new_stack = ""; new_stack = "";
else if (name.EndsWith("/")) else if (name.EndsWith("/"))
new_stack = ops._name_from_scope_name(name);
new_stack = ops.name_from_scope_name(name);
else else
new_stack = unique_name(name); new_stack = unique_name(name);




+ 2
- 1
src/TensorFlowNET.Core/Keras/Utils/base_layer_utils.cs View File

@@ -49,7 +49,8 @@ namespace Tensorflow.Keras.Utils
var v = tf.VariableV1(init_val, var v = tf.VariableV1(init_val,
use_resource: use_resource, use_resource: use_resource,
dtype: dtype, dtype: dtype,
shape: shape);
shape: shape,
name: name);


return v; return v;
} }


+ 7
- 0
src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs View File

@@ -26,5 +26,12 @@ namespace Tensorflow


return _op; return _op;
} }

public static Tensor var_is_initialized_op(Tensor resource, string name = null)
{
var _op = _op_def_lib._apply_op_helper("VarIsInitializedOp", name, new { resource });

return _op;
}
} }
} }

+ 23
- 0
src/TensorFlowNET.Core/Operations/resource_variable_ops.cs View File

@@ -74,6 +74,29 @@ namespace Tensorflow
return var is ResourceVariable; return var is ResourceVariable;
} }


/// <summary>
/// Creates a variable handle with information to do shape inference.
/// </summary>
/// <param name="initial_value"></param>
/// <param name="shape"></param>
/// <param name="shared_name"></param>
/// <param name="name"></param>
/// <param name="graph_mode"></param>
/// <returns></returns>
public static Tensor eager_safe_variable_handle(Tensor initial_value, TensorShape shape,
string shared_name, string name, bool graph_mode)
{
var dtype = initial_value.dtype.as_base_dtype();
return variable_handle_from_shape_and_dtype(
shape, dtype, shared_name, name, graph_mode, initial_value);
}

public static Tensor variable_handle_from_shape_and_dtype(TensorShape shape, TF_DataType dtype,
string shared_name, string name, bool graph_mode, Tensor extra_handle_data = null)
{
throw new NotImplementedException("");
}

/// <summary> /// <summary>
/// Represents a future for a read of a variable. /// Represents a future for a read of a variable.
/// Pretends to be the tensor if anyone looks. /// Pretends to be the tensor if anyone looks.


+ 10
- 5
src/TensorFlowNET.Core/TensorFlowNET.Core.csproj View File

@@ -5,7 +5,7 @@
<AssemblyName>TensorFlow.NET</AssemblyName> <AssemblyName>TensorFlow.NET</AssemblyName>
<RootNamespace>Tensorflow</RootNamespace> <RootNamespace>Tensorflow</RootNamespace>
<TargetTensorFlow>1.14.0</TargetTensorFlow> <TargetTensorFlow>1.14.0</TargetTensorFlow>
<Version>0.11.5</Version>
<Version>0.11.6</Version>
<Authors>Haiping Chen, Meinrad Recheis, Eli Belash</Authors> <Authors>Haiping Chen, Meinrad Recheis, Eli Belash</Authors>
<Company>SciSharp STACK</Company> <Company>SciSharp STACK</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
@@ -17,7 +17,7 @@
<PackageTags>TensorFlow, NumSharp, SciSharp, MachineLearning, TensorFlow.NET, C#</PackageTags> <PackageTags>TensorFlow, NumSharp, SciSharp, MachineLearning, TensorFlow.NET, C#</PackageTags>
<Description>Google's TensorFlow full binding in .NET Standard. <Description>Google's TensorFlow full binding in .NET Standard.
Docs: https://tensorflownet.readthedocs.io</Description> Docs: https://tensorflownet.readthedocs.io</Description>
<AssemblyVersion>0.11.5.0</AssemblyVersion>
<AssemblyVersion>0.11.6.0</AssemblyVersion>
<PackageReleaseNotes>Changes since v0.10.0: <PackageReleaseNotes>Changes since v0.10.0:
1. Upgrade NumSharp to v0.20.3. 1. Upgrade NumSharp to v0.20.3.
2. Add DisposableObject class to manage object lifetime. 2. Add DisposableObject class to manage object lifetime.
@@ -29,9 +29,11 @@ Docs: https://tensorflownet.readthedocs.io</Description>
8. Add tf.random_normal, tf.constant, tf.pad, tf.shape, tf.image.resize_nearest_neighbor. 8. Add tf.random_normal, tf.constant, tf.pad, tf.shape, tf.image.resize_nearest_neighbor.
9. MultiThread is safe. 9. MultiThread is safe.
10. Support n-dim indexing for tensor. 10. Support n-dim indexing for tensor.
11. Add RegisterNoGradient</PackageReleaseNotes>
11. Add RegisterNoGradients
12. Add CumsumGrad, BroadcastToGrad.
13. Return VariableV1 instead of RefVariable.</PackageReleaseNotes>
<LangVersion>7.3</LangVersion> <LangVersion>7.3</LangVersion>
<FileVersion>0.11.5.0</FileVersion>
<FileVersion>0.11.6.0</FileVersion>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance> <PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<SignAssembly>true</SignAssembly> <SignAssembly>true</SignAssembly>
@@ -63,7 +65,6 @@ Docs: https://tensorflownet.readthedocs.io</Description>


<ItemGroup> <ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.5.1" /> <PackageReference Include="Google.Protobuf" Version="3.5.1" />
<PackageReference Include="NumSharp" Version="0.20.4" />
</ItemGroup> </ItemGroup>


<ItemGroup> <ItemGroup>
@@ -71,4 +72,8 @@ Docs: https://tensorflownet.readthedocs.io</Description>
<Folder Include="Keras\Initializers\" /> <Folder Include="Keras\Initializers\" />
<Folder Include="Models\" /> <Folder Include="Models\" />
</ItemGroup> </ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
</ItemGroup>
</Project> </Project>

+ 1
- 1
src/TensorFlowNET.Core/Variables/RefVariable.cs View File

@@ -143,7 +143,7 @@ namespace Tensorflow
// Use attr_scope and device(None) to simulate the behavior of // Use attr_scope and device(None) to simulate the behavior of
// colocate_with when the variable we want to colocate with doesn't // colocate_with when the variable we want to colocate with doesn't
// yet exist. // yet exist.
string true_name = ops._name_from_scope_name(name);
string true_name = ops.name_from_scope_name(name);
var attr = new AttrValue var attr = new AttrValue
{ {
List = new AttrValue.Types.ListValue() List = new AttrValue.Types.ListValue()


+ 54
- 1
src/TensorFlowNET.Core/Variables/ResourceVariable.cs View File

@@ -14,6 +14,7 @@
limitations under the License. limitations under the License.
******************************************************************************/ ******************************************************************************/


using Google.Protobuf;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using static Tensorflow.Binding; using static Tensorflow.Binding;
@@ -92,7 +93,59 @@ namespace Tensorflow
var init_from_fn = initial_value.GetType().Name == "Func`1"; var init_from_fn = initial_value.GetType().Name == "Func`1";
if(collections == null) if(collections == null)
collections = new List<string>() { tf.GraphKeys.GLOBAL_VARIABLES }; collections = new List<string>() { tf.GraphKeys.GLOBAL_VARIABLES };
_trainable = trainable;
_graph_key = ops.get_default_graph().graph_key;

ops.init_scope();
_in_graph_mode = true;
tf_with(ops.name_scope(name, "Variable"), scope =>
{
name = scope;
var handle_name = ops.name_from_scope_name(name);
var shared_name = handle_name;
var unique_id = shared_name;

var attr = new AttrValue();
attr.List = new AttrValue.Types.ListValue();
attr.List.S.Add(ByteString.CopyFromUtf8($"loc:{handle_name}"));
tf_with(ops.name_scope("Initializer"), delegate
{
initial_value = ops.convert_to_tensor(init_from_fn ? (initial_value as Func<Tensor>)() : initial_value,
name: "initial_value",
dtype: dtype);
});
_shape = shape ?? (initial_value as Tensor).TensorShape;
_handle = resource_variable_ops.eager_safe_variable_handle(
initial_value: _initial_value,
shape: _shape,
shared_name: shared_name,
name: name,
graph_mode: _in_graph_mode);
_unique_id = unique_id;
_initial_value = initial_value as Tensor;
_handle_name = handle_name + ":0";
_dtype = _initial_value.dtype.as_base_dtype();
// _constraint = constraint;

if (_in_graph_mode)
{
tf_with(ops.name_scope("IsInitialized"), delegate
{
_is_initialized_op = gen_resource_variable_ops.var_is_initialized_op(_handle);
});
if(initial_value != null)
{
tf_with(ops.name_scope("Assign"), scope1 =>
{
string n = scope1;
_initializer_op = gen_resource_variable_ops.assign_variable_op(_handle,
variables._try_guard_against_uninitialized_dependencies(name, _initial_value),
name: n);
});
}
}
});

throw new NotImplementedException(""); throw new NotImplementedException("");
} }




+ 4
- 0
src/TensorFlowNET.Core/Variables/VariableV1.cs View File

@@ -35,7 +35,11 @@ namespace Tensorflow
public virtual Operation op { get; } public virtual Operation op { get; }
public virtual Operation initializer { get; } public virtual Operation initializer { get; }
public Tensor _variable; public Tensor _variable;
protected string _graph_key;
public Graph graph => _variable.graph; public Graph graph => _variable.graph;

public Tensor _is_initialized_op { get; set; }

public VariableV1(object initial_value = null, public VariableV1(object initial_value = null,
bool trainable = true, bool trainable = true,
List<string> collections = null, List<string> collections = null,


+ 54
- 0
src/TensorFlowNET.Core/Variables/variables.py.cs View File

@@ -84,6 +84,60 @@ namespace Tensorflow
return gen_control_flow_ops.no_op(name: name); return gen_control_flow_ops.no_op(name: name);
} }


public static Tensor _try_guard_against_uninitialized_dependencies(string name, Tensor initial_value)
{
return _safe_initial_value_from_tensor(name, initial_value, new Dictionary<string, Operation>());
}

public static Tensor _safe_initial_value_from_tensor(string name, Tensor tensor, Dictionary<string, Operation> op_cache)
{
var op = tensor.op;
Operation new_op = op_cache.ContainsKey(op.name) ? op_cache[op.name] : null;
if(new_op == null)
{
new_op = _safe_initial_value_from_op(name, op, op_cache);
op_cache[op.name] = new_op;
}

return new_op.outputs[tensor.value_index];
}

/// <summary>
/// Replace dependencies on variables with their initialized values.
/// </summary>
/// <param name="name"></param>
/// <param name="op"></param>
/// <param name="op_cache"></param>
/// <returns></returns>
public static Operation _safe_initial_value_from_op(string name, Operation op, Dictionary<string, Operation> op_cache)
{
var op_type = op.node_def.Op;
if (op_type == "IsVariableInitialized" ||
op_type == "VarIsInitializedOp" ||
op_type == "ReadVariableOp")
return op;

if(op_type == "Variable" ||
op_type == "VariableV2" ||
op_type == "VarHandleOp")
{
throw new NotImplementedException("");
}

// Recursively build initializer expressions for inputs.
bool modified = false;
var new_op_inputs = new List<Tensor>();
foreach(Tensor op_input in op.inputs)
{
var new_op_input = _safe_initial_value_from_tensor(name, op_input, op_cache);
new_op_inputs.Add(new_op_input);
modified = modified || new_op_input != op_input;
}

// If at least one input was modified, replace the op.
return op;
}

public static Tensor global_variables_initializer() public static Tensor global_variables_initializer()
{ {
throw new NotImplementedException(); throw new NotImplementedException();


+ 1
- 1
src/TensorFlowNET.Core/ops.cs View File

@@ -274,7 +274,7 @@ namespace Tensorflow
return node_def; return node_def;
} }


public static string _name_from_scope_name(string name)
public static string name_from_scope_name(string name)
{ {
if (name.EndsWith("/")) if (name.EndsWith("/"))
{ {


+ 1
- 1
src/TensorFlowNET.Hub/TensorFlowNET.Hub.csproj View File

@@ -18,6 +18,6 @@
<AssemblyName>TensorFlow.Hub</AssemblyName> <AssemblyName>TensorFlow.Hub</AssemblyName>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="NumSharp" Version="0.20.3" />
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

+ 3
- 0
test/TensorFlowNET.UnitTest/Keras/EmbeddingTest.cs View File

@@ -8,6 +8,9 @@ using NumSharp;


namespace TensorFlowNET.UnitTest.Keras namespace TensorFlowNET.UnitTest.Keras
{ {
/// <summary>
/// https://www.tensorflow.org/versions/r1.14/api_docs/python/tf/keras/layers/Embedding
/// </summary>
[TestClass] [TestClass]
public class EmbeddingTest public class EmbeddingTest
{ {


Loading…
Cancel
Save