Browse Source

Aadded ContextOptions

tags/v0.1.0-Tensor
haiping008 6 years ago
parent
commit
9665bf5262
8 changed files with 109 additions and 8 deletions
  1. +1
    -1
      src/TensorFlowNET.Core/Eager/Context.cs
  2. +26
    -0
      src/TensorFlowNET.Core/Eager/ContextOptions.cs
  3. +11
    -6
      src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs
  4. +2
    -0
      src/TensorFlowNET.Core/Variables/RefVariable.cs
  5. +5
    -1
      tensorflowlib/README.md
  6. +2
    -0
      test/TensorFlowNET.Examples/LinearRegression.cs
  7. +34
    -0
      test/TensorFlowNET.UnitTest/ConsumersTest.cs
  8. +28
    -0
      test/TensorFlowNET.UnitTest/Eager/CApiVariableTest.cs

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

@@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;

namespace Tensorflow
namespace Tensorflow.Eager
{
public class Context
{


+ 26
- 0
src/TensorFlowNET.Core/Eager/ContextOptions.cs View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Tensorflow.Eager
{
public class ContextOptions : IDisposable
{
private IntPtr _handle;

public ContextOptions()
{
_handle = c_api.TFE_NewContextOptions();
}

public void Dispose()
{
c_api.TFE_DeleteContextOptions(_handle);
}

public static implicit operator IntPtr(ContextOptions ctx)
{
return ctx._handle;
}
}
}

+ 11
- 6
src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs View File

@@ -102,16 +102,21 @@ namespace Tensorflow
/// <param name="func_graphs"></param>
private static void _MarkReachedOps(List<Operation> from_ops, List<Operation> reached_ops, List<object> func_graphs)
{
foreach(var op in from_ops)
Queue<Operation> queue = new Queue<Operation>(from_ops);
while (queue.Count > 0)
{
reached_ops.Add(op);
foreach(var output in op.outputs)
var op = queue.Dequeue();

if (!reached_ops.Contains(op))
{
reached_ops.AddRange(_Consumers(output, func_graphs));
reached_ops.Add(op);
foreach (var output in op.outputs)
{
var c = _Consumers(output, func_graphs).ToList();
c.ForEach(x => queue.Enqueue(x));
}
}
}

reached_ops.Reverse();
}

/// <summary>


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

@@ -12,6 +12,7 @@ namespace Tensorflow
public bool _trainable;
public Tensor _variable;
public Tensor _snapshot;
public Operation op;

public RefVariable(object initial_value,
bool trainable = true,
@@ -91,6 +92,7 @@ namespace Tensorflow
_snapshot = gen_array_ops.identity(_variable, name = "read");
}

op = _initializer_op;
ops.add_to_collections(collections, this);
}
}


+ 5
- 1
tensorflowlib/README.md View File

@@ -4,4 +4,8 @@ Here are some pre-built TensorFlow binaries you can use for each platform:
- CPU-only: https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-1.12.0.tar.gz
- GPU-enabled: https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-linux-x86_64-1.12.0.tar.gz
- Mac: https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-darwin-x86_64-1.12.0.tar.gz
- Windows: https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-1.12.0.zip
- Windows: https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-1.12.0.zip

https://www.tensorflow.org/install/source_windows
pacman -S git patch unzip
bazel build --config=opt //tensorflow:libtensorflow.so

+ 2
- 0
test/TensorFlowNET.Examples/LinearRegression.cs View File

@@ -50,6 +50,8 @@ namespace TensorFlowNET.Examples
// Note, minimize() knows to modify W and b because Variable objects are trainable=True by default
var optimizer = tf.train.GradientDescentOptimizer(learning_rate);
optimizer.minimize(cost);


}
}
}

+ 34
- 0
test/TensorFlowNET.UnitTest/ConsumersTest.cs View File

@@ -0,0 +1,34 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;
using Tensorflow;

namespace TensorFlowNET.UnitTest
{
[TestClass]
public class ConsumersTest : CApiTest
{
[TestMethod]
public void Constant()
{
var X = tf.placeholder(tf.float64);
var W = tf.constant(1.0D);

var mul = tf.multiply(X, W);
EXPECT_EQ(1, X.op.OutputNumConsumers(0));
EXPECT_EQ(1, W.op.OutputNumConsumers(0));
}

[TestMethod]
public void Variable()
{
var X = tf.placeholder(tf.float64);
var W = tf.Variable(1.0D, name: "var");

var mul = tf.multiply(X, W);
EXPECT_EQ(1, X.op.OutputNumConsumers(0));
EXPECT_EQ(1, W.op.OutputNumConsumers(0));
}
}
}

+ 28
- 0
test/TensorFlowNET.UnitTest/Eager/CApiVariableTest.cs View File

@@ -0,0 +1,28 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;
using Tensorflow;

namespace TensorFlowNET.UnitTest.Eager
{
/// <summary>
/// tensorflow\c\eager\c_api_test.cc
/// </summary>
[TestClass]
public class CApiVariableTest : CApiTest, IDisposable
{
Status status = new Status();

[TestMethod]
public void Variables()
{

}

public void Dispose()
{
}
}
}

Loading…
Cancel
Save