Skip to content

Commit f4d25c9

Browse files
committed
TextClassification: supported training against dbpedia subset
1 parent b5d8165 commit f4d25c9

2 files changed

Lines changed: 26 additions & 11 deletions

File tree

‎test/TensorFlowNET.Examples/TextProcess/DataHelpers.cs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ namespace TensorFlowNET.Examples
1111
{
1212
public class DataHelpers
1313
{
14-
private const string TRAIN_PATH = "text_classification/dbpedia_csv/train.csv";
15-
private const string TEST_PATH = "text_classification/dbpedia_csv/test.csv";
1614

17-
public static (int[][], int[], int) build_char_dataset(string step, string model, int document_max_len, int? limit = null)
15+
public static (int[][], int[], int) build_char_dataset(string path, string model, int document_max_len, int? limit = null, bool shuffle=true)
1816
{
1917
if (model != "vd_cnn")
2018
throw new NotImplementedException(model);
@@ -26,8 +24,10 @@ public static (int[][], int[], int) build_char_dataset(string step, string model
2624
char_dict["<unk>"] = 1;
2725
foreach (char c in alphabet)
2826
char_dict[c.ToString()] = char_dict.Count;
29-
var contents = new Random(17).Shuffle( File.ReadAllLines(TRAIN_PATH));
30-
//File.WriteAllLines("text_classification/dbpedia_csv/train_6400.csv", contents.Take(6400));
27+
var contents = File.ReadAllLines(path);
28+
if (shuffle)
29+
new Random(17).Shuffle(contents);
30+
//File.WriteAllLines("text_classification/dbpedia_csv/train_6400.csv", contents.Take(6400));
3131
var size = limit == null ? contents.Length : limit.Value;
3232

3333
var x = new int[size][];

‎test/TensorFlowNET.Examples/TextProcess/TextClassificationTrain.cs‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,20 @@ public class TextClassificationTrain : IExample
2525
public string Name => "Text Classification";
2626
public int? DataLimit = null;
2727
public bool ImportGraph { get; set; } = true;
28+
public bool UseSubset = true; // <----- set this true to use a limited subset of dbpedia
2829

2930
private string dataDir = "text_classification";
3031
private string dataFileName = "dbpedia_csv.tar.gz";
3132

3233
public string model_name = "vd_cnn"; // word_cnn | char_cnn | vd_cnn | word_rnn | att_rnn | rcnn
3334

35+
private const string TRAIN_PATH = "text_classification/dbpedia_csv/train.csv";
36+
private const string SUBSET_PATH = "text_classification/dbpedia_csv/dbpedia_6400.csv";
37+
private const string TEST_PATH = "text_classification/dbpedia_csv/test.csv";
38+
3439
private const int CHAR_MAX_LEN = 1014;
3540
private const int WORD_MAX_LEN = 1014;
36-
private const int NUM_CLASS = 2;
41+
private const int NUM_CLASS = 14;
3742
private const int BATCH_SIZE = 64;
3843
private const int NUM_EPOCHS = 10;
3944
protected float loss_value = 0;
@@ -55,7 +60,8 @@ protected virtual bool RunWithImportedGraph(Session sess, Graph graph)
5560
{
5661
var stopwatch = Stopwatch.StartNew();
5762
Console.WriteLine("Building dataset...");
58-
var (x, y, alphabet_size) = DataHelpers.build_char_dataset("train", model_name, CHAR_MAX_LEN, DataLimit = null);
63+
var path = UseSubset ? SUBSET_PATH : TRAIN_PATH;
64+
var (x, y, alphabet_size) = DataHelpers.build_char_dataset(path, model_name, CHAR_MAX_LEN, DataLimit = null, shuffle:!UseSubset);
5965
Console.WriteLine("\tDONE ");
6066

6167
var (train_x, valid_x, train_y, valid_y) = train_test_split(x, y, test_size: 0.15f);
@@ -249,9 +255,18 @@ private static void FillWithShuffledLabels(int[][] x, int[] y, int[][] shuffled_
249255

250256
public void PrepareData()
251257
{
252-
string url = "https://github.com/le-scientifique/torchDatasets/raw/master/dbpedia_csv.tar.gz";
253-
Web.Download(url, dataDir, dataFileName);
254-
Compress.ExtractTGZ(Path.Join(dataDir, dataFileName), dataDir);
258+
if (UseSubset)
259+
{
260+
var url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/data/dbpedia_subset.zip";
261+
Web.Download(url, dataDir, "dbpedia_subset.zip");
262+
Compress.UnZip(Path.Combine(dataDir, "dbpedia_subset.zip"), Path.Combine(dataDir, "dbpedia_csv"));
263+
}
264+
else
265+
{
266+
string url = "https://github.com/le-scientifique/torchDatasets/raw/master/dbpedia_csv.tar.gz";
267+
Web.Download(url, dataDir, dataFileName);
268+
Compress.ExtractTGZ(Path.Join(dataDir, dataFileName), dataDir);
269+
}
255270

256271
if (ImportGraph)
257272
{
@@ -264,7 +279,7 @@ public void PrepareData()
264279
Console.WriteLine("Discarding cached file: " + meta_path);
265280
File.Delete(meta_path);
266281
}
267-
url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/" + meta_file;
282+
var url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/" + meta_file;
268283
Web.Download(url, "graph", meta_file);
269284
}
270285
}

0 commit comments

Comments
 (0)