Browse Source

add tf.convert_to_tensor

tags/v0.9
Oceania2018 6 years ago
parent
commit
dc8a8cf0dd
9 changed files with 55 additions and 31 deletions
  1. +0
    -6
      TensorFlow.NET.sln
  2. +13
    -0
      src/TensorFlowNET.Core/APIs/tf.tensor.cs
  3. +1
    -1
      src/TensorFlowNET.Core/Python.cs
  4. +3
    -7
      src/TensorFlowNET.Core/TensorFlowNET.Core.csproj
  5. +31
    -8
      test/TensorFlowNET.Examples/KMeansClustering.cs
  6. +1
    -1
      test/TensorFlowNET.Examples/LogisticRegression.cs
  7. +2
    -2
      test/TensorFlowNET.Examples/NearestNeighbor.cs
  8. +2
    -3
      test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj
  9. +2
    -3
      test/TensorFlowNET.UnitTest/TensorFlowNET.UnitTest.csproj

+ 0
- 6
TensorFlow.NET.sln View File

@@ -9,8 +9,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Examples", "t
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Core", "src\TensorFlowNET.Core\TensorFlowNET.Core.csproj", "{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{DE97EAD7-B92C-4112-9690-91C40A97179E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
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}.Release|Any CPU.ActiveCfg = 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
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE


+ 13
- 0
src/TensorFlowNET.Core/APIs/tf.tensor.cs View File

@@ -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);
}
}

+ 1
- 1
src/TensorFlowNET.Core/Python.cs View File

@@ -29,7 +29,7 @@ namespace Tensorflow

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


+ 3
- 7
src/TensorFlowNET.Core/TensorFlowNET.Core.csproj View File

@@ -4,10 +4,10 @@
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>TensorFlow.NET</AssemblyName>
<RootNamespace>Tensorflow</RootNamespace>
<Version>0.6.1-alpha</Version>
<Version>0.6.1</Version>
<Authors>Haiping Chen</Authors>
<Company>SciSharp STACK</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Copyright>Apache 2.0</Copyright>
<RepositoryUrl>https://github.com/SciSharp/TensorFlow.NET</RepositoryUrl>
<RepositoryType>git</RepositoryType>
@@ -44,7 +44,7 @@ More math/ linalg APIs.</PackageReleaseNotes>

<ItemGroup>
<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>
@@ -57,8 +57,4 @@ More math/ linalg APIs.</PackageReleaseNotes>
<Folder Include="Keras\Initializers\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
</ItemGroup>

</Project>

+ 31
- 8
test/TensorFlowNET.Examples/KMeansClustering.cs View File

@@ -1,6 +1,8 @@
using NumSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Tensorflow;
using Tensorflow.Clustering;
@@ -26,7 +28,7 @@ namespace TensorFlowNET.Examples

Datasets mnist;
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 num_classes = 10; // The 10 digits
int num_features = 784; // Each image is 28x28 pixels
@@ -63,22 +65,43 @@ namespace TensorFlowNET.Examples

// Training
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));
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
// Count total number of labels per centroid, using the label of each training
// 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;


+ 1
- 1
test/TensorFlowNET.Examples/LogisticRegression.cs View File

@@ -88,7 +88,7 @@ namespace TensorFlowNET.Examples

// Display logs per epoch step
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();
}


+ 2
- 2
test/TensorFlowNET.Examples/NearestNeighbor.cs View File

@@ -51,9 +51,9 @@ namespace TensorFlowNET.Examples
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
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
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];
}



+ 2
- 3
test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj View File

@@ -2,19 +2,18 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp2.2</TargetFramework>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
<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="System.Drawing.Common" Version="4.5.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
<ProjectReference Include="..\..\src\TensorFlowNET.Core\TensorFlowNET.Core.csproj" />
</ItemGroup>



+ 2
- 3
test/TensorFlowNET.UnitTest/TensorFlowNET.UnitTest.csproj View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp2.2</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
@@ -22,7 +22,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
<ProjectReference Include="..\..\src\TensorFlowNET.Core\TensorFlowNET.Core.csproj" />
<ProjectReference Include="..\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj" />
</ItemGroup>


Loading…
Cancel
Save