Skip to content

Commit dc8a8cf

Browse files
committed
add tf.convert_to_tensor
1 parent cc0b994 commit dc8a8cf

9 files changed

Lines changed: 55 additions & 31 deletions

File tree

TensorFlow.NET.sln

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Examples", "t
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Core", "src\TensorFlowNET.Core\TensorFlowNET.Core.csproj", "{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}"
1111
EndProject
12-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{DE97EAD7-B92C-4112-9690-91C40A97179E}"
13-
EndProject
1412
Global
1513
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1614
Debug|Any CPU = Debug|Any CPU
@@ -29,10 +27,6 @@ Global
2927
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|Any CPU.Build.0 = Debug|Any CPU
3028
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.ActiveCfg = Release|Any CPU
3129
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.Build.0 = Release|Any CPU
32-
{DE97EAD7-B92C-4112-9690-91C40A97179E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33-
{DE97EAD7-B92C-4112-9690-91C40A97179E}.Debug|Any CPU.Build.0 = Debug|Any CPU
34-
{DE97EAD7-B92C-4112-9690-91C40A97179E}.Release|Any CPU.ActiveCfg = Release|Any CPU
35-
{DE97EAD7-B92C-4112-9690-91C40A97179E}.Release|Any CPU.Build.0 = Release|Any CPU
3630
EndGlobalSection
3731
GlobalSection(SolutionProperties) = preSolution
3832
HideSolutionNode = FALSE
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using NumSharp;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace Tensorflow
7+
{
8+
public static partial class tf
9+
{
10+
public static Tensor convert_to_tensor(object value,
11+
string name = null) => ops.convert_to_tensor(value, name: name);
12+
}
13+
}

src/TensorFlowNET.Core/Python.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected IEnumerable<int> range(int end)
2929

3030
protected IEnumerable<int> range(int start, int end)
3131
{
32-
return Enumerable.Range(start, end);
32+
return Enumerable.Range(start, end - start);
3333
}
3434

3535
public static T New<T>(object args) where T : IPyClass

src/TensorFlowNET.Core/TensorFlowNET.Core.csproj

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<AssemblyName>TensorFlow.NET</AssemblyName>
66
<RootNamespace>Tensorflow</RootNamespace>
7-
<Version>0.6.1-alpha</Version>
7+
<Version>0.6.1</Version>
88
<Authors>Haiping Chen</Authors>
99
<Company>SciSharp STACK</Company>
10-
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
10+
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
1111
<Copyright>Apache 2.0</Copyright>
1212
<RepositoryUrl>https://github.com/SciSharp/TensorFlow.NET</RepositoryUrl>
1313
<RepositoryType>git</RepositoryType>
@@ -44,7 +44,7 @@ More math/ linalg APIs.</PackageReleaseNotes>
4444

4545
<ItemGroup>
4646
<PackageReference Include="Google.Protobuf" Version="3.7.0" />
47-
<PackageReference Include="NumSharp" Version="0.9.0" />
47+
<PackageReference Include="NumSharp" Version="0.10.0-alpha" />
4848
</ItemGroup>
4949

5050
<ItemGroup>
@@ -57,8 +57,4 @@ More math/ linalg APIs.</PackageReleaseNotes>
5757
<Folder Include="Keras\Initializers\" />
5858
</ItemGroup>
5959

60-
<ItemGroup>
61-
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
62-
</ItemGroup>
63-
6460
</Project>

test/TensorFlowNET.Examples/KMeansClustering.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using NumSharp;
22
using System;
33
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Linq;
46
using System.Text;
57
using Tensorflow;
68
using Tensorflow.Clustering;
@@ -26,7 +28,7 @@ public class KMeansClustering : Python, IExample
2628

2729
Datasets mnist;
2830
NDArray full_data_x;
29-
int num_steps = 50; // Total steps to train
31+
int num_steps = 10; // Total steps to train
3032
int k = 25; // The number of clusters
3133
int num_classes = 10; // The 10 digits
3234
int num_features = 784; // Each image is 28x28 pixels
@@ -63,22 +65,43 @@ public bool Run()
6365

6466
// Training
6567
NDArray result = null;
66-
foreach(var i in range(1, num_steps + 1))
68+
var sw = new Stopwatch();
69+
70+
foreach (var i in range(1, num_steps + 1))
6771
{
72+
sw.Start();
6873
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]}");
74+
sw.Stop();
75+
76+
if (i % 5 == 0 || i == 1)
77+
print($"Step {i}, Avg Distance: {result[1]} Elapse: {sw.ElapsedMilliseconds}ms");
78+
79+
sw.Reset();
7180
}
7281

73-
var idx = result[2];
82+
var idx = result[2].Data<int>();
7483

7584
// Assign a label to each centroid
7685
// Count total number of labels per centroid, using the label of each training
7786
// 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];
87+
var counts = np.zeros((k, num_classes), np.float32);
88+
89+
sw.Start();
90+
foreach (var i in range(idx.Length))
91+
{
92+
var x = mnist.train.labels[i];
93+
counts[idx[i]] += x;
94+
}
95+
96+
sw.Stop();
97+
print($"Assign a label to each centroid took {sw.ElapsedMilliseconds}ms");
98+
99+
// Assign the most frequent label to the centroid
100+
var labels_map_array = np.argmax(counts, 1);
101+
var labels_map = tf.convert_to_tensor(labels_map_array);
81102

103+
// Evaluation ops
104+
// Lookup: centroid_id -> label
82105
});
83106

84107
return false;

test/TensorFlowNET.Examples/LogisticRegression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public bool Run()
8888

8989
// Display logs per epoch step
9090
if ((epoch + 1) % display_step == 0)
91-
print($"Epoch: {(epoch + 1).ToString("D4")} cost= {avg_cost.ToString("G9")} elapse= {sw.ElapsedMilliseconds}ms");
91+
print($"Epoch: {(epoch + 1).ToString("D4")} Cost: {avg_cost.ToString("G9")} Elapse: {sw.ElapsedMilliseconds}ms");
9292

9393
sw.Reset();
9494
}

test/TensorFlowNET.Examples/NearestNeighbor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ public bool Run()
5151
long nn_index = sess.run(pred, new FeedItem(xtr, Xtr), new FeedItem(xte, Xte[i]));
5252
// Get nearest neighbor class label and compare it to its true label
5353
int index = (int)nn_index;
54-
print($"Test {i} Prediction: {np.argmax(Ytr[(NDArray)index])} True Class: {np.argmax(Yte[i] as NDArray)}");
54+
print($"Test {i} Prediction: {np.argmax(Ytr[index])} True Class: {np.argmax(Yte[i])}");
5555
// Calculate accuracy
56-
if (np.argmax(Ytr[(NDArray)index]) == np.argmax(Yte[i] as NDArray))
56+
if ((int)np.argmax(Ytr[index]) == (int)np.argmax(Yte[i]))
5757
accuracy += 1f/ Xte.shape[0];
5858
}
5959

test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
<TargetFramework>netcoreapp2.2</TargetFramework>
66
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
77
</PropertyGroup>
88

99
<ItemGroup>
1010
<PackageReference Include="Colorful.Console" Version="1.2.9" />
11-
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
11+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
1212
<PackageReference Include="SharpZipLib" Version="1.1.0" />
1313
<PackageReference Include="System.Drawing.Common" Version="4.5.1" />
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
1817
<ProjectReference Include="..\..\src\TensorFlowNET.Core\TensorFlowNET.Core.csproj" />
1918
</ItemGroup>
2019

test/TensorFlowNET.UnitTest/TensorFlowNET.UnitTest.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.1</TargetFramework>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
@@ -22,7 +22,6 @@
2222
</ItemGroup>
2323

2424
<ItemGroup>
25-
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
2625
<ProjectReference Include="..\..\src\TensorFlowNET.Core\TensorFlowNET.Core.csproj" />
2726
<ProjectReference Include="..\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj" />
2827
</ItemGroup>

0 commit comments

Comments
 (0)