Skip to content

Commit c32d153

Browse files
Merge branch 'master' of https://github.com/SciSharp/TensorFlow.NET into alnovi/optimizer_tests
2 parents 2121079 + 94c0bb8 commit c32d153

File tree

71 files changed

+1893
-205
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1893
-205
lines changed

README.md

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,6 @@
1515

1616
English | [中文](docs/README-CN.md)
1717

18-
**=========================================================**
19-
20-
### [Voting: Naming Convention Approach of v1.0.0](https://github.com/SciSharp/TensorFlow.NET/issues/1074)
21-
22-
Dear all,
23-
24-
We would like to urge you to participate in our upcoming vote regarding the naming convention for TensorFlow.NET version 1.0.0 in [#1074](https://github.com/SciSharp/TensorFlow.NET/issues/1074). Your participation in the vote is essential to help us decide on the best approach for improving the naming convention used in previous versions.
25-
26-
Thank you,
27-
28-
TensorFlow.NET Authors
29-
30-
**=========================================================**
31-
3218
*master branch and v0.100.x is corresponding to tensorflow v2.10, v0.6x branch is from tensorflow v2.6, v0.15-tensorflow1.15 is from tensorflow1.15. Please add `https://www.myget.org/F/scisharp/api/v3/index.json` to nuget source to use nightly release.*
3319

3420

@@ -75,9 +61,12 @@ PM> Install-Package TensorFlow.Keras
7561
The second part is the computing support part. Only one of the following packages is needed, depending on your device and system.
7662

7763
```
78-
### CPU version for Windows, Linux and Mac
64+
### CPU version for Windows and Linux
7965
PM> Install-Package SciSharp.TensorFlow.Redist
8066
67+
### CPU version for MacOS
68+
PM> Install-Package SciSharp.TensorFlow.Redist-OSX
69+
8170
### GPU version for Windows (CUDA and cuDNN are required)
8271
PM> Install-Package SciSharp.TensorFlow.Redist-Windows-GPU
8372

TensorFlow.NET.sln

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Benchmark", "too
3939
EndProject
4040
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Console", "tools\TensorFlowNET.Console\Tensorflow.Console.csproj", "{1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}"
4141
EndProject
42+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TensorFlow.Kernel.UnitTest", "test\TensorFlow.Kernel.UnitTest\TensorFlow.Kernel.UnitTest.csproj", "{654A027D-1364-4729-880B-144DFE1FF5BB}"
43+
EndProject
4244
Global
4345
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4446
Debug|Any CPU = Debug|Any CPU
@@ -322,6 +324,24 @@ Global
322324
{1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.Release|x64.Build.0 = Release|x64
323325
{1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.Release|x86.ActiveCfg = Release|Any CPU
324326
{1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}.Release|x86.Build.0 = Release|Any CPU
327+
{654A027D-1364-4729-880B-144DFE1FF5BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
328+
{654A027D-1364-4729-880B-144DFE1FF5BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
329+
{654A027D-1364-4729-880B-144DFE1FF5BB}.Debug|x64.ActiveCfg = Debug|Any CPU
330+
{654A027D-1364-4729-880B-144DFE1FF5BB}.Debug|x64.Build.0 = Debug|Any CPU
331+
{654A027D-1364-4729-880B-144DFE1FF5BB}.Debug|x86.ActiveCfg = Debug|Any CPU
332+
{654A027D-1364-4729-880B-144DFE1FF5BB}.Debug|x86.Build.0 = Debug|Any CPU
333+
{654A027D-1364-4729-880B-144DFE1FF5BB}.GPU|Any CPU.ActiveCfg = Debug|Any CPU
334+
{654A027D-1364-4729-880B-144DFE1FF5BB}.GPU|Any CPU.Build.0 = Debug|Any CPU
335+
{654A027D-1364-4729-880B-144DFE1FF5BB}.GPU|x64.ActiveCfg = Debug|Any CPU
336+
{654A027D-1364-4729-880B-144DFE1FF5BB}.GPU|x64.Build.0 = Debug|Any CPU
337+
{654A027D-1364-4729-880B-144DFE1FF5BB}.GPU|x86.ActiveCfg = Debug|Any CPU
338+
{654A027D-1364-4729-880B-144DFE1FF5BB}.GPU|x86.Build.0 = Debug|Any CPU
339+
{654A027D-1364-4729-880B-144DFE1FF5BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
340+
{654A027D-1364-4729-880B-144DFE1FF5BB}.Release|Any CPU.Build.0 = Release|Any CPU
341+
{654A027D-1364-4729-880B-144DFE1FF5BB}.Release|x64.ActiveCfg = Release|Any CPU
342+
{654A027D-1364-4729-880B-144DFE1FF5BB}.Release|x64.Build.0 = Release|Any CPU
343+
{654A027D-1364-4729-880B-144DFE1FF5BB}.Release|x86.ActiveCfg = Release|Any CPU
344+
{654A027D-1364-4729-880B-144DFE1FF5BB}.Release|x86.Build.0 = Release|Any CPU
325345
EndGlobalSection
326346
GlobalSection(SolutionProperties) = preSolution
327347
HideSolutionNode = FALSE
@@ -342,6 +362,7 @@ Global
342362
{D24FCAA5-548C-4251-B226-A1B6535D0845} = {E1A5D2B7-10AF-4876-85C0-7714EF274214}
343363
{C23563DB-FE21-48E7-A411-87A109E4A899} = {E1A5D2B7-10AF-4876-85C0-7714EF274214}
344364
{1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0} = {E1A5D2B7-10AF-4876-85C0-7714EF274214}
365+
{654A027D-1364-4729-880B-144DFE1FF5BB} = {1B0918B9-65AD-4F34-A287-AF4597B27DBD}
345366
EndGlobalSection
346367
GlobalSection(ExtensibilityGlobals) = postSolution
347368
SolutionGuid = {2DEAD3CC-486B-4918-A607-50B0DE7B114A}

data/img001.bmp

174 KB
Binary file not shown.

src/TensorFlowNET.Core/APIs/c_api.customize.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ namespace Tensorflow
88
public partial class c_api
99
{
1010
[DllImport(TensorFlowLibName)]
11-
public static extern void TFC_SetAttr(SafeGraphHandle graph, IntPtr op, string attr_name, SafeBufferHandle attr_value_proto, SafeStatusHandle status);
11+
public static extern void TF_SetAttr(SafeGraphHandle graph, IntPtr op, string attr_name, SafeBufferHandle attr_value_proto, SafeStatusHandle status);
1212
[DllImport(TensorFlowLibName)]
13-
public static extern SafeBufferHandle TFC_GetHandleShapeAndType(SafeGraphHandle c_graph, TF_Output output);
13+
public static extern SafeBufferHandle TF_GetHandleShapeAndType(SafeGraphHandle c_graph, TF_Output output);
1414
[DllImport(TensorFlowLibName)]
15-
public static extern void TFC_SetHandleShapeAndType(SafeGraphHandle c_graph, TF_Output output, byte[] data, long proto_len, SafeStatusHandle status);
15+
public static extern void TF_SetHandleShapeAndType(SafeGraphHandle c_graph, TF_Output output, byte[] data, long proto_len, SafeStatusHandle status);
1616
}
1717
}

src/TensorFlowNET.Core/APIs/tf.array.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ public Tensor identity(Tensor input, string name = null)
140140
public Tensor gather(Tensor @params, Tensor indices, string name = null, int axis = 0)
141141
=> array_ops.gather(@params, indices, name: name, axis: ops.convert_to_tensor(axis));
142142

143+
/// <summary>
144+
/// Gather slices from `params` into a Tensor with shape specified by `indices`.
145+
/// </summary>
146+
/// <param name="params"></param>
147+
/// <param name="indices"></param>
148+
/// <param name="name"></param>
149+
/// <returns></returns>
150+
public Tensor gather_nd(Tensor @params, Tensor indices, string name = null)
151+
=> gen_array_ops.gather_nd(@params, indices, name: name);
152+
143153
/// <summary>
144154
/// Return the elements, either from `x` or `y`, depending on the `condition`.
145155
/// </summary>

src/TensorFlowNET.Core/APIs/tf.image.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,13 @@ public Tensor decode_image(Tensor contents, int channels = 0, TF_DataType dtype
339339
=> image_ops_impl.decode_image(contents, channels: channels, dtype: dtype,
340340
name: name, expand_animations: expand_animations);
341341

342+
public Tensor encode_png(Tensor contents, string name = null)
343+
=> image_ops_impl.encode_png(contents, name: name);
344+
345+
public Tensor encode_jpeg(Tensor contents, string name = null)
346+
=> image_ops_impl.encode_jpeg(contents, name: name);
347+
348+
342349
/// <summary>
343350
/// Convenience function to check if the 'contents' encodes a JPEG image.
344351
/// </summary>

src/TensorFlowNET.Core/APIs/tf.io.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
using System.Collections.Generic;
1818
using Tensorflow.IO;
19+
using Tensorflow.Operations;
1920

2021
namespace Tensorflow
2122
{
@@ -46,6 +47,12 @@ public Operation save_v2(Tensor prefix, string[] tensor_names,
4647
public Tensor[] restore_v2(Tensor prefix, string[] tensor_names,
4748
string[] shape_and_slices, TF_DataType[] dtypes, string name = null)
4849
=> ops.restore_v2(prefix, tensor_names, shape_and_slices, dtypes, name: name);
50+
51+
public Operation write_file(string filename, Tensor conentes, string name = null)
52+
=> write_file(Tensorflow.ops.convert_to_tensor(filename, TF_DataType.TF_STRING), conentes, name);
53+
54+
public Operation write_file(Tensor filename, Tensor conentes, string name = null)
55+
=> gen_ops.write_file(filename, conentes, name);
4956
}
5057

5158
public GFile gfile = new GFile();

src/TensorFlowNET.Core/APIs/tf.nn.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public Tensor embedding_lookup(Tensor @params,
101101
name: name);
102102

103103
public IActivation relu() => new relu();
104+
105+
104106
public IActivation swish() => new swish();
105107
public IActivation tanh() => new tanh();
106108

@@ -111,6 +113,9 @@ public Tensor tanh(Tensor x, string name = null)
111113
public Tensor relu(Tensor features, string name = null)
112114
=> gen_nn_ops.relu(features, name);
113115

116+
public Tensor relu6(Tensor features, string name = null)
117+
=> gen_nn_ops.relu6(features, name);
118+
114119
public Tensor[] fused_batch_norm(Tensor x,
115120
Tensor scale,
116121
Tensor offset,

src/TensorFlowNET.Core/Eager/EagerRunner.RecordGradient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ BackwardFunction GetGradientFunction(string op_name,
8080
Tensor[] op_outputs)
8181
=> (out_grads, unneeded_gradients) =>
8282
{
83+
if(!ops.gradientFunctions.ContainsKey(op_name))
84+
{
85+
throw new Exception($"gradientFunctions not find op_name: {op_name}");
86+
}
87+
8388
if (ops.gradientFunctions[op_name] == null)
8489
return new Tensor[op_inputs.Length];
8590

src/TensorFlowNET.Core/Gradients/array_grad.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,5 +381,48 @@ public static Tensor[] _ReverseV2Grad(Operation op, Tensor[] grads)
381381
var axis = op.inputs[1];
382382
return new Tensor[] { array_ops.reverse(grad, axis), null };
383383
}
384+
385+
[RegisterGradient("Tile")]
386+
public static Tensor[] _TileGrad(Operation op, Tensor[] grads)
387+
{
388+
var grad = grads[0];
389+
var input_shape = array_ops.shape(op.inputs[0], out_type: op.inputs[1].dtype);
390+
var split_shape = array_ops.reshape(array_ops.transpose(array_ops.stack(new Tensor[] { op.inputs[1], input_shape })), new Shape(-1));
391+
var axes = math_ops.range(0, array_ops.size(split_shape), 2);
392+
393+
//# Sum reduces grad along the first dimension for IndexedSlices
394+
//if isinstance(grad, indexed_slices_lib.IndexedSlices):
395+
//input_shape_0 = math_ops.cast(input_shape[0], grad.indices.dtype)
396+
//grad = math_ops.unsorted_segment_sum(
397+
// grad.values, math_ops.mod(grad.indices, input_shape_0), input_shape_0)
398+
//split_shape = array_ops.concat([[1], split_shape[1:]], axis = 0)
399+
400+
var input_grad = math_ops.reduce_sum(array_ops.reshape(grad, split_shape), axes);
401+
if (!tf.Context.executing_eagerly())
402+
{
403+
input_grad.set_shape(op.inputs[0].GetShape());
404+
}
405+
return new Tensor[] { input_grad, null };
406+
}
407+
408+
[RegisterGradient("GatherNd")]
409+
public static Tensor[] _GatherNdGrad(Operation op, Tensor[] grads)
410+
{
411+
var @ref = op.inputs[0];
412+
var indices = op.inputs[1];
413+
var grad = grads[0];
414+
var ref_shape = array_ops.shape(@ref, out_type: indices.dtype);
415+
Tensor ref_grad = null;
416+
if (indices.shape.ndim == 2 && indices.shape.dims[indices.shape.Length - 1] == 1)
417+
{
418+
ref_grad = (Tensor)new IndexedSlices(grad, array_ops.squeeze(indices, axis: -1), ref_shape);
419+
}
420+
else
421+
{
422+
ref_grad = gen_array_ops.scatter_nd(indices, grad, ref_shape);
423+
}
424+
return new Tensor[] { ref_grad, null };
425+
}
426+
384427
}
385428
}

0 commit comments

Comments
 (0)