Skip to content

Commit 1b7003b

Browse files
committed
Tensor.eval() SciSharp#145
1 parent fdd71f6 commit 1b7003b

11 files changed

Lines changed: 103 additions & 19 deletions

File tree

‎src/TensorFlowNET.Core/Operations/OpDefLibrary.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public Operation _apply_op_helper(string op_type_name, string name = "", dynamic
5151
var input_name = input_arg.Name;
5252
if (keywords[input_name] is double int_value)
5353
{
54-
keywords[input_name] = constant_op.Constant(int_value, input_name);
54+
keywords[input_name] = constant_op.constant(int_value, input_name);
5555
}
5656

5757
if (keywords[input_name] is Tensor value)

‎src/TensorFlowNET.Core/Operations/array_ops.py.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ private static Tensor _constant_if_small<T>(T value, Shape shape, TF_DataType dt
4343
var nd = np.zeros<T>(shape);
4444
if (shape.Size < 1000)
4545
{
46-
return constant_op.Constant(nd, name);
46+
return constant_op.constant(nd, name);
4747
}
4848
else
4949
{
5050
tShape = constant_op._tensor_shape_tensor_conversion_function(shape.as_shape());
51-
var c = constant_op.Constant(0);
51+
var c = constant_op.constant(0);
5252
return gen_array_ops.fill(tShape, c, name);
5353
}
5454
}

‎src/TensorFlowNET.Core/Operations/gen_math_ops.cs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,18 @@ public static Tensor real_div(Tensor x, Tensor y)
3838
return _op.outputs[0];
3939
}
4040

41-
public static Tensor mat_mul(Tensor a, Tensor b, bool transpose_a = false, bool transpose_b = false)
41+
/// <summary>
42+
/// Multiply the matrix "a" by the matrix "b".
43+
/// </summary>
44+
/// <param name="a"></param>
45+
/// <param name="b"></param>
46+
/// <param name="transpose_a"></param>
47+
/// <param name="transpose_b"></param>
48+
/// <param name="name"></param>
49+
/// <returns></returns>
50+
public static Tensor mat_mul(Tensor a, Tensor b, bool transpose_a = false, bool transpose_b = false, string name = "")
4251
{
43-
var _op = _op_def_lib._apply_op_helper("MatMul", args: new { a, b, transpose_a, transpose_b });
52+
var _op = _op_def_lib._apply_op_helper("MatMul", name, args: new { a, b, transpose_a, transpose_b });
4453

4554
return _op.outputs[0];
4655
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow
6+
{
7+
public class math_ops
8+
{
9+
public static Tensor matmul(Tensor a, Tensor b,
10+
bool transpose_a = false, bool transpose_b = false,
11+
bool adjoint_a = false, bool adjoint_b = false,
12+
bool a_is_sparse = false, bool b_is_sparse = false,
13+
string name = "")
14+
{
15+
Tensor result = null;
16+
17+
Python.with<ops.name_scope>(new ops.name_scope(name, "MatMul", new Tensor[] { a, b }), scope =>
18+
{
19+
name = scope;
20+
21+
if (transpose_a && adjoint_a)
22+
throw new ValueError("Only one of transpose_a and adjoint_a can be True.");
23+
if (transpose_b && adjoint_b)
24+
throw new ValueError("Only one of transpose_b and adjoint_b can be True.");
25+
26+
a = ops.convert_to_tensor(a, name: "a");
27+
b = ops.convert_to_tensor(b, name: "b");
28+
29+
result = gen_math_ops.mat_mul(a, b, transpose_a, transpose_b, name);
30+
});
31+
32+
return result;
33+
}
34+
}
35+
}

‎src/TensorFlowNET.Core/Sessions/Session.cs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ namespace Tensorflow
77
public class Session : BaseSession, IPython
88
{
99
private IntPtr _handle;
10-
public Status Status { get; }
10+
public Status Status = new Status();
1111
public SessionOptions Options { get; }
12+
public Graph graph;
1213

1314
public Session(string target = "", Graph graph = null)
1415
{
15-
Status = new Status();
1616
if(graph == null)
1717
{
1818
graph = tf.get_default_graph();
1919
}
20+
this.graph = graph;
2021
Options = new SessionOptions();
2122
_handle = c_api.TF_NewSession(graph, Options, Status);
2223
Status.Check();
@@ -27,9 +28,12 @@ public Session(IntPtr handle)
2728
_handle = handle;
2829
}
2930

30-
public Session(Graph graph, SessionOptions opts, Status s)
31+
public Session(Graph graph, SessionOptions opts, Status s = null)
3132
{
33+
if (s == null)
34+
s = Status;
3235
_handle = c_api.TF_NewSession(graph, opts, s);
36+
Status.Check(true);
3337
}
3438

3539
public static implicit operator IntPtr(Session session) => session._handle;

‎src/TensorFlowNET.Core/Tensors/Tensor.Implicit.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ public partial class Tensor
88
{
99
public static implicit operator Tensor(double scalar)
1010
{
11-
return constant_op.Constant(scalar);
11+
return constant_op.constant(scalar);
1212
}
1313

1414
public static implicit operator Tensor(int scalar)
1515
{
16-
return constant_op.Constant(scalar);
16+
return constant_op.constant(scalar);
1717
}
1818

1919
public static implicit operator int(Tensor tensor)

‎src/TensorFlowNET.Core/Tensors/Tensor.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ public Tensor MaybeMove()
213213
/// <param name="feed_dict">A dictionary that maps `Tensor` objects to feed values.</param>
214214
/// <param name="session">The `Session` to be used to evaluate this tensor.</param>
215215
/// <returns></returns>
216-
public NDArray eval(dynamic feed_dict = null, Session session = null)
216+
public NDArray eval(Dictionary<Tensor, NDArray> feed_dict = null, Session session = null)
217217
{
218-
return ops._eval_using_default_session(new Tensor[] { this }, feed_dict, Graph, session)[0];
218+
return ops._eval_using_default_session(this, feed_dict, Graph, session);
219219
}
220220

221221
public TF_DataType ToTFDataType(Type type)

‎src/TensorFlowNET.Core/Tensors/constant_op.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class constant_op
1919
/// <param name="name">Optional name for the tensor.</param>
2020
/// <param name="verify_shape">Boolean that enables verification of a shape of values.</param>
2121
/// <returns></returns>
22-
public static Tensor Constant(NDArray nd, string name = "Const", bool verify_shape = false)
22+
public static Tensor constant(NDArray nd, string name = "Const", bool verify_shape = false)
2323
{
2424
Graph g = ops.get_default_graph();
2525
var tensor_pb = tensor_util.make_tensor_proto(nd, verify_shape);
@@ -76,7 +76,7 @@ public static Tensor _tensor_shape_tensor_conversion_function(TensorShape s, TF_
7676
if (string.IsNullOrEmpty(name))
7777
name = "shape_as_tensor";
7878

79-
return constant_op.Constant(s_list, name);
79+
return constant_op.constant(s_list, name);
8080
}
8181
}
8282
}

‎src/TensorFlowNET.Core/Tensors/tf.constant.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static partial class tf
99
{
1010
public static Tensor constant(NDArray nd, string name = "Const", bool verify_shape = false)
1111
{
12-
return constant_op.Constant(nd, name, verify_shape);
12+
return constant_op.constant(nd, name, verify_shape);
1313
}
1414

1515
public static Tensor zeros(Shape shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = "")

‎src/TensorFlowNET.Core/ops.py.cs‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static Tensor convert_to_tensor(object value, TF_DataType dtype = TF_Data
7575
return val;
7676
default:
7777
var nd = tensor_util.convert_to_numpy_ndarray(value);
78-
return constant_op.Constant(nd, name);
78+
return constant_op.constant(nd, name);
7979
}
8080
}
8181

@@ -240,12 +240,34 @@ private static void _colocate_with_for_gradient(Operation op, int? gradient_uid,
240240
/// of numpy ndarrays that each correspond to the respective element in
241241
/// "tensors".
242242
/// </returns>
243-
public static NDArray[] _eval_using_default_session(Tensor[] tensors, dynamic feed_dict, Graph graph, Session session = null)
243+
public static NDArray _eval_using_default_session(Tensor tensor, Dictionary<Tensor, NDArray> feed_dict, Graph graph, Session session = null)
244244
{
245245
if (session == null)
246+
{
246247
session = get_default_session();
247248

248-
return null;
249+
if (session == null)
250+
throw new ValueError("Cannot evaluate tensor using `eval()`: No default " +
251+
"session is registered. Use `with " +
252+
"sess.as_default()` or pass an explicit session to " +
253+
"`eval(session=sess)`");
254+
255+
if (session.graph != graph)
256+
throw new ValueError("Cannot use the default session to evaluate tensor: " +
257+
"the tensor's graph is different from the session's " +
258+
"graph. Pass an explicit session to " +
259+
"`eval(session=sess)`.");
260+
}
261+
else
262+
{
263+
if (session.graph != graph)
264+
throw new ValueError("Cannot use the default session to evaluate tensor: " +
265+
"the tensor's graph is different from the session's " +
266+
"graph. Pass an explicit session to " +
267+
"`eval(session=sess)`.");
268+
}
269+
270+
return session.run(tensor, feed_dict);
249271
}
250272

251273
/// <summary>
@@ -254,7 +276,7 @@ public static NDArray[] _eval_using_default_session(Tensor[] tensors, dynamic fe
254276
/// <returns>The default `Session` being used in the current thread.</returns>
255277
public static Session get_default_session()
256278
{
257-
return null;
279+
return tf.Session();
258280
}
259281
}
260282
}

0 commit comments

Comments
 (0)