def test_saver_no_file(self, mock_isfile):
     mock_isfile.return_value = False
     self.saver = Saver("test", "./", 3, self.lock)
     read_data = ""
     mockOpen = mock_open(read_data=read_data)
     with patch('builtins.open', mockOpen):
         self.saver.save(self.model, condition="Test", result= self.result)
 def test_saver_file_empty(self, mock_isfile):
     mock_isfile.return_value = True
     self.saver = Saver("test", "./", 3, self.lock)
     read_data = pickle.dumps({})
     mockOpen = mock_open(read_data=read_data)
     with patch('builtins.open', mockOpen):
         self.saver.save(self.model, condition="Test", result=self.result)
 def test_load_no_file(self, mock_isfile):
     mock_isfile.return_value = False
     self.saver = Saver("test", "./", 3, self.lock)
     read_data = pickle.dumps({"LSTM": {3:self.model.state_dict()}})
     mockOpen = mock_open(read_data=read_data)
     with self.assertRaises(Exception):
         with patch('builtins.open', mockOpen):
             self.saver.load(self.model)
 def test_load_file(self, mock_isfile):
     mock_isfile.return_value = True
     self.saver = Saver("test", "./", 3, self.lock)
     read_data = pickle.dumps({"LSTM": {3:self.model.state_dict()}})
     mockOpen = mock_open(read_data=read_data)
     with patch('builtins.open', mockOpen):
         model = self.saver.load(self.model)
         self.assertIsInstance(model, LSTMLayer)
Exemple #5
0
    def __init__(self,
                 cardinality: Cardinality,
                 lock: threading.Lock,
                 batch_size=128,
                 path_model="",
                 name_dataset="",
                 batch_result=20000,
                 exclude_test=False,
                 stoppingcondition="earlystopping",
                 condition_value=0.005,
                 condition_step=3,
                 duration=5,
                 condition_epoch=3):
        self.dataset = cardinality
        self.cardinality = self.dataset.cardinality
        self.batch_size = batch_size
        self.model = -1
        # self.stopping_condition = StoppingCondition(method="earlystopping", condition_value = 0.005, condition_step=3)
        if stoppingcondition == "earlystopping":
            self.stopping_condition = StoppingCondition(
                method=stoppingcondition,
                condition_value=condition_value,
                condition_step=condition_step)
        elif stoppingcondition == "timer":
            self.stopping_condition = StoppingCondition(
                method=stoppingcondition, duration=duration)
        elif stoppingcondition == "epoch":
            self.stopping_condition = StoppingCondition(
                method=stoppingcondition, condition_epoch=condition_epoch)
        else:
            raise Exception(
                "Stopping condition method is not implemented. Please use 'earlystopping', 'timer', or 'epoch'"
            )
        self.path_model = path_model
        self.name_dataset = name_dataset
        self.lock = lock
        self.exlude_test = exclude_test
        self.saver = Saver(path_model=self.path_model,
                           name_model=self.name_dataset,
                           cardinality=self.cardinality,
                           lock=self.lock)
        self.batch_result = batch_result

        if torch.cuda.is_available():
            self.device = torch.device('cuda')
            logger.info("Starting learning on GPU")
        else:
            self.device = torch.device('cpu')
            logger.info("Starting learning on CPU")