Browse Source

text classification sample 1.

tags/v0.8.0
haiping008 6 years ago
parent
commit
2446988879
11 changed files with 207 additions and 6 deletions
  1. +12
    -0
      TensorFlow.NET.sln
  2. +45
    -0
      TensorFlowNET.Visualization/Controllers/ValuesController.cs
  3. +24
    -0
      TensorFlowNET.Visualization/Program.cs
  4. +41
    -0
      TensorFlowNET.Visualization/Startup.cs
  5. +13
    -0
      TensorFlowNET.Visualization/TensorFlowNET.Visualization.csproj
  6. +9
    -0
      TensorFlowNET.Visualization/appsettings.Development.json
  7. +8
    -0
      TensorFlowNET.Visualization/appsettings.json
  8. BIN
      data/imdb.zip
  9. +4
    -0
      src/TensorFlowNET.Core/TensorFlowNET.Core.csproj
  10. +7
    -0
      test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj
  11. +44
    -6
      test/TensorFlowNET.Examples/TextClassificationWithMovieReviews.cs

+ 12
- 0
TensorFlow.NET.sln View File

@@ -11,6 +11,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Core", "src\T
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Utility", "src\TensorFlowNET.Utility\TensorFlowNET.Utility.csproj", "{00D9085C-0FC7-453C-A0CC-BAD98F44FEA0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KerasNET.Core", "..\Keras.NET\src\KerasNET.Core\KerasNET.Core.csproj", "{07E37E81-3BCD-4BBB-AE5D-28527F1C7745}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TensorFlowNET.Visualization", "TensorFlowNET.Visualization\TensorFlowNET.Visualization.csproj", "{4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -33,6 +37,14 @@ Global
{00D9085C-0FC7-453C-A0CC-BAD98F44FEA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{00D9085C-0FC7-453C-A0CC-BAD98F44FEA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{00D9085C-0FC7-453C-A0CC-BAD98F44FEA0}.Release|Any CPU.Build.0 = Release|Any CPU
{07E37E81-3BCD-4BBB-AE5D-28527F1C7745}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{07E37E81-3BCD-4BBB-AE5D-28527F1C7745}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07E37E81-3BCD-4BBB-AE5D-28527F1C7745}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07E37E81-3BCD-4BBB-AE5D-28527F1C7745}.Release|Any CPU.Build.0 = Release|Any CPU
{4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE


+ 45
- 0
TensorFlowNET.Visualization/Controllers/ValuesController.cs View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace TensorFlowNET.Visualization.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}

// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "value";
}

// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}

// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}

// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}

+ 24
- 0
TensorFlowNET.Visualization/Program.cs View File

@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace TensorFlowNET.Visualization
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}

+ 41
- 0
TensorFlowNET.Visualization/Startup.cs View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace TensorFlowNET.Visualization
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseMvc();
}
}
}

+ 13
- 0
TensorFlowNET.Visualization/TensorFlowNET.Visualization.csproj View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>

</Project>

+ 9
- 0
TensorFlowNET.Visualization/appsettings.Development.json View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}

+ 8
- 0
TensorFlowNET.Visualization/appsettings.json View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}

BIN
data/imdb.zip View File


+ 4
- 0
src/TensorFlowNET.Core/TensorFlowNET.Core.csproj View File

@@ -51,4 +51,8 @@ Docs: https://tensorflownet.readthedocs.io</Description>
<Content CopyToOutputDirectory="PreserveNewest" Include="./runtimes/win-x64/native/tensorflow.dll" Link="tensorflow.dll" Pack="true" PackagePath="runtimes/win-x64/native/tensorflow.dll" />
</ItemGroup>

<ItemGroup>
<Folder Include="APIs\Keras\" />
</ItemGroup>

</Project>

+ 7
- 0
test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj View File

@@ -11,8 +11,15 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Keras.NET\src\KerasNET.Core\KerasNET.Core.csproj" />
<ProjectReference Include="..\..\src\TensorFlowNET.Core\TensorFlowNET.Core.csproj" />
<ProjectReference Include="..\..\src\TensorFlowNET.Utility\TensorFlowNET.Utility.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\newtonsoft.json\9.0.1\lib\netstandard1.0\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>

</Project>

+ 44
- 6
test/TensorFlowNET.Examples/TextClassificationWithMovieReviews.cs View File

@@ -4,6 +4,9 @@ using System.IO;
using System.Text;
using Tensorflow;
using NumSharp.Core;
using Newtonsoft.Json;
using System.Linq;
using Keras;

namespace TensorFlowNET.Examples
{
@@ -14,10 +17,20 @@ namespace TensorFlowNET.Examples

public void Run()
{
PrepareData();
var((train_data, train_labels), (test_data, test_labels)) = PrepareData();

Console.WriteLine($"Training entries: {train_data.size}, labels: {train_labels.size}");

// A dictionary mapping words to an integer index
var word_index = GetWordIndex();

train_data = keras.preprocessing.sequence.pad_sequences(train_data,
value: word_index["<PAD>"],
padding: "post",
maxlen: 256);
}

private void PrepareData()
private ((NDArray, NDArray), (NDArray, NDArray)) PrepareData()
{
Directory.CreateDirectory(dir);

@@ -32,14 +45,39 @@ namespace TensorFlowNET.Examples
NDArray x_train = File.ReadAllLines(Path.Join(dir, "x_train.txt"));
NDArray labels_train = File.ReadAllLines(Path.Join(dir, "y_train.txt"));
NDArray indices_train = File.ReadAllLines(Path.Join(dir, "indices_train.txt"));
x_train = x_train[indices_train];
labels_train = labels_train[indices_train];
// x_train = x_train[indices_train];
// labels_train = labels_train[indices_train];

NDArray x_test = File.ReadAllLines(Path.Join(dir, "x_test.txt"));
NDArray labels_test = File.ReadAllLines(Path.Join(dir, "y_test.txt"));
NDArray indices_test = File.ReadAllLines(Path.Join(dir, "indices_test.txt"));
x_test = x_test[indices_test];
labels_test = labels_test[indices_test];
// x_test = x_test[indices_test];
// labels_test = labels_test[indices_test];

// not completed
var xs = x_train.hstack(x_test);
var labels = labels_train.hstack(labels_test);

var idx = x_train.size;
var y_train = labels_train;
var y_test = labels_test;

return ((x_train, y_train), (x_test, y_test));
}

private Dictionary<string, int> GetWordIndex()
{
var result = new Dictionary<string, int>();
var json = File.ReadAllText(Path.Join(dir, "imdb_word_index.json"));
var dict = JsonConvert.DeserializeObject<Dictionary<string, int>>(json);

dict.Keys.Select(k => result[k] = dict[k] + 3).ToList();
result["<PAD>"] = 0;
result["<START>"] = 1;
result["<UNK>"] = 2; // unknown
result["<UNUSED>"] = 3;

return result;
}
}
}

Loading…
Cancel
Save