Skip to content

Commit 0cb07ec

Browse files
committed
TextClassification example: use imported graph (in progress)
1 parent 22c0776 commit 0cb07ec

4 files changed

Lines changed: 88 additions & 15 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ public class DataHelpers
1313
private const string TRAIN_PATH = "text_classification/dbpedia_csv/train.csv";
1414
private const string TEST_PATH = "text_classification/dbpedia_csv/test.csv";
1515

16-
public static (int[][], int[], int) build_char_dataset(string step, string model, int document_max_len, int? limit=null)
16+
public static (int[][], int[], int) build_char_dataset(string step, string model, int document_max_len, int? limit = null)
1717
{
18+
if (model != "vd_cnn")
19+
throw new NotImplementedException(model);
1820
string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:’'\"/|_#$%ˆ&*˜‘+=<>()[]{} ";
1921
/*if (step == "train")
2022
df = pd.read_csv(TRAIN_PATH, names =["class", "title", "content"]);*/
@@ -41,8 +43,8 @@ public static (int[][], int[], int) build_char_dataset(string step, string model
4143
x[i][j] = char_dict["<pad>"];
4244
else
4345
x[i][j] = char_dict.ContainsKey(content[j].ToString()) ? char_dict[content[j].ToString()] : char_dict["<unk>"];
44-
}
45-
46+
}
47+
4648
y[i] = int.Parse(parts[0]);
4749
}
4850

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

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Linq;
55
using System.Text;
66
using Tensorflow;
7+
using Tensorflow.Keras.Engine;
8+
using TensorFlowNET.Examples.Text.cnn_models;
79
using TensorFlowNET.Examples.TextClassification;
810
using TensorFlowNET.Examples.Utility;
911

@@ -15,30 +17,76 @@ namespace TensorFlowNET.Examples.CnnTextClassification
1517
public class TextClassificationTrain : Python, IExample
1618
{
1719
public int Priority => 100;
18-
public bool Enabled { get; set; }= false;
20+
public bool Enabled { get; set; } = false;
1921
public string Name => "Text Classification";
2022
public int? DataLimit = null;
2123
public bool ImportGraph { get; set; } = true;
2224

2325
private string dataDir = "text_classification";
2426
private string dataFileName = "dbpedia_csv.tar.gz";
2527

28+
public string model_name = "vd_cnn"; // word_cnn | char_cnn | vd_cnn | word_rnn | att_rnn | rcnn
29+
2630
private const int CHAR_MAX_LEN = 1014;
2731
private const int NUM_CLASS = 2;
32+
protected float loss_value = 0;
2833

2934
public bool Run()
3035
{
3136
PrepareData();
37+
return with(tf.Session(), sess =>
38+
{
39+
if (ImportGraph)
40+
return RunWithImportedGraph(sess);
41+
else
42+
return RunWithBuiltGraph(sess);
43+
});
44+
}
45+
46+
protected virtual bool RunWithImportedGraph(Session sess)
47+
{
48+
var graph = tf.Graph().as_default();
49+
Console.WriteLine("Building dataset...");
50+
var (x, y, alphabet_size) = DataHelpers.build_char_dataset("train", model_name, CHAR_MAX_LEN, DataLimit);
51+
52+
var (train_x, valid_x, train_y, valid_y) = train_test_split(x, y, test_size: 0.15f);
53+
54+
var meta_file = model_name + "_untrained.meta";
55+
tf.train.import_meta_graph(Path.Join("graph", meta_file));
56+
57+
//sess.run(tf.global_variables_initializer());
58+
59+
Tensor is_training = graph.get_operation_by_name("is_training");
60+
Tensor model_x = graph.get_operation_by_name("x");
61+
Tensor model_y = graph.get_operation_by_name("y");
62+
//Tensor loss = graph.get_operation_by_name("loss");
63+
//Tensor accuracy = graph.get_operation_by_name("accuracy");
64+
return false;
65+
}
66+
67+
protected virtual bool RunWithBuiltGraph(Session session)
68+
{
3269
Console.WriteLine("Building dataset...");
33-
var (x, y, alphabet_size) = DataHelpers.build_char_dataset("train", "vdcnn", CHAR_MAX_LEN, DataLimit);
70+
var (x, y, alphabet_size) = DataHelpers.build_char_dataset("train", model_name, CHAR_MAX_LEN, DataLimit);
3471

3572
var (train_x, valid_x, train_y, valid_y) = train_test_split(x, y, test_size: 0.15f);
3673

37-
return with(tf.Session(), sess =>
38-
{
39-
new VdCnn(alphabet_size, CHAR_MAX_LEN, NUM_CLASS);
40-
return false;
41-
});
74+
ITextClassificationModel model = null;
75+
switch (model_name) // word_cnn | char_cnn | vd_cnn | word_rnn | att_rnn | rcnn
76+
{
77+
case "word_cnn":
78+
case "char_cnn":
79+
case "word_rnn":
80+
case "att_rnn":
81+
case "rcnn":
82+
throw new NotImplementedException();
83+
break;
84+
case "vd_cnn":
85+
model=new VdCnn(alphabet_size, CHAR_MAX_LEN, NUM_CLASS);
86+
break;
87+
}
88+
// todo train the model
89+
return false;
4290
}
4391

4492
private (int[][], int[][], int[], int[]) train_test_split(int[][] x, int[] y, float test_size = 0.3f)
@@ -53,7 +101,7 @@ public bool Run()
53101
var train_y = new List<int>();
54102
var valid_y = new List<int>();
55103

56-
for (int i = 0; i< classes; i++)
104+
for (int i = 0; i < classes; i++)
57105
{
58106
for (int j = 0; j < samples; j++)
59107
{
@@ -79,6 +127,14 @@ public void PrepareData()
79127
string url = "https://github.com/le-scientifique/torchDatasets/raw/master/dbpedia_csv.tar.gz";
80128
Web.Download(url, dataDir, dataFileName);
81129
Compress.ExtractTGZ(Path.Join(dataDir, dataFileName), dataDir);
130+
131+
if (ImportGraph)
132+
{
133+
// download graph meta data
134+
var meta_file = model_name + "_untrained.meta";
135+
url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/" + meta_file;
136+
Web.Download(url, "graph", meta_file);
137+
}
82138
}
83139
}
84140
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow;
5+
6+
namespace TensorFlowNET.Examples.Text.cnn_models
7+
{
8+
interface ITextClassificationModel
9+
{
10+
Tensor is_training { get; }
11+
Tensor x { get;}
12+
Tensor y { get; }
13+
}
14+
}

‎test/TensorFlowNET.Examples/Text/cnn_models/VdCnn.cs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
using System.Linq;
44
using System.Text;
55
using Tensorflow;
6+
using TensorFlowNET.Examples.Text.cnn_models;
67

78
namespace TensorFlowNET.Examples.TextClassification
89
{
9-
public class VdCnn : Python
10+
public class VdCnn : Python, ITextClassificationModel
1011
{
1112
private int embedding_size;
1213
private int[] filter_sizes;
@@ -15,9 +16,9 @@ public class VdCnn : Python
1516
private float learning_rate;
1617
private IInitializer cnn_initializer;
1718
private IInitializer fc_initializer;
18-
private Tensor x;
19-
private Tensor y;
20-
private Tensor is_training;
19+
public Tensor x { get; private set; }
20+
public Tensor y { get; private set; }
21+
public Tensor is_training { get; private set; }
2122
private RefVariable global_step;
2223
private RefVariable embeddings;
2324
private Tensor x_emb;

0 commit comments

Comments
 (0)