Skip to content

Commit

Permalink
documentation updated
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhky committed Oct 27, 2017
1 parent f89c333 commit 4501e9e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 61 deletions.
66 changes: 9 additions & 57 deletions docs/tutorial_nnlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Then we choose a neural network. We choose ConvNet:

Initialize the classifier:

>>> classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(wvmodel, vecsize=300)
>>> classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(wvmodel)

Then train the classifier:

Expand Down Expand Up @@ -99,11 +99,11 @@ Then load the training data

Then we choose a neural network. We choose ConvNet and set `with_gensim` as `True`:

>>> kmodel = shorttext.classifiers.frameworks.CNNWordEmbed(wvmodel=wvmodel, nb_labels=len(trainclassdict.keys()), vecsize=100, with_gensim=True)
>>> kmodel = shorttext.classifiers.frameworks.CNNWordEmbed(wvmodel=wvmodel, nb_labels=len(trainclassdict.keys()) with_gensim=True)

Initialize the classifier and set `with_gensim` as `True`:

>>> classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(wvmodel, with_gensim=True, vecsize=100)
>>> classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(wvmodel, with_gensim=True)

Then train the classifier:

Expand Down Expand Up @@ -151,62 +151,30 @@ as demonstrated in Kim's paper.

The function in the frameworks returns a :class:`keras.models.Sequential` or :class:`keras.models.Model`. Its input parameters are:

`CNNWordEmbed(nb_labels, wvmodel=None, nb_filters=1200, n_gram=2, maxlen=15, vecsize=100, cnn_dropout=0.0, final_activation='softmax', dense_wl2reg=0.0, dense_bl2reg=0.0, optimizer='adam', with_gensim=False)`

* nb_labels (int) – number of class labels
* wvmodel (gensim.models.keyedvectors.KeyedVectors) – pre-trained Gensim word2vec model
* nb_filters (int) – number of filters (Default: 1200)
* n_gram (int) – n-gram, or window size of CNN/ConvNet (Default: 2)
* maxlen (int) – maximum number of words in a sentence (Default: 15)
* vecsize (int) – length of the embedded vectors in the model (Default: 100)
* cnn_dropout (float) – dropout rate for CNN/ConvNet (Default: 0.0)
* final_activation (str) – activation function. Options: softplus, softsign, relu, tanh, sigmoid, hard_sigmoid, linear. (Default: ‘softmax’)
* dense_wl2reg (float) – L2 regularization coefficient (Default: 0.0)
* dense_bl2reg (float) – L2 regularization coefficient for bias (Default: 0.0)
* optimizer (str) – optimizer for gradient descent. Options: sgd, rmsprop, adagrad, adadelta, adam, adamax, nadam. (Default: adam)
* with_gensim (bool) – boolean variable to indicate if the word-embeddings being used derived from a Gensim’s Word2Vec model. (Default: True)

The parameter `maxlen` defines the maximum length of the sentences. If the sentence has less than `maxlen`
words, then the empty words will be filled with zero vectors.

>>> kmodel = fr.CNNWordEmbed(len(trainclassdict.keys()))
>>> kmodel = fr.CNNWordEmbed(len(trainclassdict.keys()), vecsize=wvmodel.vector_size)

Or if you want to include word-embedding layer, do this: (`shorttext` >= 0.4.0)

>>> wvmodel = shorttext.utils.load_word2vec_model('/path/to/gensim/w2vmodel')
>>> kmodel = fr.CNNWordEmbed(wvmodel=wvmodel, nb_labels=len(trainclassdict.keys()), vecsize=100, with_gensim=True)
>>> kmodel = fr.CNNWordEmbed(wvmodel=wvmodel, nb_labels=len(trainclassdict.keys()), vecsize=wvmodel.vector_size, with_gensim=True)

Double ConvNet
^^^^^^^^^^^^^^

This neural network is nothing more than two ConvNet layers. The function in the frameworks returns a :class:`keras.models.Sequential` or :class:`keras.models.Model`. Its input parameters are:

`DoubleCNNWordEmbed(nb_labels, wvmodel=None, nb_filters_1=1200, nb_filters_2=600, n_gram=2, filter_length_2=10, maxlen=15, vecsize=100, cnn_dropout_1=0.0, cnn_dropout_2=0.0, final_activation='softmax', dense_wl2reg=0.0, dense_bl2reg=0.0, optimizer='adam', with_gensim=False)`

* nb_labels (int) – number of class labels
* wvmodel (gensim.models.keyedvectors.KeyedVectors) – pre-trained Gensim word2vec model
* nb_filters_1 (int) – number of filters for the first CNN/ConvNet layer (Default: 1200)
* nb_filters_2 (int) – number of filters for the second CNN/ConvNet layer (Default: 600)
* n_gram (int) – n-gram, or window size of first CNN/ConvNet (Default: 2)
* filter_length_2 (int) – window size for second CNN/ConvNet layer (Default: 10)
* maxlen (int) – maximum number of words in a sentence (Default: 15)
* vecsize (int) – length of the embedded vectors in the model (Default: 100)
* cnn_dropout_1 (float) – dropout rate for the first CNN/ConvNet layer (Default: 0.0)
* cnn_dropout_2 (float) – dropout rate for the second CNN/ConvNet layer (Default: 0.0)
* final_activation (str) – activation function. Options: softplus, softsign, relu, tanh, sigmoid, hard_sigmoid, linear. (Default: ‘softmax’)
* dense_wl2reg (float) – L2 regularization coefficient (Default: 0.0)
* dense_bl2reg (float) – L2 regularization coefficient for bias (Default: 0.0)
* optimizer (str) – optimizer for gradient descent. Options: sgd, rmsprop, adagrad, adadelta, adam, adamax, nadam. (Default: adam)

The parameter `maxlen` defines the maximum length of the sentences. If the sentence has less than `maxlen`
words, then the empty words will be filled with zero vectors.

>>> kmodel = fr.DoubleCNNWordEmbed(len(trainclassdict.keys()))
>>> kmodel = fr.DoubleCNNWordEmbed(len(trainclassdict.keys()), vecsize=wvmodel.vector_size)

Or if you want to include word-embedding layer, do this: (`shorttext` >= 0.4.0)

>>> wvmodel = shorttext.utils.load_word2vec_model('/path/to/gensim/w2vmodel')
>>> kmodel = fr.DoubleCNNWordEmbed(wvmodel=wvmodel, nb_labels=len(trainclassdict.keys()), vecsize=100, with_gensim=True)
>>> kmodel = fr.DoubleCNNWordEmbed(wvmodel=wvmodel, nb_labels=len(trainclassdict.keys()), vecsize=wvmodel.vector_size, with_gensim=True)

C-LSTM (Convolutional Long Short-Term Memory)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -219,31 +187,15 @@ and then followed by LSTM (long short-term memory), a type of recurrent neural n

The function in the frameworks returns a :class:`keras.models.Sequential` or :class:`keras.models.Model`.

`CLSTMWordEmbed(nb_labels, wvmodel=None, nb_filters=1200, n_gram=2, maxlen=15, vecsize=100, cnn_dropout=0.0, nb_rnnoutdim=1200, rnn_dropout=0.2, final_activation='softmax', dense_wl2reg=0.0, dense_bl2reg=0.0, optimizer='adam', with_gensim=False)`

* nb_labels (int) – number of class labels
* wvmodel (gensim.models.keyedvectors.KeyedVectors) – pre-trained Gensim word2vec model
* nb_filters (int) – number of filters (Default: 1200)
* n_gram (int) – n-gram, or window size of CNN/ConvNet (Default: 2)
* maxlen (int) – maximum number of words in a sentence (Default: 15)
* vecsize (int) – length of the embedded vectors in the model (Default: 100)
* cnn_dropout (float) – dropout rate for CNN/ConvNet (Default: 0.0)
* nb_rnnoutdim (int) – output dimension for the LSTM networks (Default: 1200)
* rnn_dropout (float) – dropout rate for LSTM (Default: 0.2)
* final_activation (str) – activation function. Options: softplus, softsign, relu, tanh, sigmoid, hard_sigmoid, linear. (Default: ‘softmax’)
* dense_wl2reg (float) – L2 regularization coefficient (Default: 0.0)
* dense_bl2reg (float) – L2 regularization coefficient for bias (Default: 0.0)
* optimizer (str) – optimizer for gradient descent. Options: sgd, rmsprop, adagrad, adadelta, adam, adamax, nadam. (Default: adam)

The parameter `maxlen` defines the maximum length of the sentences. If the sentence has less than `maxlen`
words, then the empty words will be filled with zero vectors.

>>> kmodel = fr.CLSTMWordEmbed(len(trainclassdict.keys()))
>>> kmodel = fr.CLSTMWordEmbed(len(trainclassdict.keys()), vecsize=wvmodel.vector_size)

Or if you want to include word-embedding layer, do this: (`shorttext` >= 0.4.0)

>>> wvmodel = shorttext.utils.load_word2vec_model('/path/to/gensim/w2vmodel')
>>> kmodel = fr.CLSTMWordEmbed(wvmodel=wvmodel, nb_labels=len(trainclassdict.keys()), vecsize=100, with_gensim=True)
>>> kmodel = fr.CLSTMWordEmbed(wvmodel=wvmodel, nb_labels=len(trainclassdict.keys()), vecsize=wvmodel.vector_size, with_gensim=True)

User-Defined Neural Network
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial_sumvec.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Then we load a set of data:

Then initialize the classifier:

>>> classifier = shorttext.classifiers.SumEmbeddedVecClassifier(wvmodel, vecsize=300) # for Google model, the vector size is 300 (default: 100)
>>> classifier = shorttext.classifiers.SumEmbeddedVecClassifier(wvmodel) # for Google model, the vector size is 300 (default: 100)
>>> classifier.train(nihtraindata)

This classifier takes relatively little time to train compared with others
Expand Down
5 changes: 3 additions & 2 deletions docs/tutorial_topic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ A list include:
shorttext.classifiers.load_autoencoder_topic -> shorttext.generators.load_autoencoder_topicmodel


For backward compatibility, developers can still call the topic models as if there were no such changes,
although they are advised to make this change.
Before release 0.5.6, for backward compatibility, developers can still call the topic models as if there were no such changes,
although they are advised to make this change. However, *effective release 0.5.7, this backward compatibility is no longer
available.*

Classification Using Cosine Similarity
--------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion docs/tutorial_wordembed.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ in the documentation of `gensim`: `Converting GloVe to Word2Vec
FastText
--------

FastText is a similar word-embedding model from Facebook.
FastText is a similar word-embedding model from Facebook. You can download pre-trained models here:

`Pre-trained word vectors
<https://github.com/facebookresearch/fastText/blob/master/pretrained-vectors.md>`_


Links
-----
Expand Down

0 comments on commit 4501e9e

Please sign in to comment.