Beispiel #1
0
def test_mse_get_layer_method_without_parameters():
	x = MSELoss()

	details = x.get_loss_function()

	assert isinstance(details, dict) == True

	assert issubclass(details["loss_function"], _MSELoss) == True

	assert isinstance(details["keyword_arguments"], dict) == True

	assert details["keyword_arguments"]["reduction"] == 'mean'
Beispiel #2
0
def test_cce_get_layer_method(reduction):
    x = MSELoss(reduction=reduction)

    details = x.get_loss_function()

    assert isinstance(details, dict) == True

    assert issubclass(details["loss_function"], _MSELoss) == True

    assert isinstance(details["keyword_arguments"], dict) == True

    assert details["keyword_arguments"]["reduction"] == reduction
Beispiel #3
0
def test_model_set_model_method():
    model = Model()
    model.set_model(pytorch_model)
    model.compile(optimizer=Adam(), loss_function=MSELoss())

    with pytest.raises(ValueError):
        model = Model()
        model.set_model(None)
Beispiel #4
0
def test_model_summary_method():
    model = Model()
    model.set_model(pytorch_model)
    model.compile(optimizer=Adam(), loss_function=MSELoss())

    model.summary()

    with pytest.raises(Exception):
        model = Model()
        model.summary()
Beispiel #5
0
def test_models_compile_method():
    model = Model()
    model.set_model(pytorch_model)
    model.compile(optimizer=Adam(), loss_function=MSELoss())

    with pytest.raises(ValueError):
        model = Model()
        model.set_model(pytorch_model)

        model.compile(optimizer=Adam(),
                      loss_function=MSELoss(),
                      metrics=["test"])

    with pytest.raises(ValueError):
        model = Model()
        model.set_model(pytorch_model)

        model.compile(optimizer=Adam(),
                      loss_function=MSELoss(),
                      metrics="test")
Beispiel #6
0
def test_model_save_for_inference_method():
    model = Model()
    model.set_model(pytorch_model)
    model.compile(optimizer=Adam(), loss_function=MSELoss())

    with pytest.raises(ValueError):
        model.save_for_inference(123)

    with pytest.raises(ValueError):
        model.save_for_inference("")

    model.save_for_inference("ignore/test.npy")
Beispiel #7
0
def test_model_evaluate_method():
    model = Model()
    model.set_model(pytorch_model)
    model.compile(optimizer=Adam(), loss_function=MSELoss())
    TrainLogger("ignore/")
    test_gen = train_generator()

    model.fit(
        train_data=(X_train, y_train),
        validation_data=(X_validation, y_validation),
        epochs=1,
        batch_size=32,
    )

    model.evaluate(test_data=(X_test, y_test))
    model.evaluate(test_data=(X_test, y_test), batch_size=4)

    model.evaluate(test_data=test_gen, batch_size=4, tests_steps=4)

    with pytest.raises(ValueError):
        model.evaluate(test_data=(X_test, y_test), batch_size=400)
Beispiel #8
0
def test_is_valid_layer():
    assert is_valid_layer(Dense(n_nodes=3)) == True
    assert is_valid_layer(ReLU()) == True
    assert is_valid_layer(MSELoss()) == False
Beispiel #9
0
def test_cce_should_throw_value_error(reduction):
    with pytest.raises(ValueError) as ex:
        x = MSELoss(reduction=reduction)
Beispiel #10
0
# Generating the data
X_train = np.random.rand(100, 1) * 10
y_train = X_train + 5 *np.random.rand(100, 1)

X_validation = np.random.rand(100, 1) * 10
y_validation = X_validation + 5 * np.random.rand(100, 1)

X_test = np.random.rand(10, 1) * 10
y_test = X_test + 5 * np.random.rand(10, 1)

# Making the model
model = Sequential()
model.add(Dense(n_nodes=1, n_inputs=1))

# Building the model
model.build()

# Compiling the model
model.compile(optimizer=Adam(), loss_function=MSELoss())

# Printing model summary
model.summary()

# Training the model
history = model.fit(train_data=(X_train, y_train), validation_data=(X_validation, y_validation), epochs=300, batch_size=4)

# Predicting some values
evaluated = model.evaluate(test_data=(X_test, y_test), batch_size=4)

print(evaluated)
Beispiel #11
0
def test_model_fit_method():
    model = Model()
    model.set_model(pytorch_model)
    model.compile(optimizer=Adam(), loss_function=MSELoss())
    logger = TrainLogger("ignore/")

    model.fit(
        train_data=(X_train, y_train),
        validation_data=(X_validation, y_validation),
        epochs=1,
        batch_size=32,
    )

    model.fit(
        train_data=(X_train, y_train),
        validation_data=(X_validation, y_validation),
        epochs=1,
        batch_size=32,
        callbacks=[logger],
    )

    train_gen = train_generator()
    validation_gen = train_generator()

    model.fit(
        train_data=train_gen,
        validation_data=validation_gen,
        epochs=1,
        batch_size=4,
        steps_per_epoch=5,
        validation_steps=5,
    )

    model.fit(
        train_data=train_gen,
        validation_data=validation_gen,
        epochs=1,
        batch_size=4,
        steps_per_epoch=5,
        validation_steps=5,
        callbacks=[logger],
    )

    with pytest.raises(ValueError):
        model.fit(
            train_data=(X_train, y_train),
            validation_data=(X_validation, y_validation),
            epochs=1,
            batch_size=1024,
        )

    with pytest.raises(ValueError):
        model.fit(
            train_data=(X_train, y_train[:-1]),
            validation_data=(X_validation, y_validation),
            epochs=-20,
            batch_size=1024,
        )

    with pytest.raises(ValueError):
        model.fit(
            train_data=(X_train, y_train[:-1]),
            validation_data=(X_validation, y_validation),
            epochs=1,
            batch_size=-10,
        )

    with pytest.raises(ValueError):
        model.fit(
            train_data=(X_train, y_train[:-1]),
            validation_data=(X_validation, y_validation),
            epochs=1,
            batch_size=32,
            callbacks="test",
        )

    with pytest.raises(ValueError):
        train_gen = train_generator()
        validation_gen = train_generator()

        model.fit(
            train_data=train_gen,
            validation_data=validation_gen,
            epochs=1,
            batch_size=32,
            steps_per_epoch=-123,
            validation_steps=5,
        )

    with pytest.raises(ValueError):
        train_gen = train_generator()
        validation_gen = train_generator()

        model.fit(
            train_data=train_gen,
            validation_data=validation_gen,
            epochs=1,
            batch_size=32,
            steps_per_epoch="test",
            validation_steps=5,
        )

    with pytest.raises(ValueError):
        train_gen = train_generator()
        validation_gen = train_generator()

        model.fit(
            train_data=train_gen,
            validation_data=validation_gen,
            epochs=1,
            batch_size=32,
            steps_per_epoch=5,
            validation_steps=-23,
        )

    with pytest.raises(ValueError):
        train_gen = train_generator()
        validation_gen = train_generator()

        model.fit(
            train_data=train_gen,
            validation_data=validation_gen,
            epochs=1,
            batch_size=32,
            steps_per_epoch=5,
            validation_steps="asd",
        )
Beispiel #12
0
'''

model = Sequential()
model.add(Dense(n_nodes=1, n_inputs=3))
model.add(ReLU())
model.add(Dense(n_nodes=2))
model.add(ReLU())
model.add(Dense(n_nodes=1))
model.add(ReLU())

# Building the Model
model.build()

# Compiling
model.compile(optimizer=Adam(), loss_function=MSELoss(), metrics=["accuracy"])
print(model.summary())

# Data for XOR

x_train = np.array([[0, 0, 1], [0, 1, 1], [1, 0, 1]])
x_test = np.array([[1, 1, 1]])
y_train = np.array([[0], [1], [1]], dtype=np.float32)
y_test = np.array([[0]], dtype=np.float32)

# Training the model
model.fit(train_data=(x_train, y_train), epochs=20, batch_size=1)

# Prediction
print(model.predict(x_test[0]))