def test_lstm_model_fit_fail(): with pytest.raises(Exception): model = lstm_model(**lstm_params) x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) y = np.array( [100, 101, 102, 105, 107, 109, 110, 115, 119, 120, "random"]) model.fit(x, y)
def test_lstm_model_predict_fail(): with pytest.raises(Exception): model = lstm_model(**lstm_params) x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) y = np.array([100, 101, 102, 105, 107, 109, 110, 115, 119, 120]) model.fit(x, y) # wrong input type result = model.predict([11])
def test_lstm_model_predict_pass(): model = lstm_model(**lstm_params) x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) y = np.array([100, 101, 102, 105, 107, 109, 110, 115, 119, 120]) model.fit(x, y) result = np.round(model.predict(np.array([11])), 0) expect = np.array([123]) assert np.allclose(result, expect)
def test_lstm_model_fit_pass(): model = lstm_model(**lstm_params) x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) y = np.array([100, 101, 102, 105, 107, 109, 110, 115, 119, 120]) model.fit(x, y) result = model.lasty expect = 120 assert result == expect
def update_prediction(self): ''' with new curren value and span, update the prediction ''' try: if self.model_type == "exp": self.model = exponential_model( **cattrs.param["prediction_models"]["exponential_model"]) if self.model_type == "lstm": self.model = lstm_model( **cattrs.param["prediction_models"]["lstm_model"]) if self.model_type == "logistic": self.model = logistic_model( **cattrs.param["prediction_models"]["logistic_model"]) # write model details if self.model_type == "none": self.pred_values = [0, 0, 0, 0, 0, 0, 0, 0] self.model_details["model_name"] = "None" self.model_details["r2"] = 0.0 self.model_details["msle"] = 0.0 self.model_details["show_prediction"] = False else: # update prediction values x, y, x_test = np.array(self.keys_use), np.array( self.values_use), np.array(self.pred_keys) try: self.model.fit(x, y) self.pred_values = list(self.model.predict(x_test)) except: # if fail to fit or predict, use the last value as prediction self.pred_values = list( np.ones(x_test.shape) * self.values[-1]) # get last x day performance try: self.evaluate_model() except: # if evaluation fails, show the failure on the web app self.model_details = { "r2": "Evaluation fails", "msle": "Evaluation fails" } self.model_details["model_name"] = self.model.get_name() self.model_details["show_prediction"] = True except Exception as ex: logger.error(Exception)
def test_lstm_model_get_name_fail(): with pytest.raises(Exception): model = lstm_model(**lstm_params) result = model.get_name("random")
def test_lstm_model_init_fail(): with pytest.raises(Exception): # invalid input model = lstm_model(nodes="random")
def test_lstm_model_get_name_pass(): model = lstm_model(**lstm_params) result = model.get_name() expected = "LSTM Model" assert result == expected
def test_lstm_model_init_pass(): model = lstm_model(**lstm_params) assert model.n_steps == 3