Browse Source

Fix BasicOperations error. #144

tags/v0.8.0
Oceania2018 6 years ago
parent
commit
fbc836e24a
11 changed files with 141 additions and 15 deletions
  1. +5
    -1
      README.md
  2. BIN
      docs/TIM.png
  3. +6
    -0
      src/TensorFlowNET.Core/Sessions/BaseSession.cs
  4. +5
    -0
      src/TensorFlowNET.Core/Sessions/Session.cs
  5. +11
    -0
      src/TensorFlowNET.Core/Tensors/Tensor.cs
  6. +37
    -0
      src/TensorFlowNET.Core/ops._DefaultStack.cs
  7. +33
    -0
      src/TensorFlowNET.Core/ops.py.cs
  8. +1
    -1
      test/TensorFlowNET.Examples/BasicOperations.cs
  9. +31
    -10
      test/TensorFlowNET.UnitTest/ConstantTest.cs
  10. +1
    -1
      test/TensorFlowNET.UnitTest/ConsumersTest.cs
  11. +11
    -2
      test/TensorFlowNET.UnitTest/VariableTest.cs

+ 5
- 1
README.md View File

@@ -57,4 +57,8 @@ using(var sess = tf.Session())

Read the docs & book [The Definitive Guide to Tensorflow.NET](https://tensorflownet.readthedocs.io/en/latest/FrontCover.html).

Star me or raise issue on [Github](https://github.com/SciSharp/TensorFlow.NET) feel free.
Star me or raise issue on [Github](https://github.com/SciSharp/TensorFlow.NET) feel free.

Scan QR code to join TIM group:

![SciSharp STACK](C:\Users\haipi\Documents\Projects\TensorFlow.NET\docs\TIM.png)

BIN
docs/TIM.png View File

Before After
Width: 226  |  Height: 290  |  Size: 15 kB

+ 6
- 0
src/TensorFlowNET.Core/Sessions/BaseSession.cs View File

@@ -146,6 +146,12 @@ namespace Tensorflow
var str = UTF8Encoding.Default.GetString(bytes, 9, bytes.Length - 9);
nd = np.array(str).reshape();
break;
case TF_DataType.TF_INT16:
var shorts = new short[tensor.size];
for (ulong i = 0; i < tensor.size; i++)
shorts[i] = *(short*)(c_api.TF_TensorData(output) + (int)(tensor.dataTypeSize * i));
nd = np.array(shorts).reshape(ndims);
break;
case TF_DataType.TF_INT32:
var ints = new int[tensor.size];
for (ulong i = 0; i < tensor.size; i++)


+ 5
- 0
src/TensorFlowNET.Core/Sessions/Session.cs View File

@@ -35,6 +35,11 @@ namespace Tensorflow
public static implicit operator IntPtr(Session session) => session._handle;
public static implicit operator Session(IntPtr handle) => new Session(handle);

public void close()
{
Dispose();
}

public void Dispose()
{
Options.Dispose();


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

@@ -207,6 +207,17 @@ namespace Tensorflow
return tensor;
}

/// <summary>
/// Evaluates this tensor in a `Session`.
/// </summary>
/// <param name="feed_dict">A dictionary that maps `Tensor` objects to feed values.</param>
/// <param name="session">The `Session` to be used to evaluate this tensor.</param>
/// <returns></returns>
public NDArray eval(dynamic feed_dict = null, Session session = null)
{
return ops._eval_using_default_session(new Tensor[] { this }, feed_dict, Graph, session)[0];
}

public TF_DataType ToTFDataType(Type type)
{
switch (type.Name)


+ 37
- 0
src/TensorFlowNET.Core/ops._DefaultStack.cs View File

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

namespace Tensorflow
{
public partial class ops
{
_DefaultStack _default_session_stack = new _DefaultStack();

public class _DefaultStack : IPython
{
Stack<object> stack;
bool _enforce_nesting = true;

public _DefaultStack()
{
stack = new Stack<object>();
}

public void __enter__()
{
throw new NotImplementedException();
}

public void __exit__()
{
throw new NotImplementedException();
}

public void Dispose()
{
throw new NotImplementedException();
}
}
}
}

+ 33
- 0
src/TensorFlowNET.Core/ops.py.cs View File

@@ -7,6 +7,7 @@ using Tensorflow;
using node_def_pb2 = Tensorflow;
using Google.Protobuf;
using System.Linq;
using NumSharp.Core;

namespace Tensorflow
{
@@ -223,5 +224,37 @@ namespace Tensorflow
var default_graph = get_default_graph();
default_graph._colocate_with_for_gradient(op, gradient_uid, ignore_existing);
}

/// <summary>
/// Uses the default session to evaluate one or more tensors.
/// </summary>
/// <param name="tensors">A single Tensor, or a list of Tensor objects.</param>
/// <param name="feed_dict">
/// A dictionary that maps Tensor objects (or tensor names) to lists,
/// numpy ndarrays, TensorProtos, or strings.
/// </param>
/// <param name="graph">The graph in which the tensors are defined.</param>
/// <param name="session">A different session to use to evaluate "tensors".</param>
/// <returns>
/// Either a single numpy ndarray if "tensors" is a single tensor; or a list
/// of numpy ndarrays that each correspond to the respective element in
/// "tensors".
/// </returns>
public static NDArray[] _eval_using_default_session(Tensor[] tensors, dynamic feed_dict, Graph graph, Session session = null)
{
if (session == null)
session = get_default_session();

return null;
}

/// <summary>
/// Returns the default session for the current thread.
/// </summary>
/// <returns>The default `Session` being used in the current thread.</returns>
public static Session get_default_session()
{
return null;
}
}
}

+ 1
- 1
test/TensorFlowNET.Examples/BasicOperations.cs View File

@@ -87,7 +87,7 @@ namespace TensorFlowNET.Examples
using (sess = tf.Session())
{
var result = sess.run(product);
Console.WriteLine(result);
Console.WriteLine(result.ToString());
if(result.Data<int>()[0] != 12)
{
throw new Exception("BasicOperations error");


+ 31
- 10
test/TensorFlowNET.UnitTest/ConstantTest.cs View File

@@ -11,21 +11,19 @@ namespace TensorFlowNET.UnitTest
[TestClass]
public class ConstantTest
{
Tensor tensor;

[TestMethod]
public void ScalarConst()
{
tensor = tf.constant(8); // int
tensor = tf.constant(6.0f); // float
tensor = tf.constant(6.0); // double
var tensor1 = tf.constant(8); // int
var tensor2 = tf.constant(6.0f); // float
var tensor3 = tf.constant(6.0); // double
}

[TestMethod]
public void StringConst()
{
string str = "Hello, TensorFlow.NET!";
tensor = tf.constant(str);
var tensor = tf.constant(str);
Python.with<Session>(tf.Session(), sess =>
{
var result = sess.run(tensor);
@@ -37,7 +35,7 @@ namespace TensorFlowNET.UnitTest
public void ZerosConst()
{
// small size
tensor = tf.zeros(new Shape(3, 2), TF_DataType.TF_INT32, "small");
var tensor = tf.zeros(new Shape(3, 2), TF_DataType.TF_INT32, "small");
Python.with<Session>(tf.Session(), sess =>
{
var result = sess.run(tensor);
@@ -67,11 +65,34 @@ namespace TensorFlowNET.UnitTest
{
var nd = np.array(new int[][]
{
new int[]{ 1, 2, 3 },
new int[]{ 4, 5, 6 }
new int[]{ 3, 1, 1 },
new int[]{ 2, 1, 3 }
});

tensor = tf.constant(nd);
var tensor = tf.constant(nd);
Python.with<Session>(tf.Session(), sess =>
{
var result = sess.run(tensor);
var data = result.Data<int>();

Assert.AreEqual(result.shape[0], 2);
Assert.AreEqual(result.shape[1], 3);
Assert.IsTrue(Enumerable.SequenceEqual(new int[] { 3, 1, 2, 1, 1, 3 }, data));
});
}

[TestMethod]
public void Multiply()
{
var a = tf.constant(3.0);
var b = tf.constant(2.0);
var c = a * b;

var sess = tf.Session();
double result = sess.run(c);
sess.close();

Assert.AreEqual(6.0, result);
}
}
}

+ 1
- 1
test/TensorFlowNET.UnitTest/ConsumersTest.cs View File

@@ -28,7 +28,7 @@ namespace TensorFlowNET.UnitTest

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

+ 11
- 2
test/TensorFlowNET.UnitTest/VariableTest.cs View File

@@ -33,8 +33,17 @@ namespace TensorFlowNET.UnitTest
[TestMethod]
public void ScalarVar()
{
var x = tf.Variable(3);
var y = tf.Variable(6f);
var x = tf.constant(3, name: "x");
var y = tf.Variable(x + 1, name: "y");

var model = tf.global_variables_initializer();

using (var session = tf.Session())
{
session.run(model);
int result = session.run(y);
Assert.AreEqual(result, 4);
}
}

/// <summary>


Loading…
Cancel
Save