Views
No views yet
1from stable_baselines3 import PPO
2from stable_baselines3.common.vec_env import VecNormalize
3import os
4
5# Create or reconstruct an environment similar to the one used for training
6# e.g. `env = make_your_env(...)` — replace with your env factory
7env = ...
8
9# If you saved VecNormalize separately, load and wrap your env first
10if os.path.exists("models/vecnormalize.pkl"):
11 vec = VecNormalize.load("models/vecnormalize.pkl", env)
12 vec.training = False
13 vec.norm_reward = False
14 env = vec
15
16# Load the full model (policy + optimizer state)
17model = PPO.load("models/ppo_xauusd.zip", env=env)1from safetensors.torch import load_file
2import torch
3from stable_baselines3 import PPO
4from stable_baselines3.common.vec_env import VecNormalize
5import os
6
7# Create or reconstruct the same environment used for training
8env = ...
9
10# If you have VecNormalize statistics, load them and wrap the env
11if os.path.exists("models/vecnormalize.pkl"):
12 vec = VecNormalize.load("models/vecnormalize.pkl", env)
13 vec.training = False
14 vec.norm_reward = False
15 env = vec
16
17# Instantiate a PPO model with the same policy architecture
18model = PPO("MlpPolicy", env)
19
20# Load SafeTensors state dict and convert values to torch.Tensor if needed
21raw_state = load_file("models/ppo_xauusd.safetensors")
22state_dict = {k: (torch.tensor(v) if not isinstance(v, torch.Tensor) else v) for k, v in raw_state.items()}
23
24# Load weights into the policy
25model.policy.load_state_dict(state_dict)
26
27# Ensure the model has the same env wrapper
28model.set_env(env)ppo_xauusd.zip is available (it contains the entire SB3 model).vec.training = False and vec.norm_reward = False when running inference.XAUUSDTradingEnv)ppo_xauusd.safetensors: Model weights in SafeTensors formatvecnormalize.pkl: VecNormalize statistics for observation normalization