Skip to content

Commit 1d90fbf

Browse files
Merge master
2 parents 3db1b28 + 0142174 commit 1d90fbf

File tree

107 files changed

+3253
-2947
lines changed

Some content is hidden

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

107 files changed

+3253
-2947
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[![Badge](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu/#/en_US)
1010
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/javiercp/BinderTF.NET/master?urlpath=lab)
1111

12-
*master branch is based on tensorflow 2.3 now, v0.15-tensorflow1.15 is from tensorflow1.15.*
12+
*master branch is based on tensorflow v2.4, v0.3x branch is based on tensorflow v2.3, v0.15-tensorflow1.15 is from tensorflow1.15.*
1313

1414

1515
![tensors_flowing](docs/assets/tensors_flowing.gif)
@@ -30,7 +30,8 @@ Go through the online docs [TensorFlow for .NET](https://scisharp.github.io/tens
3030

3131
| TensorFlow | tf native1.14, cuda 10.0 | tf native 1.15, cuda 10.0 | tf native 2.3, cuda 10.1 | tf native 2.4, cuda 11 |
3232
| -------------------------- | ------------- | -------------- | ------------- | ------------- |
33-
| tf.net 0.3x, tf.keras 0.2 | | | x | not compatible |
33+
| tf.net 0.4x, tf.keras 0.5 | | | | x |
34+
| tf.net 0.3x, tf.keras 0.4 | | | x | |
3435
| tf.net 0.2x | | x | x | |
3536
| tf.net 0.15 | x | x | | |
3637
| tf.net 0.14 | x | | | |
@@ -50,10 +51,10 @@ PM> Install-Package TensorFlow.Keras
5051

5152
### Install tensorflow binary
5253
### For CPU version
53-
PM> Install-Package SciSharp.TensorFlow.Redist -Version 2.3.1
54+
PM> Install-Package SciSharp.TensorFlow.Redist
5455

5556
### For GPU version (CUDA and cuDNN are required)
56-
PM> Install-Package SciSharp.TensorFlow.Redist-Windows-GPU -Version 2.3.1
57+
PM> Install-Package SciSharp.TensorFlow.Redist-Windows-GPU
5758
```
5859

5960
Import TF.NET and Keras API in your project.

TensorFlow.NET.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Binding", "src\T
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Benchmark", "src\TensorFlowNet.Benchmarks\Tensorflow.Benchmark.csproj", "{3A6EB896-604F-4E25-B677-B8103BCF3D2E}"
99
EndProject
10-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.UnitTest", "test\TensorFlowNET.UnitTest\Tensorflow.UnitTest.csproj", "{23C28035-2FCE-41F3-9A12-E73CE8A5AE32}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Binding.UnitTest", "test\TensorFlowNET.UnitTest\Tensorflow.Binding.UnitTest.csproj", "{23C28035-2FCE-41F3-9A12-E73CE8A5AE32}"
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Console", "src\TensorFlowNET.Console\Tensorflow.Console.csproj", "{03F06299-3F4B-4449-A709-3A647657BC0C}"
1313
EndProject

src/TensorFlowNET.Console/MemoryBasicTest.cs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using Tensorflow.Keras.Engine.DataAdapters;
55
using static Tensorflow.Binding;
66
using static Tensorflow.KerasApi;
7+
using System.Linq;
8+
using System.Collections.Generic;
79

810
namespace Tensorflow
911
{
@@ -35,13 +37,15 @@ public Action<int, int> Constant2x3
3537
public Action<int, int> ConstantString
3638
=> (epoch, iterate) =>
3739
{
38-
var tensor = tf.constant(new string[]
40+
var strList = new string[]
3941
{
4042
"Biden immigration bill would put millions of illegal immigrants on 8-year fast-track to citizenship",
4143
"The Associated Press, which also reported that the eight-year path is in the bill.",
4244
"The bill would also include provisions to stem the flow of migration by addressing root causes of migration from south of the border."
43-
});
44-
var data = tensor.numpy();
45+
};
46+
47+
var tensor = tf.constant(strList, TF_DataType.TF_STRING);
48+
var data = tensor.StringData();
4549
};
4650

4751
public Action<int, int> Variable
@@ -108,16 +112,18 @@ public Action<int, int> Conv2DWithTensor
108112
var strides = new[] { 1, 1, 1, 1 };
109113
var dilations = new[] { 1, 1, 1, 1 };
110114

111-
var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName,
112-
"Conv2D", null,
113-
null,
114-
input, filter,
115-
"strides", strides,
116-
"use_cudnn_on_gpu", true,
117-
"padding", "VALID",
118-
"explicit_paddings", new int[0],
119-
"data_format", "NHWC",
120-
"dilations", dilations);
115+
var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("Conv2D", null, input, filter)
116+
{
117+
attrs = ConvertToDict(new
118+
{
119+
strides,
120+
use_cudnn_on_gpu = true,
121+
padding = "VALID",
122+
explicit_paddings = new int[0],
123+
data_format = "NHWC",
124+
dilations
125+
})
126+
});
121127
};
122128

123129
public Action<int, int> Conv2DWithVariable
@@ -128,16 +134,18 @@ public Action<int, int> Conv2DWithVariable
128134
var strides = new[] { 1, 1, 1, 1 };
129135
var dilations = new[] { 1, 1, 1, 1 };
130136

131-
var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName,
132-
"Conv2D", null,
133-
null,
134-
input, filter,
135-
"strides", strides,
136-
"use_cudnn_on_gpu", true,
137-
"padding", "VALID",
138-
"explicit_paddings", new int[0],
139-
"data_format", "NHWC",
140-
"dilations", dilations);
137+
var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("Conv2D", null, input, filter)
138+
{
139+
attrs = ConvertToDict(new
140+
{
141+
strides,
142+
use_cudnn_on_gpu = true,
143+
padding = "VALID",
144+
explicit_paddings = new int[0],
145+
data_format = "NHWC",
146+
dilations
147+
})
148+
});
141149
};
142150

143151
public Action<int, int> Dataset

src/TensorFlowNET.Console/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static void BasicTest(MemoryMonitor mm)
4747
// explaination of constant
4848
mm.Execute(10, 100 * batchSize, basic.Constant2x3);
4949

50-
mm.Execute(10, 100 * batchSize, basic.ConstantString);
50+
mm.Execute(10, batchSize, basic.ConstantString);
5151

5252
// 100K float variable.
5353
mm.Execute(10, batchSize, basic.Variable);

src/TensorFlowNET.Console/Tensorflow.Console.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<TargetFramework>net5.0</TargetFramework>
66
<RootNamespace>Tensorflow</RootNamespace>
77
<AssemblyName>Tensorflow</AssemblyName>
88
<Platforms>AnyCPU;x64</Platforms>
@@ -11,10 +11,11 @@
1111

1212
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
1313
<DefineConstants>TRACE;DEBUG</DefineConstants>
14+
<PlatformTarget>x64</PlatformTarget>
1415
</PropertyGroup>
1516

1617
<ItemGroup>
17-
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.3.1" />
18+
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.4.1" />
1819
</ItemGroup>
1920

2021
<ItemGroup>

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ You may obtain a copy of the License at
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
******************************************************************************/
16+
using static Tensorflow.Binding;
1617

1718
namespace Tensorflow
1819
{
@@ -37,8 +38,8 @@ public Tensor diag(Tensor diagonal, string name = null)
3738
public Tensor matmul(Tensor a, Tensor b)
3839
=> math_ops.matmul(a, b);
3940

40-
public Tensor batch_matmul(Tensor x, Tensor y)
41-
=> gen_math_ops.batch_mat_mul(x, y);
41+
public Tensor batch_matmul(Tensor x, Tensor y, bool adj_x = false, bool adj_y = false, string name = null)
42+
=> math_ops.batch_matmul(x, y, adj_x: adj_x, adj_y: adj_y, name: name);
4243
}
4344

4445
public Tensor diag(Tensor diagonal, string name = null)
@@ -47,7 +48,32 @@ public Tensor diag(Tensor diagonal, string name = null)
4748
public Tensor matmul(Tensor a, Tensor b)
4849
=> math_ops.matmul(a, b);
4950

50-
public Tensor batch_matmul(Tensor x, Tensor y)
51-
=> gen_math_ops.batch_mat_mul(x, y);
51+
/// <summary>
52+
/// Multiply slices of the two matrices "x" and "y".
53+
/// </summary>
54+
/// <remarks>
55+
/// The `BatchMatMul` operation is embedded into the
56+
/// `MatMul` operation on the DLL side. However the expected
57+
/// attributes are not the same, hence we need to expose this
58+
/// method to have the right args list on the `_apply_op_helper`
59+
/// function.
60+
///
61+
/// For each rank > 2 the first rank - 2 dimensions are considered
62+
/// as fixed, and have to be consistent across the two matrices. A
63+
/// common matrix multiplication is then applied over the residual
64+
/// 2 dimensions.
65+
///
66+
/// e.g.
67+
/// x is (3, 6, 12); y is (3, 12, 6)
68+
/// batch_matmul(x, y) ==> (3, 6, 6)
69+
/// </remarks>
70+
/// <param name="x"></param>
71+
/// <param name="y"></param>
72+
/// <param name="adj_x"></param>
73+
/// <param name="adj_y"></param>
74+
/// <param name="name"></param>
75+
/// <returns></returns>
76+
public Tensor batch_matmul(Tensor x, Tensor y, bool adj_x = false, bool adj_y = false, string name = null)
77+
=> math_ops.batch_matmul(x, y, adj_x: adj_x, adj_y: adj_y, name: name);
5278
}
5379
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,28 @@ public Tensor log(Tensor x, string name = null)
3232
/// <returns></returns>
3333
public Tensor erf(Tensor x, string name = null)
3434
=> math_ops.erf(x, name);
35+
36+
/// <summary>
37+
///
38+
/// </summary>
39+
/// <param name="arr"></param>
40+
/// <param name="weights"></param>
41+
/// <param name="minlength"></param>
42+
/// <param name="maxlength"></param>
43+
/// <param name="dtype"></param>
44+
/// <param name="name"></param>
45+
/// <param name="axis"></param>
46+
/// <param name="binary_output"></param>
47+
/// <returns></returns>
48+
public Tensor bincount(Tensor arr, Tensor weights = null,
49+
Tensor minlength = null,
50+
Tensor maxlength = null,
51+
TF_DataType dtype = TF_DataType.TF_INT32,
52+
string name = null,
53+
TensorShape axis = null,
54+
bool binary_output = false)
55+
=> math_ops.bincount(arr, weights: weights, minlength: minlength, maxlength: maxlength,
56+
dtype: dtype, name: name, axis: axis, binary_output: binary_output);
3557
}
3658

3759
public Tensor abs(Tensor x, string name = null)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17+
using System;
1718
using Tensorflow.Framework;
1819

1920
namespace Tensorflow
2021
{
2122
public partial class tensorflow
2223
{
23-
public SparseTensor<T> SparseTensor<T>(long[,] indices, T[] values, long[] dense_shape)
24-
=> new SparseTensor<T>(indices, values, dense_shape);
24+
public SparseTensor SparseTensor(long[,] indices, Array values, long[] dense_shape)
25+
=> new SparseTensor(indices, values, dense_shape);
2526

26-
public Tensor sparse_tensor_to_dense<T>(SparseTensor<T> sp_input,
27-
T default_value = default,
27+
public Tensor sparse_tensor_to_dense(SparseTensor sp_input,
28+
Array default_value = default,
2829
bool validate_indices = true,
2930
string name = null)
3031
=> gen_sparse_ops.sparse_to_dense(sp_input.indices,

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

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

17+
using Tensorflow.Framework;
18+
1719
namespace Tensorflow
1820
{
1921
public partial class tensorflow
@@ -24,6 +26,30 @@ public class StringsApi
2426
{
2527
string_ops ops = new string_ops();
2628

29+
/// <summary>
30+
/// Converts all uppercase characters into their respective lowercase replacements.
31+
/// </summary>
32+
/// <param name="input"></param>
33+
/// <param name="encoding"></param>
34+
/// <param name="name"></param>
35+
/// <returns></returns>
36+
public Tensor lower(Tensor input, string encoding = "", string name = null)
37+
=> ops.lower(input: input, encoding: encoding, name: name);
38+
39+
/// <summary>
40+
///
41+
/// </summary>
42+
/// <param name="input"></param>
43+
/// <param name="pattern"></param>
44+
/// <param name="rewrite"></param>
45+
/// <param name="replace_global"></param>
46+
/// <param name="name"></param>
47+
/// <returns></returns>
48+
public Tensor regex_replace(Tensor input, string pattern, string rewrite,
49+
bool replace_global = true, string name = null)
50+
=> ops.regex_replace(input, pattern, rewrite,
51+
replace_global: replace_global, name: name);
52+
2753
/// <summary>
2854
/// Return substrings from `Tensor` of strings.
2955
/// </summary>
@@ -40,6 +66,27 @@ public Tensor substr(Tensor input, int pos, int len,
4066
public Tensor substr(string input, int pos, int len,
4167
string name = null, string @uint = "BYTE")
4268
=> ops.substr(input, pos, len, @uint: @uint, name: name);
69+
70+
/// <summary>
71+
/// String lengths of `input`.
72+
/// </summary>
73+
/// <param name="input"></param>
74+
/// <param name="name"></param>
75+
/// <param name="unit"></param>
76+
/// <returns></returns>
77+
public Tensor string_length(Tensor input, string name = null, string unit = "BYTE")
78+
=> ops.string_length(input, name: name, unit: unit);
79+
80+
public RaggedTensor split(Tensor input, string sep = "", int maxsplit = -1, string name = null)
81+
=> ops.string_split_v2(input, sep: sep, maxsplit : maxsplit, name : name);
82+
83+
public (RaggedTensor, RaggedTensor) unicode_decode_with_offsets(Tensor input, string input_encoding,
84+
string errors = "replace", int replacement_char = 0xFFFD,
85+
bool replace_control_characters = false, string name = null)
86+
=> ops.unicode_decode_with_offsets(input, input_encoding, errors,
87+
replacement_char: replacement_char,
88+
replace_control_characters: replace_control_characters,
89+
name: name);
4390
}
4491
}
4592
}

0 commit comments

Comments
 (0)