def test_model_add_layers(): model = Model() model.add(Dense(10)) model.add(Activation('relu')) model.add(Dense(1)) assert len(model.layers) == 3 assert type(model.layers[0]) == Dense assert type(model.layers[1]) == Activation
def test_relu_output_size(): x = torch.randn(2, 2) l1 = Dense(3, input_dim=2) l2 = Activation('relu') y = l1.forward(x) y = l2.forward(y) assert y.size() == (2, 3) assert (y.data >= 0).sum() == 6
def test_model_fit_unknown_loss(): x = torch.rand(20, 4) y = torch.rand(20, 10) model = Model(Dense(10, input_dim=x.size()[-1]), Activation('relu'), Dense(5), Activation('relu'), Dense(y.size()[-1])) assert len(model.params) > 0 with pytest.raises(Exception) as e: model.fit(x, y, loss='UNKNOWN_TEST', batch_size=10, n_epoch=5)
def test_model_adam_optmizer(): X = np.random.normal(size=[10, 10]).astype('float32') y = np.random.normal(size=[10, 1]).astype('float32') model = Model(Dense(10, input_dim=X.shape[-1]), Activation('relu'), Dense(5), Activation('relu'), Dense(y.shape[-1])) history = model.fit(X, y=y, loss='mse', optimizer='adam', epochs=10) y_pred = model.predict(X) assert type(y_pred) is np.ndarray assert len(history['loss']) == 10 assert all(type(v) is float for v in history['loss']) assert history['loss'] == sorted(history['loss'], reverse=True)
def test_model_simple_fit(): x = torch.rand(20, 4) y = torch.rand(20, 10) model = Model(Dense(10, input_dim=x.size()[-1]), Activation('relu'), Dense(5), Activation('relu'), Dense(y.size()[-1])) opt = SGD(lr=0.01, momentum=0.9) loss = mean_squared_error history = model.fit(x, y, loss=loss, optimizer='sgd', epochs=10, verbose=1) assert len(history['loss']) == 10 assert all(type(v) is float for v in history['loss']) assert history['loss'] == sorted(history['loss'], reverse=True)
def test_model_conv2d(): X = np.random.normal(size=[10, 3, 10, 10]).astype('float32') y = np.random.normal(size=[10, 1]).astype('float32') model = Model(Conv2D(4, kernel_size=(3, 3), input_dim=X.shape[1:]), Flatten(), Dense(5), Activation('relu'), Dense(y.shape[-1])) history = model.fit(X, y=y, loss='mse', val_data=(X, y)) y_pred = model.predict(X) assert type(y_pred) is np.ndarray assert 'loss' in history assert 'val_loss' in history assert all(type(v) is float for v in history['loss']) assert all(type(v) is float for v in history['val_loss']) assert history['loss'] == sorted(history['loss'], reverse=True)
def test_model_validation_split(): X = np.random.normal(size=[10, 10]).astype('float32') y = np.random.normal(size=[10, 1]).astype('float32') model = Model(Dense(10, input_dim=X.shape[-1]), Activation('relu'), Dense(5), Activation('relu'), Dense(y.shape[-1])) history = model.fit(X, y=y, loss='mse', val_split=0.1) y_pred = model.predict(X) assert type(y_pred) is np.ndarray assert 'loss' in history assert 'val_loss' in history assert all(type(v) is float for v in history['loss']) assert all(type(v) is float for v in history['val_loss']) assert history['loss'] == sorted(history['loss'], reverse=True)
def test_model_custom_loss(): x = torch.rand(20, 4) y = torch.rand(20, 10) model = Model(Dense(10, input_dim=x.size()[-1]), Activation('relu'), Dense(5), Activation('relu'), Dense(y.size()[-1])) opt = SGD(lr=0.01, momentum=0.9) def mae(y_true, y_pred): return torch.mean(torch.abs(y_true - y_pred)) history = model.fit(x, y, loss=mae, optimizer=opt, epochs=10) assert len(history['loss']) == 10 assert all(type(v) is float for v in history['loss']) assert history['loss'] == sorted(history['loss'], reverse=True)
def test_initializer_glorot_uniform(): X = Tensor([[10, 10, 10]]) input_dim = X.size()[-1] dense = Dense(10, init='glorot_uniform', input_dim=input_dim) assert dense.params[0].max() <= math.sqrt(6 / (input_dim + 10)) assert dense.params[1].max() <= math.sqrt(6 / (input_dim + 10)) assert dense.params[0].min() >= -math.sqrt(6 / (input_dim + 10)) assert dense.params[1].min() >= -math.sqrt(6 / (input_dim + 10))
def test_model_recurrent_time_distributed(): X = np.random.normal(size=[2, 3, 4]).astype('float32') y = np.random.normal(size=[2, 3, 10]).astype('float32') model = Model( Recurrent(units=2, length=3, input_dim=4), Activation('relu'), TimeDistributed(Dense(units=10)), ) history = model.fit(X, y, loss='mse') y_pred = model.predict(X) assert history['loss'] == sorted(history['loss'], reverse=True)
def test_dense_multiple_layers(): x = torch.randn(2, 10) l1 = Dense(5, input_dim=10) l2 = Dense(3, input_dim=5) y = l1.forward(x) assert y.size() == (2, 5) y = l2.forward(y) assert y.size() == (2, 3)
def test_model_forward(): model = Model(Dense(10, input_dim=4), Dense(1), Dense(20)) x = torch.randn(2, 4) y = model.forward(x) assert y.size() == (2, 20) model = Model(Dense(10, input_dim=4), Dense(1), Dense(20)) x = torch.randn(2, 4) y = model.predict(x) assert y.size() == (2, 20)
X = X / 127.0 X_test = X_test / 127.0 y = np.eye(y.max() + 1)[y] y_test = np.eye(y_test.max() + 1)[y_test] print(X.shape, X_test.shape) X = X.astype('float32') X_test = X_test.astype('float32') y = y.astype('float32') y_test = y_test.astype('float32') model = Model( Conv2D(8, kernel_size=(3, 3), input_dim=X.shape[1:]), Flatten(), Activation('relu'), Dropout(0.5), Dense(100), Activation('relu'), Dropout(0.5), Dense(y_test.shape[-1]), Activation('softmax') ) loss = 'categorical_crossentropy' history = model.fit(X, y, loss=loss, val_data=(X_test, y_test)) y_pred = model.predict(X_test) acc = metrics.accuracy_score(y_test.argmax(axis=1), y_pred.argmax(axis=1)) print('Classes:', y.shape[1]) print('Accuracy:', acc)
def test_layer_get_params(): l = Dense(3, input_dim=3) assert len(l.params) == 2 l = Activation('relu') assert len(l.params) == 0
def test_model_constructor_layers(): model = Model(Dense(10), Activation('relu'), Dense(1)) assert len(model.layers) == 3 assert type(model.layers[0]) == Dense assert type(model.layers[1]) == Activation
def test_dense_layer_forward(): x = torch.randn(2, 10) l = Dense(5, input_dim=10) y = l.forward(x) assert y.size() == (2, 5)
def test_dense_layer_output_dim(): l = Dense(5, input_dim=10) assert l.output_dim == 5
from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn import metrics import numpy as np from aorun.models import Model from aorun.layers import Dense from aorun.layers import Activation X, y = datasets.load_digits(return_X_y=True) X = X.astype('float32') y = np.eye(y.max() + 1)[y].astype('float32') X = StandardScaler().fit_transform(X) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3) print(X_train.shape, y_train.shape) model = Model(Dense(100, input_dim=X_train.shape[-1]), Activation('relu'), Dense(100), Activation('relu'), Dense(y_test.shape[-1]), Activation('softmax')) loss = 'categorical_crossentropy' history = model.fit(X_train, y_train, loss=loss, val_split=0.1) y_pred = model.predict(X_test) acc = metrics.accuracy_score(y_test.argmax(axis=1), y_pred.argmax(axis=1)) print('Classes:', y.shape[1]) print('Accuracy:', acc)