forked from tflearn/tflearn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Refactoring of all RNNs models - Delete dynamic_rnn (dynamic computation directly optional inside any rnn layer) - Custom RNN cells available (with deeper customization)
- Loading branch information
1 parent
48d743c
commit 4acd614
Showing
11 changed files
with
471 additions
and
1,090 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Simple example using a Dynamic RNN (LSTM) to classify IMDB sentiment dataset. | ||
Dynamic computation are performed over sequences with variable length. | ||
References: | ||
- Long Short Term Memory, Sepp Hochreiter & Jurgen Schmidhuber, Neural | ||
Computation 9(8): 1735-1780, 1997. | ||
- Andrew L. Maas, Raymond E. Daly, Peter T. Pham, Dan Huang, Andrew Y. Ng, | ||
and Christopher Potts. (2011). Learning Word Vectors for Sentiment | ||
Analysis. The 49th Annual Meeting of the Association for Computational | ||
Linguistics (ACL 2011). | ||
Links: | ||
- http://deeplearning.cs.cmu.edu/pdfs/Hochreiter97_lstm.pdf | ||
- http://ai.stanford.edu/~amaas/data/sentiment/ | ||
""" | ||
from __future__ import division, print_function, absolute_import | ||
|
||
import tflearn | ||
from tflearn.data_utils import to_categorical, pad_sequences | ||
from tflearn.datasets import imdb | ||
|
||
# IMDB Dataset loading | ||
train, test, _ = imdb.load_data(path='imdb.pkl', n_words=10000, | ||
valid_portion=0.1) | ||
trainX, trainY = train | ||
testX, testY = test | ||
|
||
# Data preprocessing | ||
# NOTE: Padding is required for dimension consistency. This will pad sequences | ||
# with 0 at the end, until it reaches the max sequence length. 0 is used as a | ||
# masking value by dynamic RNNs in TFLearn; a sequence length will be | ||
# retrieved by counting non zero elements in a sequence. Then dynamic RNN step | ||
# computation is performed according to that length. | ||
trainX = pad_sequences(trainX, maxlen=100, value=0.) | ||
testX = pad_sequences(testX, maxlen=100, value=0.) | ||
# Converting labels to binary vectors | ||
trainY = to_categorical(trainY, nb_classes=2) | ||
testY = to_categorical(testY, nb_classes=2) | ||
|
||
# Network building | ||
net = tflearn.input_data([None, 100]) | ||
# Masking is not required for embedding, sequence length is computed prior to | ||
# the embedding op and assigned as 'seq_length' attribute to the returned Tensor. | ||
net = tflearn.embedding(net, input_dim=10000, output_dim=128) | ||
net = tflearn.lstm(net, 128, dropout=0.8, dynamic=True) | ||
net = tflearn.fully_connected(net, 2, activation='softmax') | ||
net = tflearn.regression(net, optimizer='adam', learning_rate=0.001, | ||
loss='categorical_crossentropy') | ||
|
||
# Training | ||
model = tflearn.DNN(net, tensorboard_verbose=0) | ||
model.fit(trainX, trainY, validation_set=(testX, testY), show_metric=True, | ||
batch_size=32) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.