Skip to content

Commit fb23b9f

Browse files
committed
# Conflicts: # README.md # src/TensorFlowNET.Core/Sessions/_FetchMapper.cs
2 parents f8324e8 + 98af206 commit fb23b9f

5 files changed

Lines changed: 57 additions & 21 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# TensorFlow.NET
22
TensorFlow.NET provides a .NET Standard binding for [TensorFlow](https://www.tensorflow.org/). It aims to implement the complete Tensorflow API in CSharp which allows .NET developers to develop, train and deploy Machine Learning models with the cross-platform .NET Standard framework.
3-
Here is a simple test
3+
TensorFlow.NET (TF.NET) provides a .NET Standard binding for [TensorFlow](https://www.tensorflow.org/). It aims to implement the complete Tensorflow API in CSharp which allows .NET developers to develop, train and deploy Machine Learning models with the cross-platform .NET Standard framework.
4+
45
[![Join the chat at https://gitter.im/publiclab/publiclab](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/sci-sharp/community)
56
[![Tensorflow.NET](https://ci.appveyor.com/api/projects/status/wx4td43v2d3f2xj6?svg=true)](https://ci.appveyor.com/project/Haiping-Chen/tensorflow-net)
67
[![codecov](https://codecov.io/gh/SciSharp/NumSharp/branch/master/graph/badge.svg)](https://codecov.io/gh/SciSharp/NumSharp)
78
[![NuGet](https://img.shields.io/nuget/dt/TensorFlow.NET.svg)](https://www.nuget.org/packages/TensorFlow.NET)
89
[![Documentation Status](https://readthedocs.org/projects/tensorflownet/badge/?version=latest)](https://tensorflownet.readthedocs.io/en/latest/?badge=latest)
910
[![Badge](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu/#/en_US)
1011

11-
TensorFlow.NET is a member project of [SciSharp STACK](https://github.com/SciSharp).
12+
TF.NET is a member project of [SciSharp STACK](https://github.com/SciSharp).
1213

1314
![tensors_flowing](docs/assets/tensors_flowing.gif)
1415

@@ -24,14 +25,14 @@ In comparison to other projects, like for instance TensorFlowSharp which only pr
2425

2526
### How to use
2627

27-
Install TensorFlow.NET through NuGet.
28+
Install TF.NET through NuGet.
2829
```sh
2930
PM> Install-Package TensorFlow.NET
3031
```
3132

3233
If you are using Linux or Mac OS, please download the pre-compiled dll [here](tensorflowlib) and place it in the working folder. This is only need for Linux and Mac OS, and already packed into NuGet for Windows.
3334

34-
Import tensorflow.net.
35+
Import TF.NET.
3536

3637
```cs
3738
using Tensorflow;

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public static _FetchMapper for_fetch(object fetch)
1717
if (fetch.GetType().IsArray)
1818
return new _ListFetchMapper(fetches);
1919
else
20-
return new _ElementFetchMapper(fetches, (List<object> fetched_vals) => fetched_vals[0]);
20+
return new _ElementFetchMapper(fetches, (List<NDArray> fetched_vals) => fetched_vals[0]);
2121
}
2222

23-
public virtual NDArray build_results(List<object> values)
23+
public virtual NDArray build_results(List<NDArray> values)
2424
{
2525
var type = values[0].GetType();
2626
var nd = new NDArray(type, values.Count);
@@ -31,16 +31,12 @@ public virtual NDArray build_results(List<object> values)
3131
nd.SetData(values.Select(x => (float)x).ToArray());
3232
break;
3333
case "NDArray":
34-
// nd.SetData<NDArray>(values.ToArray());
35-
//NDArray[] arr = new NDArray[values.Count];
36-
//for (int i=0; i<values.Count; i++)
3734
NDArray[] arr = values.Select(x => (NDArray)x).ToArray();
3835
nd = new NDArray(arr);
3936
break;
4037
default:
4138
break;
4239
}
43-
4440
return nd;
4541
}
4642

‎test/TensorFlowNET.Examples/KMeansClustering.cs‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,54 @@ public class KMeansClustering : Python, IExample
3333

3434
public bool Run()
3535
{
36+
PrepareData();
37+
38+
var graph = tf.Graph().as_default();
39+
3640
tf.train.import_meta_graph("kmeans.meta");
3741

3842
// Input images
39-
var X = tf.placeholder(tf.float32, shape: new TensorShape(-1, num_features));
43+
var X = graph.get_operation_by_name("Placeholder").output; // tf.placeholder(tf.float32, shape: new TensorShape(-1, num_features));
4044
// Labels (for assigning a label to a centroid and testing)
41-
var Y = tf.placeholder(tf.float32, shape: new TensorShape(-1, num_classes));
45+
var Y = graph.get_operation_by_name("Placeholder_1").output; // tf.placeholder(tf.float32, shape: new TensorShape(-1, num_classes));
4246

4347
// K-Means Parameters
44-
var kmeans = new KMeans(X, k, distance_metric: KMeans.COSINE_DISTANCE, use_mini_batch: true);
48+
//var kmeans = new KMeans(X, k, distance_metric: KMeans.COSINE_DISTANCE, use_mini_batch: true);
4549

4650
// Build KMeans graph
47-
var training_graph = kmeans.training_graph();
48-
51+
//var training_graph = kmeans.training_graph();
52+
53+
var init_vars = tf.global_variables_initializer();
54+
Tensor init_op = graph.get_operation_by_name("cond/Merge");
55+
var train_op = graph.get_operation_by_name("group_deps");
56+
Tensor avg_distance = graph.get_operation_by_name("Mean");
57+
Tensor cluster_idx = graph.get_operation_by_name("Squeeze_1");
58+
59+
with(tf.Session(graph), sess =>
60+
{
61+
sess.run(init_vars, new FeedItem(X, full_data_x));
62+
sess.run(init_op, new FeedItem(X, full_data_x));
63+
64+
// Training
65+
NDArray result = null;
66+
foreach(var i in range(1, num_steps + 1))
67+
{
68+
result = sess.run(new ITensorOrOperation[] { train_op, avg_distance, cluster_idx }, new FeedItem(X, full_data_x));
69+
if (i % 2 == 0 || i == 1)
70+
print($"Step {i}, Avg Distance: {result[1]}");
71+
}
72+
73+
var idx = result[2];
74+
75+
// Assign a label to each centroid
76+
// Count total number of labels per centroid, using the label of each training
77+
// sample to their closest centroid (given by 'idx')
78+
var counts = np.zeros(k, num_classes);
79+
foreach (var i in range(idx.len))
80+
counts[idx[i]] += mnist.train.labels[i];
81+
82+
});
83+
4984
return false;
5085
}
5186

‎test/TensorFlowNET.Examples/ObjectDetection.cs‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,9 @@ public bool Run()
5050
with(tf.Session(graph), sess =>
5151
{
5252
var results = sess.run(outTensorArr, new FeedItem(imgTensor, imgArr));
53-
54-
//NDArray scores = results.Array.GetValue(2) as NDArray;
55-
56-
//floatscores.Data<float>();
53+
5754
NDArray[] resultArr = results.Data<NDArray>();
58-
59-
//float[] scores = resultArr[2].Data<float>();
55+
6056
buildOutputImage(resultArr);
6157
});
6258

‎test/TensorFlowNET.UnitTest/OperationsTest.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using System;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Text;
56
using Tensorflow;
67
using Buffer = Tensorflow.Buffer;
@@ -20,6 +21,13 @@ public void GetAllOpList()
2021
var handle = c_api.TF_GetAllOpList();
2122
var buffer = new Buffer(handle);
2223
var op_list = OpList.Parser.ParseFrom(buffer);
24+
25+
var _registered_ops = new Dictionary<string, OpDef>();
26+
foreach (var op_def in op_list.Op)
27+
_registered_ops[op_def.Name] = op_def;
28+
29+
// r1.14 added NN op
30+
var op = _registered_ops.FirstOrDefault(x => x.Key == "NearestNeighbors");
2331
Assert.IsTrue(op_list.Op.Count > 1000);
2432
}
2533

0 commit comments

Comments
 (0)