Skip to content

Commit 11216bf

Browse files
committed
Finished BasicOperations example.
1 parent af159f8 commit 11216bf

4 files changed

Lines changed: 95 additions & 12 deletions

File tree

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,36 @@ private unsafe object[] _call_tf_sessionrun(KeyValuePair<TF_Output, Tensor>[] fe
114114
for (int i = 0; i < fetch_list.Length; i++)
115115
{
116116
var tensor = new Tensor(output_values[i]);
117-
117+
Type type = tensor.dtype.as_numpy_datatype();
118+
var ndims = tensor.shape.Select(x => (int)x).ToArray();
119+
118120
switch (tensor.dtype)
119121
{
120122
case TF_DataType.TF_STRING:
121-
// wired, don't know why we have to start from offset 9.
122-
var bytes = tensor.Data();
123-
result[i] = UTF8Encoding.Default.GetString(bytes, 9, bytes.Length - 9);
123+
{
124+
// wired, don't know why we have to start from offset 9.
125+
var bytes = tensor.Data();
126+
var output = UTF8Encoding.Default.GetString(bytes, 9, bytes.Length - 9);
127+
result[i] = tensor.NDims == 0 ? output : np.array(output).reshape(ndims);
128+
}
124129
break;
125130
case TF_DataType.TF_FLOAT:
126-
result[i] = *(float*)c_api.TF_TensorData(output_values[i]);
131+
{
132+
var output = *(float*)c_api.TF_TensorData(output_values[i]);
133+
result[i] = tensor.NDims == 0 ? output : np.array(output).reshape(ndims);
134+
}
127135
break;
128136
case TF_DataType.TF_INT16:
129-
result[i] = *(short*)c_api.TF_TensorData(output_values[i]);
137+
{
138+
var output = *(short*)c_api.TF_TensorData(output_values[i]);
139+
result[i] = tensor.NDims == 0 ? output : np.array(output).reshape(ndims);
140+
}
130141
break;
131142
case TF_DataType.TF_INT32:
132-
result[i] = *(int*)c_api.TF_TensorData(output_values[i]);
143+
{
144+
var output = *(int*)c_api.TF_TensorData(output_values[i]);
145+
result[i] = tensor.NDims == 0 ? output : np.array(output).reshape(ndims);
146+
}
133147
break;
134148
default:
135149
throw new NotImplementedException("can't get output");

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public partial class Tensor : IDisposable
2222
public object value;
2323
public int value_index { get; }
2424

25+
private Status status = new Status();
26+
2527
private TF_DataType _dtype = TF_DataType.DtInvalid;
2628
public TF_DataType dtype => _handle == IntPtr.Zero ? _dtype : c_api.TF_TensorType(_handle);
2729
public ulong bytesize => _handle == IntPtr.Zero ? 0 : c_api.TF_TensorByteSize(_handle);
@@ -33,8 +35,17 @@ public long[] shape
3335
get
3436
{
3537
var dims = new long[rank];
36-
for (int i = 0; i < rank; i++)
37-
dims[i] = c_api.TF_Dim(_handle, i);
38+
39+
if (_handle == IntPtr.Zero)
40+
{
41+
c_api.TF_GraphGetTensorShape(op.Graph, _as_tf_output(), dims, rank, status);
42+
status.Check();
43+
}
44+
else
45+
{
46+
for (int i = 0; i < rank; i++)
47+
dims[i] = c_api.TF_Dim(_handle, i);
48+
}
3849

3950
return dims;
4051
}
@@ -48,7 +59,22 @@ public long[] shape
4859
/// 3 3-Tensor (cube of numbers)
4960
/// n n-Tensor (you get the idea)
5061
/// </summary>
51-
public int rank => _handle == IntPtr.Zero ? 0 : c_api.TF_NumDims(_handle);
62+
public int rank
63+
{
64+
get
65+
{
66+
if (_handle == IntPtr.Zero)
67+
{
68+
var output = _as_tf_output();
69+
return c_api.TF_GraphGetTensorNumDims(op.Graph, output, status);
70+
}
71+
else
72+
{
73+
return c_api.TF_NumDims(_handle);
74+
}
75+
}
76+
}
77+
5278
public int NDims => rank;
5379

5480
/// <summary>
@@ -182,6 +208,7 @@ public TF_DataType ToTFDataType(Type type)
182208
public void Dispose()
183209
{
184210
c_api.TF_DeleteTensor(_handle);
211+
status.Dispose();
185212
}
186213

187214
public static implicit operator IntPtr(Tensor tensor)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ namespace Tensorflow
66
{
77
public static class dtypes
88
{
9+
public static Type as_numpy_datatype(this TF_DataType type)
10+
{
11+
switch (type)
12+
{
13+
case TF_DataType.TF_INT32:
14+
return typeof(int);
15+
default:
16+
throw new NotImplementedException("as_numpy_datatype failed");
17+
}
18+
}
919
public static TF_DataType as_dtype(Type type)
1020
{
1121
TF_DataType dtype = TF_DataType.DtInvalid;

‎test/TensorFlowNET.Examples/BasicOperations.cs‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void Run()
1919
// Basic constant operations
2020
// The value returned by the constructor represents the output
2121
// of the Constant op.
22-
var a = tf.constant(2);
22+
/*var a = tf.constant(2);
2323
var b = tf.constant(3);
2424
2525
// Launch the default graph.
@@ -50,7 +50,7 @@ public void Run()
5050
// Run every operation with variable input
5151
Console.WriteLine($"Addition with variables: {sess.run(add, feed_dict)}");
5252
Console.WriteLine($"Multiplication with variables: {sess.run(mul, feed_dict)}");
53-
}
53+
}*/
5454

5555
// ----------------
5656
// More in details:
@@ -61,7 +61,39 @@ public void Run()
6161
//
6262
// The value returned by the constructor represents the output
6363
// of the Constant op.
64+
var nd1 = np.array(3, 3).reshape(1, 2);
65+
var matrix1 = tf.constant(nd1);
66+
67+
// Create another Constant that produces a 2x1 matrix.
68+
var nd2 = np.array(2, 2).reshape(2, 1);
69+
var matrix2 = tf.constant(nd2);
70+
71+
// Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.
72+
// The returned value, 'product', represents the result of the matrix
73+
// multiplication.
74+
var product = tf.matmul(matrix1, matrix2);
6475

76+
// To run the matmul op we call the session 'run()' method, passing 'product'
77+
// which represents the output of the matmul op. This indicates to the call
78+
// that we want to get the output of the matmul op back.
79+
//
80+
// All inputs needed by the op are run automatically by the session. They
81+
// typically are run in parallel.
82+
//
83+
// The call 'run(product)' thus causes the execution of threes ops in the
84+
// graph: the two constants and matmul.
85+
//
86+
// The output of the op is returned in 'result' as a numpy `ndarray` object.
87+
using (sess = tf.Session())
88+
{
89+
var result = sess.run(product);
90+
Console.WriteLine(result);
91+
if((result as NDArray).Data<int>()[0] != 12)
92+
{
93+
throw new Exception("BasicOperations error");
94+
}
95+
// ==> [[ 12.]]
96+
}
6597
}
6698
}
6799
}

0 commit comments

Comments
 (0)