Skip to content

Commit 292233b

Browse files
committed
waiting for scikit-learn's _split.train_test_split
1 parent c944f54 commit 292233b

10 files changed

Lines changed: 108 additions & 94 deletions

File tree

‎TensorFlow.NET.sln‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Visualization
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{E8340C61-12C1-4BEE-A340-403E7C1ACD82}"
1717
EndProject
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "scikit-learn", "..\scikit-learn.net\src\scikit-learn\scikit-learn.csproj", "{199DDAD8-4A6F-43B3-A560-C0393619E304}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +47,10 @@ Global
4547
{E8340C61-12C1-4BEE-A340-403E7C1ACD82}.Debug|Any CPU.Build.0 = Debug|Any CPU
4648
{E8340C61-12C1-4BEE-A340-403E7C1ACD82}.Release|Any CPU.ActiveCfg = Release|Any CPU
4749
{E8340C61-12C1-4BEE-A340-403E7C1ACD82}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{199DDAD8-4A6F-43B3-A560-C0393619E304}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{199DDAD8-4A6F-43B3-A560-C0393619E304}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{199DDAD8-4A6F-43B3-A560-C0393619E304}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{199DDAD8-4A6F-43B3-A560-C0393619E304}.Release|Any CPU.Build.0 = Release|Any CPU
4854
EndGlobalSection
4955
GlobalSection(SolutionProperties) = preSolution
5056
HideSolutionNode = FALSE

‎src/TensorFlowNET.Utility/Web.cs‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Net;
56
using System.Text;
67
using System.Threading;
@@ -10,24 +11,31 @@ namespace TensorFlowNET.Utility
1011
{
1112
public class Web
1213
{
13-
public static bool Download(string url, string file)
14+
public static bool Download(string url, string destDir, string destFileName)
1415
{
15-
if (File.Exists(file))
16+
if (destFileName == null)
17+
destFileName = url.Split(Path.DirectorySeparatorChar).Last();
18+
19+
Directory.CreateDirectory(destDir);
20+
21+
string relativeFilePath = Path.Combine(destDir, destFileName);
22+
23+
if (File.Exists(relativeFilePath))
1624
{
17-
Console.WriteLine($"{file} already exists.");
25+
Console.WriteLine($"{relativeFilePath} already exists.");
1826
return false;
1927
}
2028

2129
var wc = new WebClient();
22-
Console.WriteLine($"Downloading {file}");
23-
var download = Task.Run(() => wc.DownloadFile(url, file));
30+
Console.WriteLine($"Downloading {relativeFilePath}");
31+
var download = Task.Run(() => wc.DownloadFile(url, relativeFilePath));
2432
while (!download.IsCompleted)
2533
{
2634
Thread.Sleep(1000);
2735
Console.Write(".");
2836
}
2937
Console.WriteLine("");
30-
Console.WriteLine($"Downloaded {file}");
38+
Console.WriteLine($"Downloaded {relativeFilePath}");
3139

3240
return true;
3341
}

‎test/TensorFlowNET.Examples/CnnTextClassification/CnnTextTrain.cs‎

Lines changed: 0 additions & 58 deletions
This file was deleted.

‎test/TensorFlowNET.Examples/CnnTextClassification/TextCNN.cs‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎test/TensorFlowNET.Examples/ImageRecognition.cs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,14 @@ private void PrepareData()
8585
// get model file
8686
string url = "https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip";
8787

88-
string zipFile = Path.Join(dir, "inception5h.zip");
89-
Utility.Web.Download(url, zipFile);
88+
Utility.Web.Download(url, dir, "inception5h.zip");
9089

91-
Utility.Compress.UnZip(zipFile, dir);
90+
Utility.Compress.UnZip(Path.Join(dir, "inception5h.zip"), dir);
9291

9392
// download sample picture
94-
string pic = Path.Join(dir, "img", "grace_hopper.jpg");
9593
Directory.CreateDirectory(Path.Join(dir, "img"));
96-
Utility.Web.Download($"https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/label_image/data/grace_hopper.jpg", pic);
94+
url = $"https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/label_image/data/grace_hopper.jpg";
95+
Utility.Web.Download(url, Path.Join(dir, "img"), "grace_hopper.jpg");
9796
}
9897
}
9998
}

‎test/TensorFlowNET.Examples/InceptionArchGoogLeNet.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ private void PrepareData()
9090
// get model file
9191
string url = "https://storage.googleapis.com/download.tensorflow.org/models/inception_v3_2016_08_28_frozen.pb.tar.gz";
9292

93-
string zipFile = Path.Join(dir, $"{pbFile}.tar.gz");
94-
Utility.Web.Download(url, zipFile);
93+
Utility.Web.Download(url, dir, $"{pbFile}.tar.gz");
9594

96-
Utility.Compress.ExtractTGZ(zipFile, dir);
95+
Utility.Compress.ExtractTGZ(Path.Join(dir, $"{pbFile}.tar.gz"), dir);
9796

9897
// download sample picture
9998
string pic = "grace_hopper.jpg";
100-
Utility.Web.Download($"https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/label_image/data/{pic}", Path.Join(dir, pic));
99+
url = $"https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/label_image/data/{pic}";
100+
Utility.Web.Download(url, dir, pic);
101101
}
102102
}
103103
}

‎test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<ItemGroup>
1414
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
15+
<ProjectReference Include="..\..\..\scikit-learn.net\src\scikit-learn\scikit-learn.csproj" />
1516
<ProjectReference Include="..\..\src\TensorFlowNET.Core\TensorFlowNET.Core.csproj" />
1617
<ProjectReference Include="..\..\src\TensorFlowNET.Utility\TensorFlowNET.Utility.csproj" />
1718
</ItemGroup>

test/TensorFlowNET.Examples/CnnTextClassification/DataHelpers.cs renamed to test/TensorFlowNET.Examples/TextClassification/DataHelpers.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,44 @@ namespace TensorFlowNET.Examples.CnnTextClassification
1010
{
1111
public class DataHelpers
1212
{
13+
private const string TRAIN_PATH = "text_classification/dbpedia_csv/train.csv";
14+
private const string TEST_PATH = "text_classification/dbpedia_csv/test.csv";
15+
16+
public static (int[][], int[], int) build_char_dataset(string step, string model, int document_max_len)
17+
{
18+
string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:’'\"/|_#$%ˆ&*˜‘+=<>()[]{} ";
19+
/*if (step == "train")
20+
df = pd.read_csv(TRAIN_PATH, names =["class", "title", "content"]);*/
21+
var char_dict = new Dictionary<string, int>();
22+
char_dict["<pad>"] = 0;
23+
char_dict["<unk>"] = 1;
24+
foreach (char c in alphabet)
25+
char_dict[c.ToString()] = char_dict.Count;
26+
27+
var contents = File.ReadAllLines(TRAIN_PATH);
28+
29+
var x = new int[contents.Length][];
30+
var y = new int[contents.Length];
31+
for (int i = 0; i < contents.Length; i++)
32+
{
33+
string[] parts = contents[i].ToLower().Split(",\"").ToArray();
34+
string content = parts[2];
35+
content = content.Substring(0, content.Length - 1);
36+
x[i] = new int[document_max_len];
37+
for (int j = 0; j < document_max_len; j++)
38+
{
39+
if (j >= content.Length)
40+
x[i][j] = char_dict["<pad>"];
41+
else
42+
x[i][j] = char_dict.ContainsKey(content[j].ToString()) ? char_dict[content[j].ToString()] : char_dict["<unk>"];
43+
}
44+
45+
y[i] = int.Parse(parts[0]);
46+
}
47+
48+
return (x, y, alphabet.Length + 2);
49+
}
50+
1351
/// <summary>
1452
/// Loads MR polarity data from files, splits the data into words and generates labels.
1553
/// Returns split sentences and labels.
@@ -20,8 +58,8 @@ public class DataHelpers
2058
public static (string[], NDArray) load_data_and_labels(string positive_data_file, string negative_data_file)
2159
{
2260
Directory.CreateDirectory("CnnTextClassification");
23-
Utility.Web.Download(positive_data_file, "CnnTextClassification/rt-polarity.pos");
24-
Utility.Web.Download(negative_data_file, "CnnTextClassification/rt-polarity.neg");
61+
Utility.Web.Download(positive_data_file, "CnnTextClassification", "rt -polarity.pos");
62+
Utility.Web.Download(negative_data_file, "CnnTextClassification", "rt-polarity.neg");
2563

2664
// Load data from files
2765
var positive_examples = File.ReadAllLines("CnnTextClassification/rt-polarity.pos")
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using NumSharp.Core;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Text;
7+
using Tensorflow;
8+
using TensorFlowNET.Utility;
9+
10+
namespace TensorFlowNET.Examples.CnnTextClassification
11+
{
12+
/// <summary>
13+
/// https://github.com/dongjun-Lee/text-classification-models-tf
14+
/// </summary>
15+
public class TextClassificationTrain : Python, IExample
16+
{
17+
private string dataDir = "text_classification";
18+
private string dataFileName = "dbpedia_csv.tar.gz";
19+
20+
private const int CHAR_MAX_LEN = 1014;
21+
22+
public void Run()
23+
{
24+
download_dbpedia();
25+
Console.WriteLine("Building dataset...");
26+
var (x, y, alphabet_size) = DataHelpers.build_char_dataset("train", "vdcnn", CHAR_MAX_LEN);
27+
var (train_x, valid_x, train_y, valid_y) = train_test_split(x, y, test_size: 0.15);
28+
}
29+
30+
public void download_dbpedia()
31+
{
32+
string url = "https://github.com/le-scientifique/torchDatasets/raw/master/dbpedia_csv.tar.gz";
33+
Web.Download(url, dataDir, dataFileName);
34+
Compress.ExtractTGZ(Path.Join(dataDir, dataFileName), dataDir);
35+
}
36+
}
37+
}

‎test/TensorFlowNET.Examples/TextClassificationWithMovieReviews.cs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ public void Run()
4646
// get model file
4747
string url = $"https://github.com/SciSharp/TensorFlow.NET/raw/master/data/{dataFile}";
4848

49-
string zipFile = Path.Join(dir, $"imdb.zip");
50-
Utility.Web.Download(url, zipFile);
51-
Utility.Compress.UnZip(zipFile, dir);
49+
Utility.Web.Download(url, dir, "imdb.zip");
50+
Utility.Compress.UnZip(Path.Join(dir, $"imdb.zip"), dir);
5251

5352
// prepare training dataset
5453
var x_train = ReadData(Path.Join(dir, "x_train.txt"));

0 commit comments

Comments
 (0)