@@ -37,7 +37,7 @@ Then load the training data
37
37
Then we choose a neural network. We choose ConvNet:
38
38
39
39
>>> import shorttext.classifiers.embed.nnlib.frameworks as fr
40
- >>> kmodel = fr.CNNWordEmbed(len (classdict .keys()))
40
+ >>> kmodel = fr.CNNWordEmbed(len (trainclassdict .keys()))
41
41
42
42
Initialize the classifier:
43
43
@@ -69,18 +69,88 @@ Epoch 10/10
69
69
45/45 [==============================] - 0s - loss: 0.0743
70
70
71
71
Then the model is ready for classification, like:
72
+
72
73
>>> classifier.score(' artificial intelligence' )
73
74
{'mathematics': 0.57749695, 'physics': 0.33749574, 'theology': 0.085007325}
74
75
75
76
Provided Neural Networks
76
77
------------------------
77
78
79
+ There are three neural networks available in this package for the use in
80
+ :class: `shorttext.classifiers.embed.nnlib.VarNNEmbeddedVecClassification.VarNNEmbeddedVecClassifier `,
81
+ and they are available in the module :module: `shorttext.classifiers.embed.nnlib.frameworks `.
82
+
83
+ ConvNet (Convolutional Neural Network)
84
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85
+
86
+ This neural network for supervised learning is using convolutional neural network (ConvNet),
87
+ as demonstrated in Kim's paper.
88
+
89
+ .. image :: images/nnlib_cnn.png
90
+
91
+ The function in the frameworks returns a :class: `keras.models.Sequential `.
92
+
93
+ .. autofunction :: shortext.embed.nnlib.frameworks.CNNWordEmbed
94
+
95
+ The parameter `maxlen ` defines the maximum length of the sentences. If the sentence has less than `maxlen `
96
+ words, then the empty words will be filled with zero vectors.
97
+
98
+ >>> kmodel = fr.CNNWordEmbed(len (trainclassdict.keys()))
99
+
100
+ Double ConvNet
101
+ ^^^^^^^^^^^^^^
102
+
103
+ This neural network is nothing more than two ConvNet layers.
104
+
105
+ .. autofunction :: shortext.embed.nnlib.frameworks.DoubleCNNWordEmbed
106
+
107
+ The parameter `maxlen ` defines the maximum length of the sentences. If the sentence has less than `maxlen `
108
+ words, then the empty words will be filled with zero vectors.
109
+
110
+ >>> kmodel = fr.DoubleCNNWordEmbed(len (trainclassdict.keys()))
111
+
112
+ C-LSTM (Convolutional Long Short-Term Memory)
113
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
114
+
115
+ This neural network for supervised learning is using C-LSTM, according to the paper
116
+ written by Zhou *et. al. * It is a neural network with ConvNet as the first layer,
117
+ and then followed by LSTM (long short-term memory), a type of recurrent neural network (RNN).
118
+
119
+ .. image :: images/nnlib_clstm.png
120
+
121
+ The function in the frameworks returns a :class: `keras.models.Sequential `.
122
+
123
+ .. autofunction :: shorttext.embed.nnlib.frameworks.CLSTMWordEmbed
124
+
125
+ The parameter `maxlen ` defines the maximum length of the sentences. If the sentence has less than `maxlen `
126
+ words, then the empty words will be filled with zero vectors.
127
+
128
+ >>> kmodel = fr.CLSTMWordEmbed(len (trainclassdict.keys()))
129
+
130
+ User-Defined Neural Network
131
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
132
+
133
+ Users can define their own neural network for use in the classifier wrapped by
134
+ :class: `shorttext.classifiers.embed.nnlib.VarNNEmbeddedVecClassification.VarNNEmbeddedVecClassifier `
135
+ as long as the following criteria are met:
136
+
137
+ - the input matrix is :class: `numpy.ndarray `, and of shape `(maxlen, vecsize) `, where
138
+ `maxlen ` is the maximum length of the sentence, and `vecsize ` is the number of dimensions
139
+ of the embedded vectors. The output is a one-dimensional array, of size equal to
140
+ the number of classes provided by the training data. The order of the class labels is assumed
141
+ to be the same as the order of the given training data (stored as a Python dictionary).
78
142
79
143
Reference
80
144
---------
81
145
82
146
Chunting Zhou, Chonglin Sun, Zhiyuan Liu, Francis Lau, "A C-LSTM Neural Network for Text Classification," (arXiv:1511.08630). [`arXiv
83
147
<https://arxiv.org/abs/1511.08630> `_]
84
148
149
+ "CS231n Convolutional Neural Networks for Visual Recognition," Stanford Online Course. [`link
150
+ <http://cs231n.github.io/convolutional-networks/> `_]
151
+
85
152
Yoon Kim, "Convolutional Neural Networks for Sentence Classification," *EMNLP * 2014, 1746-1751 (arXiv:1408.5882). [`arXiv
86
153
<https://arxiv.org/abs/1408.5882> `_]
154
+
155
+ Zackary C. Lipton, John Berkowitz, "A Critical Review of Recurrent Neural Networks for Sequence Learning," arXiv:1506.00019 (2015). [`arXiv
156
+ <https://arxiv.org/abs/1506.00019> `_]
0 commit comments