Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

train.py 1.4 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  1. #!/usr/bin/env python3
  2. import mlflow
  3. import dagshub
  4. import gym
  5. from gym import spaces
  6. import numpy as np
  7. from stable_baselines3 import PPO
  8. import os
  9. class PPOModelWrapper(mlflow.pyfunc.PythonModel):
  10. def load_context(self, context):
  11. self.model = PPO.load(context.artifacts["path"])
  12. def predict(self, context, model_input):
  13. action, states = self.model.predict(model_input)
  14. return {"action": action, "states": states}
  15. # Create a simple custom gym environment
  16. class SimpleEnv(gym.Env):
  17. def __init__(self):
  18. super(SimpleEnv, self).__init__()
  19. self.action_space = spaces.Discrete(3)
  20. self.observation_space = spaces.Box(low=-np.inf, high=np.inf, shape=(4,))
  21. def step(self, action):
  22. return np.array([0, 0, 0, 0]), 0, False, {}
  23. def reset(self):
  24. return np.array([0, 0, 0, 0])
  25. if __name__ == '__main__':
  26. dagshub.init('stable_baselines3', 'jinensetpal') # repo name
  27. # Create and train the model
  28. env = SimpleEnv()
  29. model = PPO("MlpPolicy", env, verbose=1)
  30. model.learn(total_timesteps=1000)
  31. model.save('ppo')
  32. # Save the model using MLflow
  33. mlflow.pyfunc.log_model('model',
  34. python_model=PPOModelWrapper(),
  35. artifacts={'path': 'ppo.zip'})
  36. # Load the model from MLflow using the captured run_id
  37. run_id = mlflow.active_run().info.run_id
  38. loaded_model = mlflow.pyfunc.load_model(f"runs:/{run_id}/model")
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...