mirror of
https://github.com/wassname/keras-contrib.git
synced 2026-09-09 11:25:17 +08:00
fix import errors in tests
This commit is contained in:
@@ -123,7 +123,7 @@ def test_cosineconvolution_2d():
|
||||
W0 = X[0, :, :, :, None]
|
||||
|
||||
model = Sequential()
|
||||
model.add(convolutional.CosineConvolution2D(1, 5, 5, use_bias=True, input_shape=input_dim, dim_ordering=dim_ordering))
|
||||
model.add(convolutional.CosineConvolution2D(1, (5, 5), use_bias=True, input_shape=input_dim, data_format=data_format))
|
||||
model.compile(loss='mse', optimizer='rmsprop')
|
||||
W = model.get_weights()
|
||||
W[0] = W0
|
||||
@@ -133,7 +133,7 @@ def test_cosineconvolution_2d():
|
||||
assert_allclose(out, np.ones((1, 1, 1, 1), dtype=K.floatx()), atol=1e-5)
|
||||
|
||||
model = Sequential()
|
||||
model.add(convolutional.CosineConvolution2D(1, 5, 5, use_bias=False, input_shape=input_dim, dim_ordering=dim_ordering))
|
||||
model.add(convolutional.CosineConvolution2D(1, (5, 5), use_bias=False, input_shape=input_dim, data_format=data_format))
|
||||
model.compile(loss='mse', optimizer='rmsprop')
|
||||
W = model.get_weights()
|
||||
W[0] = -2 * W0
|
||||
@@ -151,7 +151,7 @@ def test_sub_pixel_upscaling():
|
||||
for scale_factor in [2, 3, 4]:
|
||||
input_data = np.random.random((nb_samples, 4 * (scale_factor ** 2), nb_row, nb_col))
|
||||
|
||||
if K.image_dim_ordering() == 'tf':
|
||||
if K.image_data_format() == 'tf':
|
||||
input_data = input_data.transpose((0, 2, 3, 1))
|
||||
|
||||
input_tensor = K.variable(input_data)
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
import pytest
|
||||
import numpy as np
|
||||
from numpy.testing import assert_allclose
|
||||
|
||||
from keras import backend as K
|
||||
from keras_contrib import backend as KC
|
||||
from keras_contrib import activations
|
||||
|
||||
|
||||
def get_standard_values():
|
||||
'''
|
||||
These are just a set of floats used for testing the activation
|
||||
functions, and are useful in multiple tests.
|
||||
'''
|
||||
return np.array([[0, 0.1, 0.5, 0.9, 1.0]], dtype=K.floatx())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__])
|
||||
@@ -1,29 +0,0 @@
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense
|
||||
from keras.utils.test_utils import get_test_data
|
||||
from keras import backend as K
|
||||
from keras_contrib import backend as KC
|
||||
from keras.utils import np_utils
|
||||
from keras_contrib import callbacks
|
||||
|
||||
import os
|
||||
import sys
|
||||
import multiprocessing
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
from csv import Sniffer
|
||||
|
||||
|
||||
np.random.seed(1337)
|
||||
|
||||
input_dim = 2
|
||||
nb_hidden = 4
|
||||
nb_class = 2
|
||||
batch_size = 5
|
||||
train_samples = 20
|
||||
test_samples = 20
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__])
|
||||
@@ -1,25 +0,0 @@
|
||||
import pytest
|
||||
import numpy as np
|
||||
from numpy.testing import assert_allclose
|
||||
|
||||
from keras import backend as K
|
||||
from keras_contrib import backend as KC
|
||||
from keras_contrib import constraints
|
||||
|
||||
|
||||
test_values = [0.1, 0.5, 3, 8, 1e-7]
|
||||
np.random.seed(3537)
|
||||
example_array = np.random.random((100, 100)) * 100. - 50.
|
||||
example_array[0, 0] = 0. # 0 could possibly cause trouble
|
||||
|
||||
|
||||
def test_clip():
|
||||
clip_instance = constraints.clip()
|
||||
clipped = clip_instance(K.variable(example_array))
|
||||
assert(np.max(np.abs(K.eval(clipped))) <= K.cast_to_floatx(0.01))
|
||||
clip_instance = constraints.clip(0.1)
|
||||
clipped = clip_instance(K.variable(example_array))
|
||||
assert(np.max(np.abs(K.eval(clipped))) <= K.cast_to_floatx(0.1))
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__])
|
||||
@@ -1,45 +0,0 @@
|
||||
from keras import backend as K
|
||||
from keras_contrib import backend as KC
|
||||
from keras_contrib import initializations
|
||||
import pytest
|
||||
import numpy as np
|
||||
|
||||
|
||||
# 2D tensor test fixture
|
||||
FC_SHAPE = (100, 100)
|
||||
|
||||
# 4D convolution in th order. This shape has the same effective shape as FC_SHAPE
|
||||
CONV_SHAPE = (25, 25, 2, 2)
|
||||
|
||||
# The equivalent shape of both test fixtures
|
||||
SHAPE = (100, 100)
|
||||
|
||||
|
||||
def _runner(init, shape, target_mean=None, target_std=None,
|
||||
target_max=None, target_min=None):
|
||||
variable = init(shape)
|
||||
output = K.get_value(variable)
|
||||
lim = 1e-2
|
||||
if target_std is not None:
|
||||
assert abs(output.std() - target_std) < lim
|
||||
if target_mean is not None:
|
||||
assert abs(output.mean() - target_mean) < lim
|
||||
if target_max is not None:
|
||||
assert abs(output.max() - target_max) < lim
|
||||
if target_min is not None:
|
||||
assert abs(output.min() - target_min) < lim
|
||||
|
||||
|
||||
'''
|
||||
# Example :
|
||||
|
||||
@pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])
|
||||
def test_uniform(tensor_shape):
|
||||
_runner(initializations.uniform, tensor_shape, target_mean=0.,
|
||||
target_max=0.05, target_min=-0.05)
|
||||
|
||||
'''
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__])
|
||||
@@ -1,22 +0,0 @@
|
||||
import pytest
|
||||
import numpy as np
|
||||
|
||||
from keras import backend as K
|
||||
from keras_contrib import backend as KC
|
||||
from keras_contrib import metrics
|
||||
|
||||
|
||||
all_metrics = []
|
||||
all_sparse_metrics = []
|
||||
|
||||
|
||||
def test_metrics():
|
||||
y_a = K.variable(np.random.random((6, 7)))
|
||||
y_b = K.variable(np.random.random((6, 7)))
|
||||
for metric in all_metrics:
|
||||
output = metric(y_a, y_b)
|
||||
assert K.eval(output).shape == ()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__])
|
||||
@@ -1,56 +0,0 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
from keras import backend as K
|
||||
from numpy.testing import assert_allclose
|
||||
|
||||
from keras_contrib import backend as KC
|
||||
from keras_contrib import objectives
|
||||
|
||||
allobj = []
|
||||
|
||||
|
||||
def test_objective_shapes_3d():
|
||||
y_a = K.variable(np.random.random((5, 6, 7)))
|
||||
y_b = K.variable(np.random.random((5, 6, 7)))
|
||||
for obj in allobj:
|
||||
objective_output = obj(y_a, y_b)
|
||||
assert K.eval(objective_output).shape == (5, 6)
|
||||
|
||||
|
||||
def test_objective_shapes_2d():
|
||||
y_a = K.variable(np.random.random((6, 7)))
|
||||
y_b = K.variable(np.random.random((6, 7)))
|
||||
for obj in allobj:
|
||||
objective_output = obj(y_a, y_b)
|
||||
assert K.eval(objective_output).shape == (6,)
|
||||
|
||||
|
||||
def test_dssim_same():
|
||||
x = np.random.random_sample(30 * 30 * 3).reshape([1, 30, 30, 3])
|
||||
x1 = KC.variable(x)
|
||||
loss = objectives.DSSIMObjective()
|
||||
assert_allclose([0.0], KC.eval(loss(x1, x1)), atol=1.0e-4)
|
||||
|
||||
|
||||
def test_dssim_opposite():
|
||||
x = np.zeros([1, 30, 30, 3])
|
||||
x1 = KC.variable(x)
|
||||
y = np.ones([1, 30, 30, 3])
|
||||
y1 = KC.variable(y)
|
||||
loss = objectives.DSSIMObjective()
|
||||
assert_allclose([0.5], KC.eval(loss(x1, y1)), atol=1.0e-4)
|
||||
|
||||
|
||||
def test_dssim_compile():
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Convolution2D
|
||||
x = np.zeros([1, 30, 30, 3])
|
||||
loss = objectives.DSSIMObjective()
|
||||
model = Sequential()
|
||||
model.add(Convolution2D(3, 3, 3, border_mode="same", input_shape=(30, 30, 3)))
|
||||
model.compile("rmsprop", loss)
|
||||
model.fit([x], [x], 1, 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__])
|
||||
@@ -1,43 +0,0 @@
|
||||
from __future__ import print_function
|
||||
from keras.utils.test_utils import get_test_data
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Activation
|
||||
from keras.utils.np_utils import to_categorical
|
||||
from keras_contrib import optimizers
|
||||
import pytest
|
||||
import numpy as np
|
||||
np.random.seed(1337)
|
||||
|
||||
|
||||
(X_train, y_train), (X_test, y_test) = get_test_data(nb_train=1000,
|
||||
nb_test=200,
|
||||
input_shape=(10,),
|
||||
classification=True,
|
||||
nb_class=2)
|
||||
y_train = to_categorical(y_train)
|
||||
y_test = to_categorical(y_test)
|
||||
|
||||
|
||||
def get_model(input_dim, nb_hidden, output_dim):
|
||||
model = Sequential()
|
||||
model.add(Dense(nb_hidden, input_shape=(input_dim,)))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Dense(output_dim))
|
||||
model.add(Activation('softmax'))
|
||||
return model
|
||||
|
||||
|
||||
def _test_optimizer(optimizer, target=0.89):
|
||||
model = get_model(X_train.shape[1], 10, y_train.shape[1])
|
||||
model.compile(loss='categorical_crossentropy',
|
||||
optimizer=optimizer,
|
||||
metrics=['accuracy'])
|
||||
history = model.fit(X_train, y_train, nb_epoch=12, batch_size=16,
|
||||
validation_data=(X_test, y_test), verbose=2)
|
||||
config = optimizer.get_config()
|
||||
assert type(config) == dict
|
||||
assert history.history['val_acc'][-1] >= target
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__])
|
||||
@@ -1,55 +0,0 @@
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Merge
|
||||
from keras.layers import Dense
|
||||
from keras.layers import Activation
|
||||
from keras.layers import Flatten
|
||||
from keras.layers import ActivityRegularization
|
||||
from keras.layers import Embedding
|
||||
from keras.datasets import mnist
|
||||
from keras.utils import np_utils
|
||||
from keras_contrib import regularizers
|
||||
import pytest
|
||||
import numpy as np
|
||||
np.random.seed(1337)
|
||||
|
||||
|
||||
nb_classes = 10
|
||||
batch_size = 128
|
||||
nb_epoch = 5
|
||||
weighted_class = 9
|
||||
standard_weight = 1
|
||||
high_weight = 5
|
||||
max_train_samples = 5000
|
||||
max_test_samples = 1000
|
||||
|
||||
|
||||
def get_data():
|
||||
# the data, shuffled and split between tran and test sets
|
||||
(X_train, y_train), (X_test, y_test) = mnist.load_data()
|
||||
X_train = X_train.reshape(60000, 784)[:max_train_samples]
|
||||
X_test = X_test.reshape(10000, 784)[:max_test_samples]
|
||||
X_train = X_train.astype("float32") / 255
|
||||
X_test = X_test.astype("float32") / 255
|
||||
|
||||
# convert class vectors to binary class matrices
|
||||
y_train = y_train[:max_train_samples]
|
||||
y_test = y_test[:max_test_samples]
|
||||
Y_train = np_utils.to_categorical(y_train, nb_classes)
|
||||
Y_test = np_utils.to_categorical(y_test, nb_classes)
|
||||
test_ids = np.where(y_test == np.array(weighted_class))[0]
|
||||
|
||||
return (X_train, Y_train), (X_test, Y_test), test_ids
|
||||
|
||||
|
||||
def create_model(weight_reg=None, activity_reg=None):
|
||||
model = Sequential()
|
||||
model.add(Dense(50, input_shape=(784,)))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Dense(10, W_regularizer=weight_reg,
|
||||
activity_regularizer=activity_reg))
|
||||
model.add(Activation('softmax'))
|
||||
return model
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__])
|
||||
Reference in New Issue
Block a user