| @@ -9,8 +9,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Examples", "t | |||||
| EndProject | EndProject | ||||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Core", "src\TensorFlowNET.Core\TensorFlowNET.Core.csproj", "{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}" | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Core", "src\TensorFlowNET.Core\TensorFlowNET.Core.csproj", "{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}" | ||||
| EndProject | EndProject | ||||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{DE97EAD7-B92C-4112-9690-91C40A97179E}" | |||||
| EndProject | |||||
| Global | Global | ||||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||||
| Debug|Any CPU = Debug|Any CPU | Debug|Any CPU = Debug|Any CPU | ||||
| @@ -29,10 +27,6 @@ Global | |||||
| {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|Any CPU.Build.0 = Debug|Any CPU | {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||||
| {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.ActiveCfg = Release|Any CPU | {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||||
| {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.Build.0 = Release|Any CPU | {FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.Build.0 = Release|Any CPU | ||||
| {DE97EAD7-B92C-4112-9690-91C40A97179E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |||||
| {DE97EAD7-B92C-4112-9690-91C40A97179E}.Debug|Any CPU.Build.0 = Debug|Any CPU | |||||
| {DE97EAD7-B92C-4112-9690-91C40A97179E}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||||
| {DE97EAD7-B92C-4112-9690-91C40A97179E}.Release|Any CPU.Build.0 = Release|Any CPU | |||||
| EndGlobalSection | EndGlobalSection | ||||
| GlobalSection(SolutionProperties) = preSolution | GlobalSection(SolutionProperties) = preSolution | ||||
| HideSolutionNode = FALSE | HideSolutionNode = FALSE | ||||
| @@ -0,0 +1,13 @@ | |||||
| using NumSharp; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Text; | |||||
| namespace Tensorflow | |||||
| { | |||||
| public static partial class tf | |||||
| { | |||||
| public static Tensor convert_to_tensor(object value, | |||||
| string name = null) => ops.convert_to_tensor(value, name: name); | |||||
| } | |||||
| } | |||||
| @@ -29,7 +29,7 @@ namespace Tensorflow | |||||
| protected IEnumerable<int> range(int start, int end) | protected IEnumerable<int> range(int start, int end) | ||||
| { | { | ||||
| return Enumerable.Range(start, end); | |||||
| return Enumerable.Range(start, end - start); | |||||
| } | } | ||||
| public static T New<T>(object args) where T : IPyClass | public static T New<T>(object args) where T : IPyClass | ||||
| @@ -4,10 +4,10 @@ | |||||
| <TargetFramework>netstandard2.0</TargetFramework> | <TargetFramework>netstandard2.0</TargetFramework> | ||||
| <AssemblyName>TensorFlow.NET</AssemblyName> | <AssemblyName>TensorFlow.NET</AssemblyName> | ||||
| <RootNamespace>Tensorflow</RootNamespace> | <RootNamespace>Tensorflow</RootNamespace> | ||||
| <Version>0.6.1-alpha</Version> | |||||
| <Version>0.6.1</Version> | |||||
| <Authors>Haiping Chen</Authors> | <Authors>Haiping Chen</Authors> | ||||
| <Company>SciSharp STACK</Company> | <Company>SciSharp STACK</Company> | ||||
| <GeneratePackageOnBuild>true</GeneratePackageOnBuild> | |||||
| <GeneratePackageOnBuild>false</GeneratePackageOnBuild> | |||||
| <Copyright>Apache 2.0</Copyright> | <Copyright>Apache 2.0</Copyright> | ||||
| <RepositoryUrl>https://github.com/SciSharp/TensorFlow.NET</RepositoryUrl> | <RepositoryUrl>https://github.com/SciSharp/TensorFlow.NET</RepositoryUrl> | ||||
| <RepositoryType>git</RepositoryType> | <RepositoryType>git</RepositoryType> | ||||
| @@ -44,7 +44,7 @@ More math/ linalg APIs.</PackageReleaseNotes> | |||||
| <ItemGroup> | <ItemGroup> | ||||
| <PackageReference Include="Google.Protobuf" Version="3.7.0" /> | <PackageReference Include="Google.Protobuf" Version="3.7.0" /> | ||||
| <PackageReference Include="NumSharp" Version="0.9.0" /> | |||||
| <PackageReference Include="NumSharp" Version="0.10.0-alpha" /> | |||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| @@ -57,8 +57,4 @@ More math/ linalg APIs.</PackageReleaseNotes> | |||||
| <Folder Include="Keras\Initializers\" /> | <Folder Include="Keras\Initializers\" /> | ||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | |||||
| <ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" /> | |||||
| </ItemGroup> | |||||
| </Project> | </Project> | ||||
| @@ -1,6 +1,8 @@ | |||||
| using NumSharp; | using NumSharp; | ||||
| using System; | using System; | ||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| using System.Diagnostics; | |||||
| using System.Linq; | |||||
| using System.Text; | using System.Text; | ||||
| using Tensorflow; | using Tensorflow; | ||||
| using Tensorflow.Clustering; | using Tensorflow.Clustering; | ||||
| @@ -26,7 +28,7 @@ namespace TensorFlowNET.Examples | |||||
| Datasets mnist; | Datasets mnist; | ||||
| NDArray full_data_x; | NDArray full_data_x; | ||||
| int num_steps = 50; // Total steps to train | |||||
| int num_steps = 10; // Total steps to train | |||||
| int k = 25; // The number of clusters | int k = 25; // The number of clusters | ||||
| int num_classes = 10; // The 10 digits | int num_classes = 10; // The 10 digits | ||||
| int num_features = 784; // Each image is 28x28 pixels | int num_features = 784; // Each image is 28x28 pixels | ||||
| @@ -63,22 +65,43 @@ namespace TensorFlowNET.Examples | |||||
| // Training | // Training | ||||
| NDArray result = null; | NDArray result = null; | ||||
| foreach(var i in range(1, num_steps + 1)) | |||||
| var sw = new Stopwatch(); | |||||
| foreach (var i in range(1, num_steps + 1)) | |||||
| { | { | ||||
| sw.Start(); | |||||
| result = sess.run(new ITensorOrOperation[] { train_op, avg_distance, cluster_idx }, new FeedItem(X, full_data_x)); | result = sess.run(new ITensorOrOperation[] { train_op, avg_distance, cluster_idx }, new FeedItem(X, full_data_x)); | ||||
| if (i % 2 == 0 || i == 1) | |||||
| print($"Step {i}, Avg Distance: {result[1]}"); | |||||
| sw.Stop(); | |||||
| if (i % 5 == 0 || i == 1) | |||||
| print($"Step {i}, Avg Distance: {result[1]} Elapse: {sw.ElapsedMilliseconds}ms"); | |||||
| sw.Reset(); | |||||
| } | } | ||||
| var idx = result[2]; | |||||
| var idx = result[2].Data<int>(); | |||||
| // Assign a label to each centroid | // Assign a label to each centroid | ||||
| // Count total number of labels per centroid, using the label of each training | // Count total number of labels per centroid, using the label of each training | ||||
| // sample to their closest centroid (given by 'idx') | // sample to their closest centroid (given by 'idx') | ||||
| var counts = np.zeros(k, num_classes); | |||||
| foreach (var i in range(idx.len)) | |||||
| counts[idx[i]] += mnist.train.labels[i]; | |||||
| var counts = np.zeros((k, num_classes), np.float32); | |||||
| sw.Start(); | |||||
| foreach (var i in range(idx.Length)) | |||||
| { | |||||
| var x = mnist.train.labels[i]; | |||||
| counts[idx[i]] += x; | |||||
| } | |||||
| sw.Stop(); | |||||
| print($"Assign a label to each centroid took {sw.ElapsedMilliseconds}ms"); | |||||
| // Assign the most frequent label to the centroid | |||||
| var labels_map_array = np.argmax(counts, 1); | |||||
| var labels_map = tf.convert_to_tensor(labels_map_array); | |||||
| // Evaluation ops | |||||
| // Lookup: centroid_id -> label | |||||
| }); | }); | ||||
| return false; | return false; | ||||
| @@ -88,7 +88,7 @@ namespace TensorFlowNET.Examples | |||||
| // Display logs per epoch step | // Display logs per epoch step | ||||
| if ((epoch + 1) % display_step == 0) | if ((epoch + 1) % display_step == 0) | ||||
| print($"Epoch: {(epoch + 1).ToString("D4")} cost= {avg_cost.ToString("G9")} elapse= {sw.ElapsedMilliseconds}ms"); | |||||
| print($"Epoch: {(epoch + 1).ToString("D4")} Cost: {avg_cost.ToString("G9")} Elapse: {sw.ElapsedMilliseconds}ms"); | |||||
| sw.Reset(); | sw.Reset(); | ||||
| } | } | ||||
| @@ -51,9 +51,9 @@ namespace TensorFlowNET.Examples | |||||
| long nn_index = sess.run(pred, new FeedItem(xtr, Xtr), new FeedItem(xte, Xte[i])); | long nn_index = sess.run(pred, new FeedItem(xtr, Xtr), new FeedItem(xte, Xte[i])); | ||||
| // Get nearest neighbor class label and compare it to its true label | // Get nearest neighbor class label and compare it to its true label | ||||
| int index = (int)nn_index; | int index = (int)nn_index; | ||||
| print($"Test {i} Prediction: {np.argmax(Ytr[(NDArray)index])} True Class: {np.argmax(Yte[i] as NDArray)}"); | |||||
| print($"Test {i} Prediction: {np.argmax(Ytr[index])} True Class: {np.argmax(Yte[i])}"); | |||||
| // Calculate accuracy | // Calculate accuracy | ||||
| if (np.argmax(Ytr[(NDArray)index]) == np.argmax(Yte[i] as NDArray)) | |||||
| if ((int)np.argmax(Ytr[index]) == (int)np.argmax(Yte[i])) | |||||
| accuracy += 1f/ Xte.shape[0]; | accuracy += 1f/ Xte.shape[0]; | ||||
| } | } | ||||
| @@ -2,19 +2,18 @@ | |||||
| <PropertyGroup> | <PropertyGroup> | ||||
| <OutputType>Exe</OutputType> | <OutputType>Exe</OutputType> | ||||
| <TargetFramework>netcoreapp2.1</TargetFramework> | |||||
| <TargetFramework>netcoreapp2.2</TargetFramework> | |||||
| <GeneratePackageOnBuild>false</GeneratePackageOnBuild> | <GeneratePackageOnBuild>false</GeneratePackageOnBuild> | ||||
| </PropertyGroup> | </PropertyGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| <PackageReference Include="Colorful.Console" Version="1.2.9" /> | <PackageReference Include="Colorful.Console" Version="1.2.9" /> | ||||
| <PackageReference Include="Newtonsoft.Json" Version="12.0.1" /> | |||||
| <PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> | |||||
| <PackageReference Include="SharpZipLib" Version="1.1.0" /> | <PackageReference Include="SharpZipLib" Version="1.1.0" /> | ||||
| <PackageReference Include="System.Drawing.Common" Version="4.5.1" /> | <PackageReference Include="System.Drawing.Common" Version="4.5.1" /> | ||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| <ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" /> | |||||
| <ProjectReference Include="..\..\src\TensorFlowNET.Core\TensorFlowNET.Core.csproj" /> | <ProjectReference Include="..\..\src\TensorFlowNET.Core\TensorFlowNET.Core.csproj" /> | ||||
| </ItemGroup> | </ItemGroup> | ||||
| @@ -1,7 +1,7 @@ | |||||
| <Project Sdk="Microsoft.NET.Sdk"> | |||||
| <Project Sdk="Microsoft.NET.Sdk"> | |||||
| <PropertyGroup> | <PropertyGroup> | ||||
| <TargetFramework>netcoreapp2.1</TargetFramework> | |||||
| <TargetFramework>netcoreapp2.2</TargetFramework> | |||||
| <IsPackable>false</IsPackable> | <IsPackable>false</IsPackable> | ||||
| </PropertyGroup> | </PropertyGroup> | ||||
| @@ -22,7 +22,6 @@ | |||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| <ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" /> | |||||
| <ProjectReference Include="..\..\src\TensorFlowNET.Core\TensorFlowNET.Core.csproj" /> | <ProjectReference Include="..\..\src\TensorFlowNET.Core\TensorFlowNET.Core.csproj" /> | ||||
| <ProjectReference Include="..\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj" /> | <ProjectReference Include="..\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj" /> | ||||
| </ItemGroup> | </ItemGroup> | ||||