def test_save_load(self): model = nnRegressor.Convolutional_NN_Model(self.input_dim, self.target_dim) reg = nnRegressor.NeuralNetRegressor(model=model, patience=1, use_gpu=True) reg.save('testCNN') self.assertTrue(os.path.exists('testCNN')) newReg = nnRegressor.NeuralNetRegressor() newReg.load('testCNN') self.assertEquals(newReg.model.input_dim, self.input_dim) self.assertEquals(newReg.model.output_dim, self.target_dim)
def test_save_load(self): model = nnRegressor.Linear_NN_Model(self.input_dim, self.target_dim, 'mean') reg = nnRegressor.NeuralNetRegressor(model=model, patience=1, use_gpu=True) reg.save('test') self.assertTrue(os.path.exists('test')) newReg = nnRegressor.NeuralNetRegressor() newReg.load('test') self.assertEquals(newReg.model.fc1.in_features, self.input_dim) self.assertEquals(newReg.model.fc3.out_features, self.target_dim)
def test_score(self): model = nnRegressor.Linear_NN_Model(self.input_dim, self.target_dim, 'mean') reg = nnRegressor.NeuralNetRegressor(model=model, patience=1, use_gpu=True) fitted = reg.fit(self.X_train, self.y_train) score = fitted.score(self.X_test, self.y_test)
def test_score(self): input_dim = len(self.X_train[0, :]) target_dim = len(self.y_train[0, :]) model = nnRegressor.Convolutional_NN_Model(input_dim, target_dim) fittedReg = nnRegressor.NeuralNetRegressor( model=model, patience=1).fit(self.X_train, self.y_train) score = fittedReg.score(self.X_test, self.y_test)
def test_predict_without_GPU_default_model(self): fittedReg = nnRegressor.NeuralNetRegressor(patience=1).fit( self.X_train, self.y_train) result = fittedReg.predict(self.X_test) self.assertEqual(next(fittedReg.model.parameters()).is_cuda, False) self.assertEqual(result.shape, (len(self.X_test), len(self.y_test[0, :]))) self.assertTrue(type(result) is numpy.ndarray) self.assertTrue(result.dtype is numpy.dtype('float32') or result.dtype is numpy.dtype('float64'))
def test_predict_without_GPU(self): model = nnRegressor.Linear_NN_Model(self.input_dim, self.target_dim, 'mean') fittedReg = nnRegressor.NeuralNetRegressor( model=model, patience=1).fit(self.X_train, self.y_train) result = fittedReg.predict(self.X_test) self.assertEqual(next(fittedReg.model.parameters()).is_cuda, False) self.assertEqual(result.shape, (len(self.X_test), len(self.y_test[0, :]))) self.assertTrue(type(result) is numpy.ndarray) self.assertTrue(result.dtype is numpy.dtype('float32') or result.dtype is numpy.dtype('float64'))
def perform_GridSearch(X, y): scorer = make_scorer(metrics.average_relative_root_mean_squared_error, greater_is_better=False) parameters = { "learning_rate": [0.01], # 0.001, 0.1, 0.2, 0.3], "patience": [1] #, 3, 5, 8] } reg = GridSearchCV(nn.NeuralNetRegressor(training_limit=10000), parameters, cv=3, scoring=scorer, n_jobs=1) return reg.fit(X, y)
def test_predict_with_GPU(self): if torch.cuda.is_available(): input_dim = len(self.X_train[0, :]) target_dim = len(self.y_train[0, :]) model = nnRegressor.Convolutional_NN_Model(input_dim, target_dim) fittedReg = nnRegressor.NeuralNetRegressor(model=model, patience=1, use_gpu=True).fit( self.X_train, self.y_train) result = fittedReg.predict(self.X_test) self.assertEqual(next(fittedReg.model.parameters()).is_cuda, True) self.assertEqual(result.shape, (len(self.X_test), len(self.y_test[0, :]))) self.assertTrue(type(result) is numpy.ndarray) self.assertTrue(result.dtype is numpy.dtype('float32') or result.dtype is numpy.dtype('float64'))