From 901d5747e1dcca9bebfaf4714fd04c82777e015f Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sat, 30 Jan 2021 04:16:46 -0600 Subject: [PATCH] Expose learning_rate property in Optimizer. --- src/TensorFlowNET.Console/MemoryBasicTest.cs | 22 ++++++++++++++++--- src/TensorFlowNET.Console/Program.cs | 4 ++++ src/TensorFlowNET.Core/APIs/tf.math.cs | 3 +++ .../Operations/gen_math_ops.cs | 12 +++++++++- .../Tensors/Tensor.String.cs | 16 ++++++++------ .../Optimizers/OptimizerV2.cs | 3 +++ 6 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/TensorFlowNET.Console/MemoryBasicTest.cs b/src/TensorFlowNET.Console/MemoryBasicTest.cs index 199f870c..d61cca69 100644 --- a/src/TensorFlowNET.Console/MemoryBasicTest.cs +++ b/src/TensorFlowNET.Console/MemoryBasicTest.cs @@ -56,15 +56,31 @@ namespace Tensorflow { var nd = np.zeros(1 * 256 * 256 * 3).astype(np.float32).reshape(1, 256, 256, 3); ResourceVariable variable = tf.Variable(nd); - var nd2 = np.arange(1 * 256 * 256 * 3).astype(np.float32).reshape(1, 256, 256, 3); - variable.assign(nd2); - for (int i = 0; i< 100; i++) + for (int i = 0; i< 10; i++) { var v = variable.numpy(); } }; + public Action VariableAssign + => (epoch, iterate) => + { + ResourceVariable variable = tf.Variable(3112f); + AssignVariable(variable); + for (int i = 0; i < 100; i++) + { + var v = variable.numpy(); + if ((float)v != 1984f) + throw new ValueError(""); + } + }; + + void AssignVariable(IVariableV1 v) + { + using var tensor = tf.constant(1984f); + v.assign(tensor); + } public Action MathAdd => (epoch, iterate) => diff --git a/src/TensorFlowNET.Console/Program.cs b/src/TensorFlowNET.Console/Program.cs index d65e7e6b..38b878af 100644 --- a/src/TensorFlowNET.Console/Program.cs +++ b/src/TensorFlowNET.Console/Program.cs @@ -52,6 +52,10 @@ namespace Tensorflow // 100K float variable. mm.Execute(10, batchSize, basic.Variable); + mm.Execute(10, batchSize, basic.VariableRead); + + mm.Execute(10, batchSize, basic.VariableAssign); + // 1 million math. mm.Execute(10, 100 * batchSize, basic.MathAdd); diff --git a/src/TensorFlowNET.Core/APIs/tf.math.cs b/src/TensorFlowNET.Core/APIs/tf.math.cs index 2d91be12..e27a5e3c 100644 --- a/src/TensorFlowNET.Core/APIs/tf.math.cs +++ b/src/TensorFlowNET.Core/APIs/tf.math.cs @@ -118,6 +118,9 @@ namespace Tensorflow public Tensor cos(Tensor x, string name = null) => gen_math_ops.cos(x, name); + public Tensor cos(float x, string name = null) + => gen_math_ops.cos(x, name); + /// /// Computes hyperbolic cosine of x element-wise. /// diff --git a/src/TensorFlowNET.Core/Operations/gen_math_ops.cs b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs index 3d64e8b9..bebb24b8 100644 --- a/src/TensorFlowNET.Core/Operations/gen_math_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs @@ -376,8 +376,18 @@ namespace Tensorflow return _op.outputs[0]; } - public static Tensor cos(Tensor x, string name = null) + public static Tensor cos(T x, string name = null) { + if (tf.executing_eagerly()) + { + var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, + "Cos", name, + null, + x); + + return results[0]; + } + var _op = tf.OpDefLib._apply_op_helper("Cos", name, args: new { x }); return _op.outputs[0]; diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.String.cs b/src/TensorFlowNET.Core/Tensors/Tensor.String.cs index a2ad7530..e9d8efdc 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.String.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.String.cs @@ -90,15 +90,17 @@ namespace Tensorflow size *= s; var buffer = new byte[size][]; - var data_start = c_api.TF_TensorData(_handle); - var string_start = data_start + (int)(size * sizeof(ulong)); + var src = c_api.TF_TensorData(_handle); + src += (int)(size * 8); for (int i = 0; i < buffer.Length; i++) { - var len = *(byte*)string_start; - buffer[i] = new byte[len]; - string_start += 1; - Marshal.Copy(string_start, buffer[i], 0, len); - string_start += len; + IntPtr dst = IntPtr.Zero; + ulong dstLen = 0; + var read = c_api.TF_StringDecode((byte*)src, bytesize, (byte**)&dst, ref dstLen, tf.Status.Handle); + tf.Status.Check(true); + buffer[i] = new byte[(int)dstLen]; + Marshal.Copy(dst, buffer[i], 0, buffer[i].Length); + src += (int)read; } return buffer; diff --git a/src/TensorFlowNET.Keras/Optimizers/OptimizerV2.cs b/src/TensorFlowNET.Keras/Optimizers/OptimizerV2.cs index 7ad370ae..2383c5d1 100644 --- a/src/TensorFlowNET.Keras/Optimizers/OptimizerV2.cs +++ b/src/TensorFlowNET.Keras/Optimizers/OptimizerV2.cs @@ -26,6 +26,9 @@ namespace Tensorflow.Keras.Optimizers protected float _initial_decay = 0.0f; protected bool _use_locking = true; + public IVariableV1 lr + => _hyper_variables["learning_rate"]; + Dictionary> _slots; List _slot_names;