-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from jdsutton/tests
Add tests
- Loading branch information
Showing
5 changed files
with
250 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
*.pyc | ||
__pycache__ | ||
*.egg-info | ||
build/* | ||
dist/* |
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,43 @@ | ||
''' | ||
This file contains test cases for tflearn | ||
''' | ||
|
||
import tensorflow as tf | ||
import tflearn | ||
import unittest | ||
|
||
class TestActivations(unittest.TestCase): | ||
''' | ||
This class contains test cases for the functions in tflearn/activations.py | ||
''' | ||
PLACES = 4 # Number of places to match when testing floating point values | ||
|
||
def test_linear(self): | ||
f = tflearn.linear | ||
|
||
# Case 1 | ||
x = tf.placeholder(tf.float32, shape=()) | ||
self.assertEqual(f(x), x) | ||
|
||
# Case 2 | ||
x = tf.placeholder(tf.int64, shape=()) | ||
self.assertEqual(f(x), x) | ||
|
||
def test_tanh(self): | ||
f = tflearn.tanh | ||
x = tf.placeholder(tf.float32, shape=()) | ||
|
||
with tf.Session() as sess: | ||
# Case 1 | ||
self.assertEqual(sess.run(f(x), feed_dict={x:0}), 0) | ||
|
||
# Case 2 | ||
self.assertAlmostEqual(sess.run(f(x), feed_dict={x:0.5}), | ||
0.4621, places=TestActivations.PLACES) | ||
|
||
# Case 3 | ||
self.assertAlmostEqual(sess.run(f(x), feed_dict={x:-0.25}), | ||
-0.2449, places=TestActivations.PLACES) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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,42 @@ | ||
import tensorflow as tf | ||
import tflearn | ||
import unittest | ||
import os | ||
|
||
class TestHelpers(unittest.TestCase): | ||
""" | ||
Testing helper functions from tflearn/helpers | ||
""" | ||
|
||
def test_variable(self): | ||
# Bulk Tests | ||
with tf.Graph().as_default(): | ||
W = tflearn.variable(name='W1', shape=[784, 256], | ||
initializer='uniform_scaling', | ||
regularizer='L2') | ||
W = tflearn.variable(name='W2', shape=[784, 256], | ||
initializer='uniform_scaling', | ||
regularizer='L2') | ||
|
||
def test_regularizer(self): | ||
# Bulk Tests | ||
with tf.Graph().as_default(): | ||
x = tf.placeholder("float", [None, 4]) | ||
W = tf.Variable(tf.random_normal([4, 4])) | ||
x = tf.nn.tanh(tf.matmul(x, W)) | ||
tflearn.add_weights_regularizer(W, 'L2', weight_decay=0.001) | ||
|
||
def test_summarizer(self): | ||
# Bulk Tests | ||
with tf.Graph().as_default(): | ||
x = tf.placeholder("float", [None, 4]) | ||
W = tf.Variable(tf.random_normal([4, 4])) | ||
x = tf.nn.tanh(tf.matmul(x, W)) | ||
tf.add_to_collection(tf.GraphKeys.ACTIVATIONS, x) | ||
import tflearn.helpers.summarizer as s | ||
s.summarize_variables([W]) | ||
s.summarize_activations(tf.get_collection(tf.GraphKeys.ACTIVATIONS)) | ||
s.summarize(x, 'histogram', "test_summary") | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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,93 @@ | ||
import tensorflow as tf | ||
import tflearn | ||
import unittest | ||
import os | ||
|
||
class TestLayers(unittest.TestCase): | ||
""" | ||
Testing layers from tflearn/layers | ||
""" | ||
|
||
def test_core_layers(self): | ||
|
||
X = [[0., 0.], [0., 1.], [1., 0.], [1., 1.]] | ||
Y_nand = [[1.], [1.], [1.], [0.]] | ||
Y_or = [[0.], [1.], [1.], [1.]] | ||
|
||
# Graph definition | ||
with tf.Graph().as_default(): | ||
# Building a network with 2 optimizers | ||
g = tflearn.input_data(shape=[None, 2]) | ||
|
||
# Nand operator definition | ||
g_nand = tflearn.fully_connected(g, 32, activation='linear') | ||
g_nand = tflearn.fully_connected(g_nand, 32, activation='linear') | ||
g_nand = tflearn.fully_connected(g_nand, 1, activation='sigmoid') | ||
g_nand = tflearn.regression(g_nand, optimizer='sgd', | ||
learning_rate=2., | ||
loss='binary_crossentropy') | ||
# Or operator definition | ||
g_or = tflearn.fully_connected(g, 32, activation='linear') | ||
g_or = tflearn.fully_connected(g_or, 32, activation='linear') | ||
g_or = tflearn.fully_connected(g_or, 1, activation='sigmoid') | ||
g_or = tflearn.regression(g_or, optimizer='sgd', | ||
learning_rate=2., | ||
loss='binary_crossentropy') | ||
# XOR merging Nand and Or operators | ||
g_xor = tflearn.merge([g_nand, g_or], mode='elemwise_mul') | ||
|
||
# Training | ||
m = tflearn.DNN(g_xor) | ||
m.fit(X, [Y_nand, Y_or], n_epoch=400, snapshot_epoch=False) | ||
|
||
# Testing | ||
self.assertLess(m.predict([[0., 0.]])[0][0], 0.01) | ||
self.assertGreater(m.predict([[0., 1.]])[0][0], 0.9) | ||
self.assertGreater(m.predict([[1., 0.]])[0][0], 0.9) | ||
self.assertLess(m.predict([[1., 1.]])[0][0], 0.01) | ||
|
||
# Bulk Tests | ||
with tf.Graph().as_default(): | ||
net = tflearn.input_data(shape=[None, 2]) | ||
net = tflearn.flatten(net) | ||
net = tflearn.reshape(net, new_shape=[-1]) | ||
net = tflearn.activation(net, 'relu') | ||
net = tflearn.dropout(net, 0.5) | ||
net = tflearn.single_unit(net) | ||
|
||
def test_conv_layers(self): | ||
|
||
X = [[0., 0., 0., 0.], [1., 1., 1., 1.], [0., 0., 1., 0.], [1., 1., 1., 0.]] | ||
Y = [[1., 0.], [0., 1.], [1., 0.], [0., 1.]] | ||
|
||
with tf.Graph().as_default(): | ||
g = tflearn.input_data(shape=[None, 4]) | ||
g = tflearn.reshape(g, new_shape=[-1, 2, 2, 1]) | ||
g = tflearn.conv_2d(g, 4, 2) | ||
g = tflearn.conv_2d(g, 4, 1) | ||
g = tflearn.max_pool_2d(g, 2) | ||
g = tflearn.fully_connected(g, 2, activation='softmax') | ||
g = tflearn.regression(g, optimizer='sgd', learning_rate=1.) | ||
|
||
m = tflearn.DNN(g) | ||
m.fit(X, Y, n_epoch=500, snapshot_epoch=False) | ||
self.assertGreater(m.predict([[1., 0., 0., 0.]])[0][0], 0.9) | ||
|
||
def test_recurrent_layers(self): | ||
|
||
X = [[1, 3, 5, 7], [2, 4, 8, 10], [1, 5, 9, 11], [2, 6, 8, 0]] | ||
Y = [[0., 1.], [1., 0.], [0., 1.], [1., 0.]] | ||
|
||
with tf.Graph().as_default(): | ||
g = tflearn.input_data(shape=[None, 4]) | ||
g = tflearn.embedding(g, input_dim=12, output_dim=4) | ||
g = tflearn.lstm(g, 6) | ||
g = tflearn.fully_connected(g, 2, activation='softmax') | ||
g = tflearn.regression(g, optimizer='sgd', learning_rate=1.) | ||
|
||
m = tflearn.DNN(g) | ||
m.fit(X, Y, n_epoch=500, snapshot_epoch=False) | ||
self.assertGreater(m.predict([[5, 9, 11, 1]])[0][1], 0.9) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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,70 @@ | ||
import tensorflow as tf | ||
import tflearn | ||
import unittest | ||
import os | ||
|
||
class TestModels(unittest.TestCase): | ||
""" | ||
Testing DNN model from tflearn/models/dnn.py | ||
""" | ||
|
||
def test_dnn(self): | ||
|
||
with tf.Graph().as_default(): | ||
X = [3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,7.042,10.791,5.313,7.997,5.654,9.27,3.1] | ||
Y = [1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,2.827,3.465,1.65,2.904,2.42,2.94,1.3] | ||
input = tflearn.input_data(shape=[None]) | ||
linear = tflearn.single_unit(input) | ||
regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square', | ||
metric='R2', learning_rate=0.01) | ||
m = tflearn.DNN(regression) | ||
# Testing fit and predict | ||
m.fit(X, Y, n_epoch=1000, show_metric=True, snapshot_epoch=False) | ||
res = m.predict([3.2])[0] | ||
self.assertGreater(res, 1.3, "DNN test (linear regression) failed! with score: " + str(res) + " expected > 1.3") | ||
self.assertLess(res, 1.8, "DNN test (linear regression) failed! with score: " + str(res) + " expected < 1.8") | ||
|
||
# Testing save method | ||
m.save("test_dnn.tflearn") | ||
self.assertTrue(os.path.exists("test_dnn.tflearn")) | ||
|
||
# Testing load method | ||
m.load("test_dnn.tflearn") | ||
res = m.predict([3.2])[0] | ||
self.assertGreater(res, 1.3, "DNN test (linear regression) failed after loading model! score: " + str(res) + " expected > 1.3") | ||
self.assertLess(res, 1.8, "DNN test (linear regression) failed after loading model! score: " + str(res) + " expected < 1.8") | ||
|
||
def test_sequencegenerator(self): | ||
|
||
with tf.Graph().as_default(): | ||
text = "123456789101234567891012345678910123456789101234567891012345678910" | ||
maxlen = 5 | ||
|
||
X, Y, char_idx = \ | ||
tflearn.data_utils.string_to_semi_redundant_sequences(text, seq_maxlen=maxlen, redun_step=3) | ||
|
||
g = tflearn.input_data(shape=[None, maxlen, len(char_idx)]) | ||
g = tflearn.lstm(g, 32) | ||
g = tflearn.dropout(g, 0.5) | ||
g = tflearn.fully_connected(g, len(char_idx), activation='softmax') | ||
g = tflearn.regression(g, optimizer='adam', loss='categorical_crossentropy', | ||
learning_rate=0.1) | ||
|
||
m = tflearn.SequenceGenerator(g, dictionary=char_idx, | ||
seq_maxlen=maxlen, | ||
clip_gradients=5.0) | ||
m.fit(X, Y, validation_set=0.1, n_epoch=200, snapshot_epoch=False) | ||
res = m.generate(10, temperature=1., seq_seed="12345") | ||
self.assertEqual(res, "123456789101234", "SequenceGenerator test failed! Generated sequence: " + res + " expected '123456789101234'") | ||
|
||
# Testing save method | ||
m.save("test_seqgen.tflearn") | ||
self.assertTrue(os.path.exists("test_seqgen.tflearn")) | ||
|
||
# Testing load method | ||
m.load("test_seqgen.tflearn") | ||
res = m.generate(10, temperature=1., seq_seed="12345") | ||
self.assertEqual(res, "123456789101234", "SequenceGenerator test failed after loading model! Generated sequence: " + res + " expected '123456789101234'") | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |