Skip to content

Commit 4c1878b

Browse files
committed
Merge branch 'master' into support_function_load
2 parents 4252952 + ef687ae commit 4c1878b

20 files changed

Lines changed: 668 additions & 113 deletions

File tree

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*****************************************************************************
2-
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
2+
Copyright 2023 The TensorFlow.NET Authors. All Rights Reserved.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -57,7 +57,7 @@ public Tensor softplus(Tensor features, string name = null)
5757

5858
public Tensor tanh(Tensor x, string name = null)
5959
=> math_ops.tanh(x, name: name);
60-
60+
6161
/// <summary>
6262
/// Finds values and indices of the `k` largest entries for the last dimension.
6363
/// </summary>
@@ -93,6 +93,16 @@ public Tensor bincount(Tensor arr, Tensor weights = null,
9393
bool binary_output = false)
9494
=> math_ops.bincount(arr, weights: weights, minlength: minlength, maxlength: maxlength,
9595
dtype: dtype, name: name, axis: axis, binary_output: binary_output);
96+
97+
public Tensor real(Tensor x, string name = null)
98+
=> gen_ops.real(x, x.dtype.real_dtype(), name);
99+
public Tensor imag(Tensor x, string name = null)
100+
=> gen_ops.imag(x, x.dtype.real_dtype(), name);
101+
102+
public Tensor conj(Tensor x, string name = null)
103+
=> gen_ops.conj(x, name);
104+
public Tensor angle(Tensor x, string name = null)
105+
=> gen_ops.angle(x, x.dtype.real_dtype(), name);
96106
}
97107

98108
public Tensor abs(Tensor x, string name = null)
@@ -537,7 +547,7 @@ public Tensor reduce_prod(Tensor input_tensor, Axis? axis = null, bool keepdims
537547
public Tensor reduce_sum(Tensor input, Axis? axis = null, Axis? reduction_indices = null,
538548
bool keepdims = false, string name = null)
539549
{
540-
if(keepdims)
550+
if (keepdims)
541551
return math_ops.reduce_sum(input, axis: constant_op.constant(axis ?? reduction_indices), keepdims: keepdims, name: name);
542552
else
543553
return math_ops.reduce_sum(input, axis: constant_op.constant(axis ?? reduction_indices));
@@ -585,5 +595,7 @@ public Tensor square(Tensor x, string name = null)
585595
=> gen_math_ops.square(x, name: name);
586596
public Tensor squared_difference(Tensor x, Tensor y, string name = null)
587597
=> gen_math_ops.squared_difference(x: x, y: y, name: name);
598+
public Tensor complex(Tensor real, Tensor imag, Tensorflow.TF_DataType? dtype = null,
599+
string name = null) => gen_ops.complex(real, imag, dtype, name);
588600
}
589601
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*****************************************************************************
2+
Copyright 2023 Konstantin Balashov All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
using Tensorflow.Operations;
18+
19+
namespace Tensorflow
20+
{
21+
public partial class tensorflow
22+
{
23+
public SignalApi signal { get; } = new SignalApi();
24+
public class SignalApi
25+
{
26+
public Tensor fft(Tensor input, string name = null)
27+
=> gen_ops.f_f_t(input, name: name);
28+
public Tensor ifft(Tensor input, string name = null)
29+
=> gen_ops.i_f_f_t(input, name: name);
30+
public Tensor fft2d(Tensor input, string name = null)
31+
=> gen_ops.f_f_t2d(input, name: name);
32+
public Tensor ifft2d(Tensor input, string name = null)
33+
=> gen_ops.i_f_f_t2d(input, name: name);
34+
public Tensor fft3d(Tensor input, string name = null)
35+
=> gen_ops.f_f_t3d(input, name: name);
36+
public Tensor ifft3d(Tensor input, string name = null)
37+
=> gen_ops.i_f_f_t3d(input, name: name);
38+
}
39+
}
40+
}

‎src/TensorFlowNET.Core/Gradients/math_grad.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ public static Tensor[] _PowGrad(Operation op, Tensor[] grads)
840840
/// <param name="x"></param>
841841
/// <param name="y"></param>
842842
/// <returns></returns>
843-
private static (Tensor, Tensor, bool)[] SmartBroadcastGradientArgs(Tensor x, Tensor y, Tensor grad)
843+
public static (Tensor, Tensor, bool)[] SmartBroadcastGradientArgs(Tensor x, Tensor y, Tensor grad)
844844
{
845845
Tensor sx, sy;
846846
if (x.shape.IsFullyDefined &&

‎src/TensorFlowNET.Core/Gradients/nn_grad.cs‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
******************************************************************************/
1616

1717
using System;
18+
using System.Diagnostics;
1819
using System.Linq;
1920
using Tensorflow.Operations;
2021
using static Tensorflow.Binding;
@@ -135,13 +136,35 @@ public static Tensor[] _SquaredDifferenceGrad(Operation op, Tensor[] grads)
135136
{
136137
Tensor x = op.inputs[0];
137138
Tensor y = op.inputs[1];
139+
var grad = grads[0];
138140
var scale = ops.convert_to_tensor(2.0f, dtype: x.dtype);
139-
var x_grad = math_ops.scalar_mul(scale, grads[0]) * (x - y);
140-
return new Tensor[]
141+
var x_grad = math_ops.scalar_mul(scale, grad) * (x - y);
142+
if (math_grad._ShapesFullySpecifiedAndEqual(x, y, grad))
141143
{
142-
x_grad,
143-
-x_grad
144-
};
144+
return new Tensor[] { x_grad, -x_grad };
145+
}
146+
var broadcast_info = math_grad.SmartBroadcastGradientArgs(x, y, grad);
147+
Debug.Assert(broadcast_info.Length == 2);
148+
var (sx, rx, must_reduce_x) = broadcast_info[0];
149+
var (sy, ry, must_reduce_y) = broadcast_info[1];
150+
Tensor gx, gy;
151+
if (must_reduce_x)
152+
{
153+
gx = array_ops.reshape(math_ops.reduce_sum(x_grad, rx), sx);
154+
}
155+
else
156+
{
157+
gx = x_grad;
158+
}
159+
if (must_reduce_y)
160+
{
161+
gy = -array_ops.reshape(math_ops.reduce_sum(x_grad, ry), sy);
162+
}
163+
else
164+
{
165+
gy = -x_grad;
166+
}
167+
return new Tensor[] { gx, gy };
145168
}
146169

147170
/// <summary>

‎src/TensorFlowNET.Core/Keras/Engine/ICallback.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ public interface ICallback
1515
void on_predict_end();
1616
void on_test_begin();
1717
void on_test_batch_begin(long step);
18-
void on_test_batch_end(long end_step, IEnumerable<(string, Tensor)> logs);
18+
void on_test_batch_end(long end_step, Dictionary<string, float> logs);
1919
}

‎src/TensorFlowNET.Core/Keras/Engine/IModel.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ICallback fit(NDArray x, NDArray y,
2222
int verbose = 1,
2323
List<ICallback> callbacks = null,
2424
float validation_split = 0f,
25+
(NDArray val_x, NDArray val_y)? validation_data = null,
2526
bool shuffle = true,
2627
int initial_epoch = 0,
2728
int max_queue_size = 10,
@@ -34,6 +35,7 @@ ICallback fit(IEnumerable<NDArray> x, NDArray y,
3435
int verbose = 1,
3536
List<ICallback> callbacks = null,
3637
float validation_split = 0f,
38+
(IEnumerable<NDArray> val_x, NDArray val_y)? validation_data = null,
3739
bool shuffle = true,
3840
int initial_epoch = 0,
3941
int max_queue_size = 10,
@@ -65,7 +67,8 @@ Dictionary<string, float> evaluate(NDArray x, NDArray y,
6567
int max_queue_size = 10,
6668
int workers = 1,
6769
bool use_multiprocessing = false,
68-
bool return_dict = false);
70+
bool return_dict = false,
71+
bool is_val = false);
6972

7073
Tensors predict(Tensors x,
7174
int batch_size = -1,
@@ -79,5 +82,5 @@ Tensors predict(Tensors x,
7982

8083
IKerasConfig get_config();
8184

82-
void set_stopTraining_true();
85+
bool Stop_training { get;set; }
8386
}

‎src/TensorFlowNET.Core/Operations/gen_ops.cs‎

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -730,12 +730,7 @@ public static (Tensor sampled_candidates, Tensor true_expected_count, Tensor sam
730730
/// </remarks>
731731
public static Tensor angle(Tensor input, TF_DataType? Tout = null, string name = "Angle")
732732
{
733-
var dict = new Dictionary<string, object>();
734-
dict["input"] = input;
735-
if (Tout.HasValue)
736-
dict["Tout"] = Tout.Value;
737-
var op = tf.OpDefLib._apply_op_helper("Angle", name: name, keywords: dict);
738-
return op.output;
733+
return tf.Context.ExecuteOp("Angle", name, new ExecuteOpArgs(input).SetAttributes(new { Tout = Tout }));
739734
}
740735

741736
/// <summary>
@@ -4976,15 +4971,14 @@ public static Tensor compare_and_bitpack(Tensor input, Tensor threshold, string
49764971
/// tf.complex(real, imag) ==&amp;gt; [[2.25 + 4.75j], [3.25 + 5.75j]]
49774972
/// </code>
49784973
/// </remarks>
4979-
public static Tensor complex(Tensor real, Tensor imag, TF_DataType? Tout = null, string name = "Complex")
4974+
public static Tensor complex(Tensor real, Tensor imag, TF_DataType? a_Tout = null, string name = "Complex")
49804975
{
4981-
var dict = new Dictionary<string, object>();
4982-
dict["real"] = real;
4983-
dict["imag"] = imag;
4984-
if (Tout.HasValue)
4985-
dict["Tout"] = Tout.Value;
4986-
var op = tf.OpDefLib._apply_op_helper("Complex", name: name, keywords: dict);
4987-
return op.output;
4976+
TF_DataType Tin = real.GetDataType();
4977+
if (a_Tout is null)
4978+
{
4979+
a_Tout = (Tin == TF_DataType.TF_DOUBLE)? TF_DataType.TF_COMPLEX128: TF_DataType.TF_COMPLEX64;
4980+
}
4981+
return tf.Context.ExecuteOp("Complex", name, new ExecuteOpArgs(real, imag).SetAttributes(new { T=Tin, Tout=a_Tout }));
49884982
}
49894983

49904984
/// <summary>
@@ -5008,12 +5002,7 @@ public static Tensor complex(Tensor real, Tensor imag, TF_DataType? Tout = null,
50085002
/// </remarks>
50095003
public static Tensor complex_abs(Tensor x, TF_DataType? Tout = null, string name = "ComplexAbs")
50105004
{
5011-
var dict = new Dictionary<string, object>();
5012-
dict["x"] = x;
5013-
if (Tout.HasValue)
5014-
dict["Tout"] = Tout.Value;
5015-
var op = tf.OpDefLib._apply_op_helper("ComplexAbs", name: name, keywords: dict);
5016-
return op.output;
5005+
return tf.Context.ExecuteOp("ComplexAbs", name, new ExecuteOpArgs(x).SetAttributes(new { Tout = Tout }));
50175006
}
50185007

50195008
/// <summary>
@@ -5313,10 +5302,7 @@ public static Tensor configure_distributed_t_p_u(string embedding_config = null,
53135302
/// </remarks>
53145303
public static Tensor conj(Tensor input, string name = "Conj")
53155304
{
5316-
var dict = new Dictionary<string, object>();
5317-
dict["input"] = input;
5318-
var op = tf.OpDefLib._apply_op_helper("Conj", name: name, keywords: dict);
5319-
return op.output;
5305+
return tf.Context.ExecuteOp("Conj", name, new ExecuteOpArgs(new object[] { input }));
53205306
}
53215307

53225308
/// <summary>
@@ -10489,10 +10475,7 @@ public static Tensor extract_jpeg_shape(Tensor contents, TF_DataType? output_typ
1048910475
/// </remarks>
1049010476
public static Tensor f_f_t(Tensor input, string name = "FFT")
1049110477
{
10492-
var dict = new Dictionary<string, object>();
10493-
dict["input"] = input;
10494-
var op = tf.OpDefLib._apply_op_helper("FFT", name: name, keywords: dict);
10495-
return op.output;
10478+
return tf.Context.ExecuteOp("FFT", name, new ExecuteOpArgs(input));
1049610479
}
1049710480

1049810481
/// <summary>
@@ -10519,10 +10502,7 @@ public static Tensor f_f_t(Tensor input, string name = "FFT")
1051910502
/// </remarks>
1052010503
public static Tensor f_f_t2d(Tensor input, string name = "FFT2D")
1052110504
{
10522-
var dict = new Dictionary<string, object>();
10523-
dict["input"] = input;
10524-
var op = tf.OpDefLib._apply_op_helper("FFT2D", name: name, keywords: dict);
10525-
return op.output;
10505+
return tf.Context.ExecuteOp("FFT2D", name, new ExecuteOpArgs(input));
1052610506
}
1052710507

1052810508
/// <summary>
@@ -10549,10 +10529,7 @@ public static Tensor f_f_t2d(Tensor input, string name = "FFT2D")
1054910529
/// </remarks>
1055010530
public static Tensor f_f_t3d(Tensor input, string name = "FFT3D")
1055110531
{
10552-
var dict = new Dictionary<string, object>();
10553-
dict["input"] = input;
10554-
var op = tf.OpDefLib._apply_op_helper("FFT3D", name: name, keywords: dict);
10555-
return op.output;
10532+
return tf.Context.ExecuteOp("FFT3D", name, new ExecuteOpArgs(input));
1055610533
}
1055710534

1055810535
/// <summary>
@@ -12875,10 +12852,7 @@ public static Tensor host_const(Tensor value, TF_DataType dtype, string name = "
1287512852
/// </remarks>
1287612853
public static Tensor i_f_f_t(Tensor input, string name = "IFFT")
1287712854
{
12878-
var dict = new Dictionary<string, object>();
12879-
dict["input"] = input;
12880-
var op = tf.OpDefLib._apply_op_helper("IFFT", name: name, keywords: dict);
12881-
return op.output;
12855+
return tf.Context.ExecuteOp("IFFT", name, new ExecuteOpArgs(input));
1288212856
}
1288312857

1288412858
/// <summary>
@@ -12905,10 +12879,7 @@ public static Tensor i_f_f_t(Tensor input, string name = "IFFT")
1290512879
/// </remarks>
1290612880
public static Tensor i_f_f_t2d(Tensor input, string name = "IFFT2D")
1290712881
{
12908-
var dict = new Dictionary<string, object>();
12909-
dict["input"] = input;
12910-
var op = tf.OpDefLib._apply_op_helper("IFFT2D", name: name, keywords: dict);
12911-
return op.output;
12882+
return tf.Context.ExecuteOp("IFFT2D", name, new ExecuteOpArgs(input));
1291212883
}
1291312884

1291412885
/// <summary>
@@ -12935,10 +12906,7 @@ public static Tensor i_f_f_t2d(Tensor input, string name = "IFFT2D")
1293512906
/// </remarks>
1293612907
public static Tensor i_f_f_t3d(Tensor input, string name = "IFFT3D")
1293712908
{
12938-
var dict = new Dictionary<string, object>();
12939-
dict["input"] = input;
12940-
var op = tf.OpDefLib._apply_op_helper("IFFT3D", name: name, keywords: dict);
12941-
return op.output;
12909+
return tf.Context.ExecuteOp("IFFT3D", name, new ExecuteOpArgs(input));
1294212910
}
1294312911

1294412912
/// <summary>
@@ -13325,14 +13293,12 @@ public static Tensor igammac(Tensor a, Tensor x, string name = "Igammac")
1332513293
/// tf.imag(input) ==&amp;gt; [4.75, 5.75]
1332613294
/// </code>
1332713295
/// </remarks>
13328-
public static Tensor imag(Tensor input, TF_DataType? Tout = null, string name = "Imag")
13296+
public static Tensor imag(Tensor input, TF_DataType? a_Tout = null, string name = "Imag")
1332913297
{
13330-
var dict = new Dictionary<string, object>();
13331-
dict["input"] = input;
13332-
if (Tout.HasValue)
13333-
dict["Tout"] = Tout.Value;
13334-
var op = tf.OpDefLib._apply_op_helper("Imag", name: name, keywords: dict);
13335-
return op.output;
13298+
TF_DataType Tin = input.GetDataType();
13299+
return tf.Context.ExecuteOp("Imag", name, new ExecuteOpArgs(input).SetAttributes(new { T = Tin, Tout = a_Tout }));
13300+
13301+
// return tf.Context.ExecuteOp("Imag", name, new ExecuteOpArgs(new object[] { input }));
1333613302
}
1333713303

1333813304
/// <summary>
@@ -23863,14 +23829,12 @@ public static Tensor reader_serialize_state_v2(Tensor reader_handle, string name
2386323829
/// tf.real(input) ==&amp;gt; [-2.25, 3.25]
2386423830
/// </code>
2386523831
/// </remarks>
23866-
public static Tensor real(Tensor input, TF_DataType? Tout = null, string name = "Real")
23832+
public static Tensor real(Tensor input, TF_DataType? a_Tout = null, string name = "Real")
2386723833
{
23868-
var dict = new Dictionary<string, object>();
23869-
dict["input"] = input;
23870-
if (Tout.HasValue)
23871-
dict["Tout"] = Tout.Value;
23872-
var op = tf.OpDefLib._apply_op_helper("Real", name: name, keywords: dict);
23873-
return op.output;
23834+
TF_DataType Tin = input.GetDataType();
23835+
return tf.Context.ExecuteOp("Real", name, new ExecuteOpArgs(input).SetAttributes(new { T = Tin, Tout = a_Tout }));
23836+
23837+
// return tf.Context.ExecuteOp("Real", name, new ExecuteOpArgs(new object[] {input}));
2387423838
}
2387523839

2387623840
/// <summary>

‎src/TensorFlowNET.Core/Operations/math_ops.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License.
2020
using System.Linq;
2121
using Tensorflow.Framework;
2222
using static Tensorflow.Binding;
23+
using Tensorflow.Operations;
2324

2425
namespace Tensorflow
2526
{
@@ -35,8 +36,9 @@ public static Tensor abs(Tensor x, string name = null)
3536
name = scope;
3637
x = ops.convert_to_tensor(x, name: "x");
3738
if (x.dtype.is_complex())
38-
throw new NotImplementedException("math_ops.abs for dtype.is_complex");
39-
//return gen_math_ops.complex_abs(x, Tout: x.dtype.real_dtype, name: name);
39+
{
40+
return gen_ops.complex_abs(x, Tout: x.dtype.real_dtype(), name: name);
41+
}
4042
return gen_math_ops._abs(x, name: name);
4143
});
4244
}

‎src/TensorFlowNET.Keras/Callbacks/CallbackList.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void on_test_batch_begin(long step)
6969
{
7070
callbacks.ForEach(x => x.on_test_batch_begin(step));
7171
}
72-
public void on_test_batch_end(long end_step, IEnumerable<(string, Tensor)> logs)
72+
public void on_test_batch_end(long end_step, Dictionary<string, float> logs)
7373
{
7474
callbacks.ForEach(x => x.on_test_batch_end(end_step, logs));
7575
}

0 commit comments

Comments
 (0)