44using System . Linq ;
55using System . Text ;
66using Tensorflow ;
7+ using Tensorflow . Keras . Engine ;
8+ using TensorFlowNET . Examples . Text . cnn_models ;
79using TensorFlowNET . Examples . TextClassification ;
810using 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}
0 commit comments