-
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.
- Loading branch information
Showing
15 changed files
with
371 additions
and
271 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: UTF-8 -*- | ||
|
||
""" | ||
This example show you how to train an LSTM for text generation. | ||
""" | ||
import os | ||
import yadll | ||
import logging | ||
|
||
logging.basicConfig(level=logging.DEBUG, format='%(message)s') | ||
|
||
# load the data | ||
datafile = 'nietzsche.txt' | ||
if not os.path.isfile(datafile): | ||
import urllib | ||
origin = 'https://s3.amazonaws.com/text-datasets/nietzsche.txt' | ||
print 'Downloading data from %s' % origin | ||
urllib.urlretrieve(origin, datafile) | ||
data = yadll.data.Data(datafile) | ||
|
||
# create the model | ||
model = yadll.model.Model(name='lstm', data=data) | ||
|
||
# Hyperparameters | ||
hp = yadll.hyperparameters.Hyperparameters() | ||
hp('batch_size', 128) | ||
hp('n_epochs', 1000) | ||
hp('learning_rate', 0.9) | ||
hp('momentum', 0.5) | ||
hp('l1_reg', 0.00) | ||
hp('l2_reg', 0.0000) | ||
hp('patience', 10000) | ||
|
||
# add the hyperparameters to the model | ||
model.hp = hp | ||
|
||
# Create connected layers | ||
# Input layer | ||
l_in = yadll.layers.InputLayer(input_shape=(hp.batch_size, 28 * 28), name='Input') | ||
# Dropout Layer 1 | ||
l_dro1 = yadll.layers.Dropout(incoming=l_in, corruption_level=0.4, name='Dropout 1') | ||
# Dense Layer 1 | ||
l_hid1 = yadll.layers.DenseLayer(incoming=l_dro1, n_units=100, W=yadll.init.glorot_uniform, | ||
l1=hp.l1_reg, l2=hp.l2_reg, activation=yadll.activations.relu, | ||
name='Hidden layer 1') | ||
# Dropout Layer 2 | ||
l_dro2 = yadll.layers.Dropout(incoming=l_hid1, corruption_level=0.2, name='Dropout 2') | ||
# Dense Layer 2 | ||
l_hid2 = yadll.layers.DenseLayer(incoming=l_dro2, n_units=100, W=yadll.init.glorot_uniform, | ||
l1=hp.l1_reg, l2=hp.l2_reg, activation=yadll.activations.relu, | ||
name='Hidden layer 2') | ||
# Logistic regression Layer | ||
l_out = yadll.layers.LogisticRegression(incoming=l_hid2, n_class=10, l1=hp.l1_reg, | ||
l2=hp.l2_reg, name='Logistic regression') | ||
|
||
# Create network and add layers | ||
net = yadll.network.Network('2 layers mlp with dropout') | ||
net.add(l_in) | ||
net.add(l_dro1) | ||
net.add(l_hid1) | ||
net.add(l_dro2) | ||
net.add(l_hid2) | ||
net.add(l_out) | ||
|
||
# add the network to the model | ||
model.network = net | ||
|
||
# updates method | ||
model.updates = yadll.updates.newton | ||
|
||
# train the model and save it to file at each best | ||
model.train() | ||
|
||
# saving network paramters | ||
net.save_params('net_params.yp') | ||
|
||
# make prediction | ||
# We can test it on some examples from test | ||
test_set_x = data.test_set_x.get_value() | ||
test_set_y = data.test_set_y.eval() | ||
|
||
predicted_values = model.predict(test_set_x[:30]) | ||
|
||
print ("Model 1, predicted values for the first 30 examples in test set:") | ||
print predicted_values | ||
print test_set_y[:30] |
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.