-
Notifications
You must be signed in to change notification settings - Fork 27
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 #12 from chummm/master
Modify test_activations, remove whitespace, and begin Python3 support
- Loading branch information
Showing
27 changed files
with
3,379 additions
and
3,349 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 |
---|---|---|
@@ -1,76 +1,121 @@ | ||
import pytest | ||
""" | ||
test_activations.py - Unit tests for YANN core activation functions | ||
""" | ||
|
||
import unittest | ||
import numpy as np | ||
import theano | ||
import theano.tensor as T | ||
import yann.core.activations as A | ||
|
||
class TestActivations: | ||
@classmethod | ||
def setup_class(self): | ||
self.theano_input = T.matrix() | ||
self.numpy_input = np.asarray(np.random.uniform(-4, 4, (5,5)),dtype=theano.config.floatX) | ||
# Create some 5X5 matrix randomly | ||
|
||
def Elu(self, x, alpha = 1): return np.where(x > 0, x, alpha * (np.exp(x) - 1)) | ||
def Abs(self, x): return np.abs(x) | ||
def ReLU(self, x): return x * (x > 0) | ||
def Sigmoid(self, x): return 1 / (1 + np.exp(-x)) | ||
def Tanh(self, x): return np.tanh(x) | ||
def Softmax(self, x, temp = 1): | ||
if temp != 1: | ||
expo = np.exp(x / float(temp)) | ||
return expo / expo.sum(axis = 1, keepdims=True) | ||
else: | ||
return np.exp(x) / np.exp(x).sum(axis = 1, keepdims=True) | ||
def Squared(self, x): return x**2 | ||
class TestActivations(unittest.TestCase): | ||
""" | ||
expected_array values are precomputed using a Python interpreter, numpy_input, | ||
and the corresponding activation function. | ||
""" | ||
|
||
def setUp(self): | ||
""" | ||
numpy_input is hardcoded so we can test against known result values for | ||
different activation functions | ||
""" | ||
self.theano_input = T.matrix() | ||
self.numpy_input = np.array([[-1,2,-3,4,5], | ||
[-1,2,-3,4,5], | ||
[-1,2,-3,4,5], | ||
[-1,2,-3,4,5], | ||
[-1,2,-3,4,5]], | ||
dtype=theano.config.floatX) | ||
|
||
def test_abs(self): | ||
def test_abs(self): | ||
expected_array = np.array([[ 1., 2., 3., 4., 5.], | ||
[ 1., 2., 3., 4., 5.], | ||
[ 1., 2., 3., 4., 5.], | ||
[ 1., 2., 3., 4., 5.], | ||
[ 1., 2., 3., 4., 5.]]) | ||
theano_result = A.Abs(self.theano_input).eval({self.theano_input: self.numpy_input}) | ||
np_result = self.Abs(self.numpy_input) | ||
assert theano_result.shape == np_result.shape | ||
assert np.allclose(theano_result, np_result) | ||
self.assertEqual(theano_result.shape, expected_array.shape) | ||
self.assertTrue(np.allclose(theano_result, expected_array)) | ||
|
||
def test_relu(self): | ||
def test_relu(self): | ||
expected_array = np.array([[-0., 2., -0., 4., 5.], | ||
[-0., 2., -0., 4., 5.], | ||
[-0., 2., -0., 4., 5.], | ||
[-0., 2., -0., 4., 5.], | ||
[-0., 2., -0., 4., 5.]]) | ||
theano_result = A.ReLU(self.theano_input).eval({self.theano_input: self.numpy_input}) | ||
np_result = self.ReLU(self.numpy_input) | ||
assert theano_result.shape == np_result.shape | ||
assert np.allclose(theano_result, np_result) | ||
self.assertEqual(theano_result.shape, expected_array.shape) | ||
self.assertTrue(np.allclose(theano_result, expected_array)) | ||
|
||
def test_elu(self): | ||
alpha = np.asarray(np.random.random(1),dtype = theano.config.floatX) | ||
def test_elu(self): | ||
expected_array = np.array([[-0.63212056, 2., -0.95021293, 4., 5.], | ||
[-0.63212056, 2., -0.95021293, 4., 5.], | ||
[-0.63212056, 2., -0.95021293, 4., 5.], | ||
[-0.63212056, 2., -0.95021293, 4., 5.], | ||
[-0.63212056, 2., -0.95021293, 4., 5.]]) | ||
alpha = 1 | ||
theano_result = A.Elu(self.theano_input,alpha).eval({self.theano_input: self.numpy_input}) | ||
np_result = self.Elu(self.numpy_input,alpha) | ||
assert theano_result.shape == np_result.shape | ||
assert np.allclose(theano_result, np_result) | ||
self.assertEqual(theano_result.shape, expected_array.shape) | ||
self.assertTrue(np.allclose(theano_result, expected_array)) | ||
|
||
def test_sigmoid(self): | ||
def test_sigmoid(self): | ||
expected_array = np.array([[ 0.26894142, 0.88079708, 0.04742587, 0.98201379, 0.99330715], | ||
[ 0.26894142, 0.88079708, 0.04742587, 0.98201379, 0.99330715], | ||
[ 0.26894142, 0.88079708, 0.04742587, 0.98201379, 0.99330715], | ||
[ 0.26894142, 0.88079708, 0.04742587, 0.98201379, 0.99330715], | ||
[ 0.26894142, 0.88079708, 0.04742587, 0.98201379, 0.99330715]]) | ||
theano_result = A.Sigmoid(self.theano_input).eval({self.theano_input: self.numpy_input}) | ||
np_result = self.Sigmoid(self.numpy_input) | ||
assert theano_result.shape == np_result.shape | ||
assert np.allclose(theano_result, np_result) | ||
self.assertEqual(theano_result.shape, expected_array.shape) | ||
self.assertTrue(np.allclose(theano_result, expected_array)) | ||
|
||
def test_tanh(self): | ||
def test_tanh(self): | ||
expected_array = np.array([[-0.76159416, 0.96402758, -0.99505475, 0.9993293 , 0.9999092 ], | ||
[-0.76159416, 0.96402758, -0.99505475, 0.9993293 , 0.9999092 ], | ||
[-0.76159416, 0.96402758, -0.99505475, 0.9993293 , 0.9999092 ], | ||
[-0.76159416, 0.96402758, -0.99505475, 0.9993293 , 0.9999092 ], | ||
[-0.76159416, 0.96402758, -0.99505475, 0.9993293 , 0.9999092 ]]) | ||
theano_result = A.Tanh(self.theano_input).eval({self.theano_input: self.numpy_input}) | ||
np_result = self.Tanh(self.numpy_input) | ||
assert theano_result.shape == np_result.shape | ||
assert np.allclose(theano_result, np_result) | ||
self.assertEqual(theano_result.shape, expected_array.shape) | ||
self.assertTrue(np.allclose(theano_result, expected_array)) | ||
|
||
def test_softmax(self): | ||
def test_softmax(self): | ||
expected_array = np.array([[ 1.74500937e-03, 3.50494502e-02, 2.36161338e-04, | ||
2.58982354e-01, 7.03987026e-01], | ||
[ 1.74500937e-03, 3.50494502e-02, 2.36161338e-04, | ||
2.58982354e-01, 7.03987026e-01], | ||
[ 1.74500937e-03, 3.50494502e-02, 2.36161338e-04, | ||
2.58982354e-01, 7.03987026e-01], | ||
[ 1.74500937e-03, 3.50494502e-02, 2.36161338e-04, | ||
2.58982354e-01, 7.03987026e-01], | ||
[ 1.74500937e-03, 3.50494502e-02, 2.36161338e-04, | ||
2.58982354e-01, 7.03987026e-01]]) | ||
theano_result = A.Softmax(self.theano_input).eval({self.theano_input: self.numpy_input}) | ||
np_result = self.Softmax(self.numpy_input) | ||
assert theano_result.shape == np_result.shape | ||
assert np.allclose(theano_result, np_result) | ||
self.assertEqual(theano_result.shape, expected_array.shape) | ||
self.assertTrue(np.allclose(theano_result, expected_array)) | ||
|
||
def test_temperature_softmax(self): | ||
t = np.asarray(np.random.uniform(2, 10, 1),dtype = theano.config.floatX) | ||
theano_result = A.Softmax(self.theano_input, | ||
temp = t ).eval({self.theano_input: self.numpy_input}) | ||
np_result = self.Softmax(self.numpy_input, temp = t) | ||
assert theano_result.shape == np_result.shape | ||
assert np.allclose(theano_result, np_result) | ||
def test_temperature_softmax(self): | ||
""" | ||
Utilized np.asarray(np.random.uniform(2, 10, 1), dtype = tehano.config.floatX) | ||
to hardcode a value for t. | ||
""" | ||
t = np.array([7.94705237]) | ||
expected_array = np.array([[ 0.1381257 , 0.20147445, 0.10739337, 0.25912957, 0.29387691], | ||
[ 0.1381257 , 0.20147445, 0.10739337, 0.25912957, 0.29387691], | ||
[ 0.1381257 , 0.20147445, 0.10739337, 0.25912957, 0.29387691], | ||
[ 0.1381257 , 0.20147445, 0.10739337, 0.25912957, 0.29387691], | ||
[ 0.1381257 , 0.20147445, 0.10739337, 0.25912957, 0.29387691]]) | ||
theano_result = A.Softmax(self.theano_input, | ||
temp=t).eval({self.theano_input: self.numpy_input}) | ||
self.assertEqual(theano_result.shape, expected_array.shape) | ||
self.assertTrue(np.allclose(theano_result, expected_array)) | ||
|
||
def test_squared(self): | ||
def test_squared(self): | ||
expected_array = np.array([[1., 4., 9., 16., 25.], | ||
[1., 4., 9., 16., 25.], | ||
[1., 4., 9., 16., 25.], | ||
[1., 4., 9., 16., 25.], | ||
[1., 4., 9., 16., 25.]]) | ||
theano_result = A.Squared(self.theano_input).eval({self.theano_input: self.numpy_input}) | ||
np_result = self.Squared(self.numpy_input) | ||
assert theano_result.shape == np_result.shape | ||
assert np.allclose(theano_result, np_result) | ||
self.assertEqual(theano_result.shape, expected_array.shape) | ||
self.assertTrue(np.allclose(theano_result, expected_array)) |
Oops, something went wrong.