Browse Source

Expose learning_rate property in Optimizer.

tags/yolov3
Oceania2018 4 years ago
parent
commit
901d5747e1
6 changed files with 49 additions and 11 deletions
  1. +19
    -3
      src/TensorFlowNET.Console/MemoryBasicTest.cs
  2. +4
    -0
      src/TensorFlowNET.Console/Program.cs
  3. +3
    -0
      src/TensorFlowNET.Core/APIs/tf.math.cs
  4. +11
    -1
      src/TensorFlowNET.Core/Operations/gen_math_ops.cs
  5. +9
    -7
      src/TensorFlowNET.Core/Tensors/Tensor.String.cs
  6. +3
    -0
      src/TensorFlowNET.Keras/Optimizers/OptimizerV2.cs

+ 19
- 3
src/TensorFlowNET.Console/MemoryBasicTest.cs View File

@@ -56,15 +56,31 @@ namespace Tensorflow
{ {
var nd = np.zeros(1 * 256 * 256 * 3).astype(np.float32).reshape(1, 256, 256, 3); var nd = np.zeros(1 * 256 * 256 * 3).astype(np.float32).reshape(1, 256, 256, 3);
ResourceVariable variable = tf.Variable(nd); 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(); var v = variable.numpy();
} }
}; };


public Action<int, int> 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<int, int> MathAdd public Action<int, int> MathAdd
=> (epoch, iterate) => => (epoch, iterate) =>


+ 4
- 0
src/TensorFlowNET.Console/Program.cs View File

@@ -52,6 +52,10 @@ namespace Tensorflow
// 100K float variable. // 100K float variable.
mm.Execute(10, batchSize, basic.Variable); mm.Execute(10, batchSize, basic.Variable);


mm.Execute(10, batchSize, basic.VariableRead);

mm.Execute(10, batchSize, basic.VariableAssign);

// 1 million math. // 1 million math.
mm.Execute(10, 100 * batchSize, basic.MathAdd); mm.Execute(10, 100 * batchSize, basic.MathAdd);




+ 3
- 0
src/TensorFlowNET.Core/APIs/tf.math.cs View File

@@ -118,6 +118,9 @@ namespace Tensorflow
public Tensor cos(Tensor x, string name = null) public Tensor cos(Tensor x, string name = null)
=> gen_math_ops.cos(x, name); => gen_math_ops.cos(x, name);


public Tensor cos(float x, string name = null)
=> gen_math_ops.cos(x, name);

/// <summary> /// <summary>
/// Computes hyperbolic cosine of x element-wise. /// Computes hyperbolic cosine of x element-wise.
/// </summary> /// </summary>


+ 11
- 1
src/TensorFlowNET.Core/Operations/gen_math_ops.cs View File

@@ -376,8 +376,18 @@ namespace Tensorflow
return _op.outputs[0]; return _op.outputs[0];
} }


public static Tensor cos(Tensor x, string name = null)
public static Tensor cos<T>(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 }); var _op = tf.OpDefLib._apply_op_helper("Cos", name, args: new { x });


return _op.outputs[0]; return _op.outputs[0];


+ 9
- 7
src/TensorFlowNET.Core/Tensors/Tensor.String.cs View File

@@ -90,15 +90,17 @@ namespace Tensorflow
size *= s; size *= s;


var buffer = new byte[size][]; 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++) 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; return buffer;


+ 3
- 0
src/TensorFlowNET.Keras/Optimizers/OptimizerV2.cs View File

@@ -26,6 +26,9 @@ namespace Tensorflow.Keras.Optimizers
protected float _initial_decay = 0.0f; protected float _initial_decay = 0.0f;
protected bool _use_locking = true; protected bool _use_locking = true;


public IVariableV1 lr
=> _hyper_variables["learning_rate"];

Dictionary<string, Dictionary<string, IVariableV1>> _slots; Dictionary<string, Dictionary<string, IVariableV1>> _slots;
List<string> _slot_names; List<string> _slot_names;




Loading…
Cancel
Save