Browse Source

eager Tape test

tags/v0.20
Oceania2018 5 years ago
parent
commit
776486527b
13 changed files with 86 additions and 17 deletions
  1. +2
    -2
      README.md
  2. BIN
      docs/assets/tf2.jpg
  3. BIN
      docs/assets/tf2.psd
  4. +6
    -0
      src/TensorFlowNET.Core/Eager/EagerTensor.cs
  5. +22
    -4
      src/TensorFlowNET.Core/Eager/c_api.eager.cs
  6. +15
    -0
      src/TensorFlowNET.Core/Eager/wrap_tfe_src..cs
  7. +1
    -1
      src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_FastPathExecute.cs
  8. +15
    -6
      src/TensorFlowNET.Core/Gradients/GradientActor.cs
  9. +18
    -1
      src/TensorFlowNET.Core/Gradients/Tape.cs
  10. +1
    -1
      src/TensorFlowNET.Core/Tensors/Tensor.cs
  11. +2
    -0
      src/TensorFlowNET.Core/Variables/ResourceVariable.Operators.cs
  12. +3
    -1
      tensorflowlib/README.md
  13. +1
    -1
      test/TensorFlowNET.UnitTest/Tensorflow.UnitTest.csproj

+ 2
- 2
README.md View File

@@ -9,7 +9,7 @@
[![Badge](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu/#/en_US)
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/javiercp/BinderTF.NET/master?urlpath=lab)

*master branch is based on tensorflow 2.1 now, v0.15-tensorflow1.15 is from tensorflow1.15.*
*master branch is based on tensorflow 2.2 now, v0.15-tensorflow1.15 is from tensorflow1.15.*

TF.NET is a member project of [SciSharp STACK](https://github.com/SciSharp).

@@ -28,7 +28,7 @@ In comparison to other projects, like for instance TensorFlowSharp which only pr

### How to use

| TensorFlow | tf 1.13 | tf 1.14 | tf 1.15 | tf 2.0 |
| TensorFlow | tf 1.13 | tf 1.14 | tf 1.15 | tf 2.2 |
| ----------- | ------- | ------- | ------- | ------ |
| tf.net 0.20 | | | x | x |
| tf.net 0.15 | | x | x | |


BIN
docs/assets/tf2.jpg View File

Before After
Width: 1200  |  Height: 600  |  Size: 92 kB

BIN
docs/assets/tf2.psd View File


+ 6
- 0
src/TensorFlowNET.Core/Eager/EagerTensor.cs View File

@@ -13,31 +13,37 @@ namespace Tensorflow.Eager
{
tfe_tensor_handle = handle;
_handle = c_api.TFE_TensorHandleResolve(handle, status);
_id = ops.uid();
}

public EagerTensor(string value, string device_name) : base(value)
{
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status);
_id = ops.uid();
}

public EagerTensor(int value, string device_name) : base(value)
{
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status);
_id = ops.uid();
}

public EagerTensor(float[] value, string device_name) : base(value)
{
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status);
_id = ops.uid();
}

public EagerTensor(double[] value, string device_name) : base(value)
{
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status);
_id = ops.uid();
}

public EagerTensor(NDArray value, string device_name) : base(value)
{
tfe_tensor_handle = c_api.TFE_NewTensorHandle(_handle, status);
_id = ops.uid();
}

public override string ToString()


+ 22
- 4
src/TensorFlowNET.Core/Eager/c_api.eager.cs View File

@@ -102,14 +102,20 @@ namespace Tensorflow
public static extern TFE_Op TFE_NewOp(IntPtr ctx, string op_or_function_name, IntPtr status);

/// <summary>
///
/// Resets `op_to_reset` with `op_or_function_name` and `raw_device_name`. This
/// is for performance optimization by reusing an exiting unused op rather than
/// creating a new op every time. If `raw_device_name` is `NULL` or empty, it
/// does not set the device name. If it's not `NULL`, then it attempts to parse
/// and set the device name. It's effectively `TFE_OpSetDevice`, but it is faster
/// than separately calling it because if the existing op has the same
/// `raw_device_name`, it skips parsing and just leave as it is.
/// </summary>
/// <param name="ctx">TFE_Context*</param>
/// <param name="op_to_reset">TFE_Op*</param>
/// <param name="op_or_function_name">const char*</param>
/// <param name="raw_device_name">const char*</param>
/// <param name="status">TF_Status*</param>
/// <param name="op_to_reset">TFE_Op*</param>
[DllImport(TensorFlowLibName)]
public static extern void TFE_OpReset(IntPtr ctx, string op_or_function_name, IntPtr status, IntPtr op_to_reset);
public static extern void TFE_OpReset(IntPtr op_to_reset, string op_or_function_name, string raw_device_name, IntPtr status);

/// <summary>
///
@@ -304,5 +310,17 @@ namespace Tensorflow
/// <returns>TFE_Executor*</returns>
[DllImport(TensorFlowLibName)]
public static extern TFE_Executor TFE_ContextGetExecutorForThread(IntPtr ctx);

[DllImport(TensorFlowLibName)]
public static extern void TFE_Test();

[DllImport(TensorFlowLibName)]
public static extern IntPtr TFE_TapeSetNew(bool persistent, bool watch_accessed_variables);

[DllImport(TensorFlowLibName)]
public static extern void TFE_TapeWatch(IntPtr tape, IntPtr tensor, int tensor_id);

[DllImport(TensorFlowLibName)]
public static extern void TFE_TapeGradient(IntPtr tape, long[] targetTensorIds, IntPtr[] target, long[] sourcesTensorIds, IntPtr status);
}
}

+ 15
- 0
src/TensorFlowNET.Core/Eager/wrap_tfe_src..cs View File

@@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.Linq;
using System;
using static Tensorflow.OpDef.Types;

namespace Tensorflow.Eager
{
/// <summary>
/// python\eager\pywrap_tfe_src.cc
/// </summary>
public partial class wrap_tfe_src
{

}
}

+ 1
- 1
src/TensorFlowNET.Core/Eager/wrap_tfe_src.TFE_FastPathExecute.cs View File

@@ -110,7 +110,7 @@ namespace Tensorflow.Eager
var maybe_op = ReleaseThreadLocalOp();
if (maybe_op != IntPtr.Zero)
{
c_api.TFE_OpReset(ctx, op_or_function_name, status, maybe_op);
c_api.TFE_OpReset(maybe_op, op_or_function_name, ctx.device_name, status);
}
else
{


+ 15
- 6
src/TensorFlowNET.Core/Gradients/GradientActor.cs View File

@@ -23,7 +23,6 @@ namespace Tensorflow.Gradients
bool _watch_accessed_variables;
bool _created_eagerly;
Tape _tape;
int tape_nesting_id_counter = 0;

public GradientActor(bool persistent = false,
bool watch_accessed_variables = true)
@@ -41,18 +40,28 @@ namespace Tensorflow.Gradients
"re-enter an already-active tape.");

if (_tape == null)
{
_tape = new Tape();
_tape.tape = new GradientTape(_persistent, _watch_accessed_variables);
_tape.nesting_id = tape_nesting_id_counter++;
}
_tape = new Tape(_persistent, _watch_accessed_variables);
else
throw new NotImplementedException("");

_recording = true;
}

/// <summary>
/// Marks this tensor to be watched by the given tape.
/// </summary>
/// <param name="x"></param>
public void watch(Tensor x)
{
_tape.watch(x);
}

public Tensor gradient(Tensor target, Tensor sources)
{
c_api.TFE_Test();
//using (var status = new Status())
//c_api.TFE_TapeGradient(_tape, new long[] { target.Id }, status);
return null;
}

public void Dispose()


+ 18
- 1
src/TensorFlowNET.Core/Gradients/Tape.cs View File

@@ -4,11 +4,21 @@ using System.Text;

namespace Tensorflow.Gradients
{
public class Tape
public class Tape : DisposableObject
{
public GradientTape tape { get; set; }
public int nesting_id { get; set; }

public Tape(bool persistent, bool watch_accessed_variables)
{
_handle = c_api.TFE_TapeSetNew(persistent, watch_accessed_variables);
}

public void watch(Tensor x)
{
c_api.TFE_TapeWatch(_handle, x, x.Id);
}

public static bool IsDtypeTrainable(DataType dtype)
{
switch (dtype)
@@ -26,5 +36,12 @@ namespace Tensorflow.Gradients
return false;
}
}

protected override void DisposeUnmanagedResources(IntPtr handle)
{
}

public static implicit operator IntPtr(Tape tape)
=> tape._handle;
}
}

+ 1
- 1
src/TensorFlowNET.Core/Tensors/Tensor.cs View File

@@ -39,7 +39,7 @@ namespace Tensorflow
IPackable<Tensor>,
ICanBeFlattened
{
private readonly int _id;
protected int _id;
private readonly Operation _op;
private readonly int _value_index;
private TF_Output? _tf_output;


+ 2
- 0
src/TensorFlowNET.Core/Variables/ResourceVariable.Operators.cs View File

@@ -30,6 +30,8 @@ namespace Tensorflow
public static Tensor operator -(ResourceVariable x, double y) => op_helper("sub", x, y);
public static Tensor operator -(ResourceVariable x, Tensor y) => op_helper("sub", x, y);

public static Tensor operator *(ResourceVariable x, ResourceVariable y) => gen_math_ops.mul(x, y);

public static Tensor operator <(ResourceVariable x, Tensor y) => gen_math_ops.less(x.value(), y);

public static Tensor operator >(ResourceVariable x, Tensor y) => gen_math_ops.greater(x.value(), y);


+ 3
- 1
tensorflowlib/README.md View File

@@ -44,7 +44,9 @@ We can't found official prebuild binaries for each platform since tensorflow 2.0

https://www.tensorflow.org/install/source_windows

Download [Bazel 0.29.1](https://github.com/bazelbuild/bazel/releases/tag/0.29.1) to build tensorflow2.x. We build customized binary to export c_api from this [fork](https://github.com/SciSharp/tensorflow).
Download [Bazel 2.0.0](https://github.com/bazelbuild/bazel/releases/tag/2.0.0) to build tensorflow2.x. We build customized binary to export c_api from this [fork](https://github.com/SciSharp/tensorflow).

Set ENV `BAZEL_VC=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC`.

`pacman -S git patch unzip`



+ 1
- 1
test/TensorFlowNET.UnitTest/Tensorflow.UnitTest.csproj View File

@@ -29,7 +29,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.2" />
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />


Loading…
Cancel
Save