i have bestcheckpointpath = os.path.join(checkpointpath, 'epoch=44-step=4500.ckpt') how…
- User
i have
best_checkpoint_path = os.path.join(checkpoint_path, 'epoch=44-step=4500.ckpt')
how i can load model
from gluonts.torch import DeepAREstimator from lightning.pytorch.loggers import CSVLogger from lightning.pytorch.callbacks import EarlyStopping, ModelCheckpoint
Путь для сохранения чекпоинтов и логирования
run_name = f'{model_rev}' logger_path = os.path.join(os.getcwd(), work_model_path) checkpoint_path = os.path.join(os.getcwd(), work_model_path, 'lightning_logs', 'version_2', 'checkpoints')
Коллбек на чекпоинт
checkpoint_callback = ModelCheckpoint( dirpath=checkpoint_path, filename='f{model_rev}', save_top_k=1, verbose=True, monitor='val_loss', mode='min' )
Коллбек на раннюю останову
early_stopping_callback = EarlyStopping( monitor='val_loss', patience=10, verbose=True, mode='min', min_delta=0.001 )
Тренер
trainer_kwargs={ "enable_progress_bar": True, "enable_model_summary": True, "max_epochs": num_epochs, "logger": CSVLogger(save_dir=logger_path), "callbacks": [early_stopping_callback] }
Инициализация модели
estimator = DeepAREstimator( freq=time_freq, prediction_length=prediction_length, context_length=context_length, patience=10, num_layers=2, hidden_size=40, lr=1e-4, weight_decay=1e-2, lags_seq=lags_sequence, embedding_dimension=[2], time_features=time_features, num_batches_per_epoch=100, num_feat_static_cat=1, cardinality=[2], # num_feat_dynamic_real=2, batch_size=batch_size, trainer_kwargs=trainer_kwargs, )
into estimator
- Assistant
- User
Selection deleted from gluonts.torch.model.deepar.lightning_module import DeepARLightningModule
if not TRAINING: best_checkpoint_path = os.path.join(checkpoint_path, 'epoch=44-step=4500.ckpt') checkpoint = torch.load(best_checkpoint_path) estimator.deserialize(checkpoint)
code# # Load the model from the checkpoint # loaded_predictor = DeepARLightningModule.load_from_checkpoint(best_checkpoint_path) # # Compare model's state_dict to the one in the checkpoint # model_state_dict = loaded_predictor.state_dict() # checkpoint_state_dict = torch.load(best_checkpoint_path)['state_dict'] # # Check if the model's state_dict matches the one in the checkpoint # if all(k in model_state_dict and torch.all(torch.eq(model_state_dict[k], checkpoint_state_dict[k])) for k in model_state_dict): # print("Model loaded correctly from checkpoint.") # else: # print("Model was not loaded correctly from checkpoint.") - Assistant
- User
and how after use model to predict?
- Assistant
- User
AttributeError Traceback (most recent call last) Cell In[44], line 14 11 test_dataset = temp_dataset['test'] 12 test_dataset.set_transform(partial(transform_start_field, freq=time_freq)) ---> 14 forecasts, forecasts_median, forecasts_mean, forecasts_scale = test_evaluate(predictor, test_dataset)
Cell In[35], line 8, in test_evaluate(predictor, dataset) 5 forecasts_scale = [] 7 # Генерация предсказаний для тестового набора данных ----> 8 for test_entry, forecast in tqdm(zip(dataset, predictor.predict(dataset))): 9 if forecast.samples.shape[1] == prediction_length: 10 outputs = forecast.samples
File ~\miniconda3\envs\pronin\lib\site-packages\torch\nn\modules\module.py:1688, in Module.getattr(self, name) 1686 if name in modules: 1687 return modules[name] -> 1688 raise AttributeError(f"'{type(self).name}' object has no attribute '{name}'")
AttributeError: 'DeepARLightningModule' object has no attribute 'predict'
- Assistant
- User
from gluonts.torch.model.deepar.lightning_module import DeepARLightningModule
if not TRAINING: best_checkpoint_path = os.path.join(checkpoint_path, 'epoch=44-step=4500.ckpt') loaded_model = DeepARLightningModule.load_from_checkpoint(best_checkpoint_path) predictor = estimator.create_predictor(loaded_model)
TypeError Traceback (most recent call last) Cell In[45], line 6 4 best_checkpoint_path = os.path.join(checkpoint_path, 'epoch=44-step=4500.ckpt') 5 loaded_model = DeepARLightningModule.load_from_checkpoint(best_checkpoint_path) ----> 6 predictor = estimator.create_predictor(loaded_model)
TypeError: DeepAREstimator.create_predictor() missing 1 required positional argument: 'module'
- Assistant