Skip to content

Commit 93fd34b

Browse files
committed
imdb dataset.
1 parent 7293c32 commit 93fd34b

File tree

9 files changed

+174
-7
lines changed

9 files changed

+174
-7
lines changed

TensorFlow.NET.sln

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Console", "src\T
1313
EndProject
1414
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Keras", "src\TensorFlowNET.Keras\Tensorflow.Keras.csproj", "{49D71826-C03D-4FA7-9BAC-22C1327E65CF}"
1515
EndProject
16-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tensorflow.Text", "src\TensorFlowNET.Text\Tensorflow.Text.csproj", "{1AB8108D-4FFE-4A16-88E7-328EAF686370}"
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Text", "src\TensorFlowNET.Text\Tensorflow.Text.csproj", "{1AB8108D-4FFE-4A16-88E7-328EAF686370}"
17+
EndProject
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Recommenders", "src\TensorFlowNET.Recommenders\Tensorflow.Recommenders.csproj", "{F17AAECB-960A-4E18-A270-BAD776F0E55B}"
1719
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -175,6 +177,30 @@ Global
175177
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Release|x64.Build.0 = Release|Any CPU
176178
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Release|x86.ActiveCfg = Release|Any CPU
177179
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Release|x86.Build.0 = Release|Any CPU
180+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
181+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|Any CPU.Build.0 = Debug|Any CPU
182+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|x64.ActiveCfg = Debug|Any CPU
183+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|x64.Build.0 = Debug|Any CPU
184+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|x86.ActiveCfg = Debug|Any CPU
185+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|x86.Build.0 = Debug|Any CPU
186+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug-Minimal|Any CPU.ActiveCfg = Debug|Any CPU
187+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug-Minimal|Any CPU.Build.0 = Debug|Any CPU
188+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug-Minimal|x64.ActiveCfg = Debug|Any CPU
189+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug-Minimal|x64.Build.0 = Debug|Any CPU
190+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug-Minimal|x86.ActiveCfg = Debug|Any CPU
191+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug-Minimal|x86.Build.0 = Debug|Any CPU
192+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Publish|Any CPU.ActiveCfg = Debug|Any CPU
193+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Publish|Any CPU.Build.0 = Debug|Any CPU
194+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Publish|x64.ActiveCfg = Debug|Any CPU
195+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Publish|x64.Build.0 = Debug|Any CPU
196+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Publish|x86.ActiveCfg = Debug|Any CPU
197+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Publish|x86.Build.0 = Debug|Any CPU
198+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|Any CPU.ActiveCfg = Release|Any CPU
199+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|Any CPU.Build.0 = Release|Any CPU
200+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|x64.ActiveCfg = Release|Any CPU
201+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|x64.Build.0 = Release|Any CPU
202+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|x86.ActiveCfg = Release|Any CPU
203+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|x86.Build.0 = Release|Any CPU
178204
EndGlobalSection
179205
GlobalSection(SolutionProperties) = preSolution
180206
HideSolutionNode = FALSE

src/TensorFlowNET.Console/Tensorflow.Console.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<ProjectReference Include="..\TensorFlowNET.Keras\Tensorflow.Keras.csproj" />
15+
<ProjectReference Include="..\TensorFlowNET.Recommenders\Tensorflow.Recommenders.csproj" />
1616
<ProjectReference Include="..\TensorFlowNET.Text\Tensorflow.Text.csproj" />
1717
</ItemGroup>
1818

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
using Tensorflow.Keras.Utils;
6+
using NumSharp;
7+
using System.Linq;
8+
9+
namespace Tensorflow.Keras.Datasets
10+
{
11+
/// <summary>
12+
/// This is a dataset of 25,000 movies reviews from IMDB, labeled by sentiment
13+
/// (positive/negative). Reviews have been preprocessed, and each review is
14+
/// encoded as a list of word indexes(integers).
15+
/// </summary>
16+
public class Imdb
17+
{
18+
string origin_folder = "https://storage.googleapis.com/tensorflow/tf-keras-datasets/";
19+
string file_name = "imdb.npz";
20+
string dest_folder = "imdb";
21+
22+
/// <summary>
23+
/// Loads the [IMDB dataset](https://ai.stanford.edu/~amaas/data/sentiment/).
24+
/// </summary>
25+
/// <param name="path"></param>
26+
/// <param name="num_words"></param>
27+
/// <param name="skip_top"></param>
28+
/// <param name="maxlen"></param>
29+
/// <param name="seed"></param>
30+
/// <param name="start_char"></param>
31+
/// <param name="oov_char"></param>
32+
/// <param name="index_from"></param>
33+
/// <returns></returns>
34+
public DatasetPass load_data(string path = "imdb.npz",
35+
int num_words = -1,
36+
int skip_top = 0,
37+
int maxlen = -1,
38+
int seed = 113,
39+
int start_char = 1,
40+
int oov_char= 2,
41+
int index_from = 3)
42+
{
43+
var dst = Download();
44+
45+
var lines = File.ReadAllLines(Path.Combine(dst, "imdb_train.txt"));
46+
var x_train_string = new string[lines.Length];
47+
var y_train = np.zeros(new int[] { lines.Length }, NPTypeCode.Int64);
48+
for (int i = 0; i < lines.Length; i++)
49+
{
50+
y_train[i] = long.Parse(lines[i].Substring(0, 1));
51+
x_train_string[i] = lines[i].Substring(2);
52+
}
53+
54+
var x_train = np.array(x_train_string);
55+
56+
File.ReadAllLines(Path.Combine(dst, "imdb_test.txt"));
57+
var x_test_string = new string[lines.Length];
58+
var y_test = np.zeros(new int[] { lines.Length }, NPTypeCode.Int64);
59+
for (int i = 0; i < lines.Length; i++)
60+
{
61+
y_test[i] = long.Parse(lines[i].Substring(0, 1));
62+
x_test_string[i] = lines[i].Substring(2);
63+
}
64+
65+
var x_test = np.array(x_test_string);
66+
67+
return new DatasetPass
68+
{
69+
Train = (x_train, y_train),
70+
Test = (x_test, y_test)
71+
};
72+
}
73+
74+
(NDArray, NDArray) LoadX(byte[] bytes)
75+
{
76+
var y = np.Load_Npz<byte[]>(bytes);
77+
return (y["x_train.npy"], y["x_test.npy"]);
78+
}
79+
80+
(NDArray, NDArray) LoadY(byte[] bytes)
81+
{
82+
var y = np.Load_Npz<long[]>(bytes);
83+
return (y["y_train.npy"], y["y_test.npy"]);
84+
}
85+
86+
string Download()
87+
{
88+
var dst = Path.Combine(Path.GetTempPath(), dest_folder);
89+
Directory.CreateDirectory(dst);
90+
91+
Web.Download(origin_folder + file_name, dst, file_name);
92+
93+
return dst;
94+
// return Path.Combine(dst, file_name);
95+
}
96+
}
97+
}

src/TensorFlowNET.Keras/Datasets/KerasDataset.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ public class KerasDataset
2020
{
2121
public Mnist mnist { get; } = new Mnist();
2222
public Cifar10 cifar10 { get; } = new Cifar10();
23+
public Imdb imdb { get; } = new Imdb();
2324
}
2425
}

src/TensorFlowNET.Keras/Preprocessings/Preprocessing.image_dataset_from_directory.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,20 @@ public IDatasetV2 image_dataset_from_directory(string directory,
5757
dataset = dataset.batch(batch_size);
5858
return dataset;
5959
}
60+
61+
public IDatasetV2 text_dataset_from_directory(string directory,
62+
string labels = "inferred",
63+
string label_mode = "int",
64+
string[] class_names = null,
65+
int batch_size = 32,
66+
bool shuffle = true,
67+
int? seed = null,
68+
float validation_split = 0.2f,
69+
string subset = null)
70+
{
71+
72+
73+
return null;
74+
}
6075
}
6176
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<Version>0.0.1</Version>
6+
<Description>TensorFlow Recommenders is a library for building recommender system models using TensorFlow.</Description>
7+
<PackageLicenseFile>LICENSE</PackageLicenseFile>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<None Include="..\..\LICENSE">
12+
<Pack>True</Pack>
13+
<PackagePath></PackagePath>
14+
</None>
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\TensorFlowNET.Keras\Tensorflow.Keras.csproj" />
19+
</ItemGroup>
20+
21+
</Project>

src/TensorFlowNET.Text/Tensorflow.Text.csproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@
66
<AssemblyName>Tensorflow.Text</AssemblyName>
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
88
<Version>0.0.1</Version>
9+
<PackageLicenseFile>LICENSE</PackageLicenseFile>
910
</PropertyGroup>
1011

1112
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
1213
<DefineConstants>DEBUG;TRACE</DefineConstants>
1314
</PropertyGroup>
1415

1516
<ItemGroup>
16-
<ProjectReference Include="..\TensorFlowNET.Core\Tensorflow.Binding.csproj" />
17+
<None Include="..\..\LICENSE">
18+
<Pack>True</Pack>
19+
<PackagePath></PackagePath>
20+
</None>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\TensorFlowNET.Keras\Tensorflow.Keras.csproj" />
1725
</ItemGroup>
1826

1927
</Project>

src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@
2929

3030
<ItemGroup>
3131
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
32-
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.3.0" />
33-
<PackageReference Include="TensorFlow.NET" Version="0.20.0" />
32+
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.3.1" />
3433
</ItemGroup>
3534

3635
<ItemGroup>
37-
<ProjectReference Include="..\TensorFlowNET.Core\Tensorflow.Binding.csproj" />
36+
<ProjectReference Include="..\TensorFlowNET.Keras\Tensorflow.Keras.csproj" />
3837
</ItemGroup>
3938

4039
</Project>

test/TensorFlowNET.UnitTest/Tensorflow.UnitTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
</ItemGroup>
5656

5757
<ItemGroup>
58-
<ProjectReference Include="..\..\src\TensorFlowNET.Keras\Tensorflow.Keras.csproj" />
58+
<ProjectReference Include="..\..\src\TensorFlowNET.Recommenders\Tensorflow.Recommenders.csproj" />
5959
<ProjectReference Include="..\..\src\TensorFlowNET.Text\Tensorflow.Text.csproj" />
6060
</ItemGroup>
6161

0 commit comments

Comments
 (0)