Views
No views yet
1import torch
2from huggingface_hub import hf_hub_download
3import torch.nn as nn
4import numpy as np
5
6# Download the model
7model_path = hf_hub_download(repo_id="siddheshtv/td3-stock-aapl", filename="td3_stock_prediction_model_AAPL_full.pth")
8
9# Load the model
10checkpoint = torch.load(model_path)
11
12# Recreate the Actor class
13class Actor(nn.Module):
14 def __init__(self, state_dim, action_dim, max_action):
15 super(Actor, self).__init__()
16 self.net = nn.Sequential(
17 nn.Linear(state_dim, 400),
18 nn.ReLU(),
19 nn.Dropout(0.2),
20 nn.Linear(400, 300),
21 nn.ReLU(),
22 nn.Dropout(0.2),
23 nn.Linear(300, action_dim),
24 nn.Tanh()
25 )
26 self.max_action = max_action
27
28 def forward(self, state):
29 return self.max_action * self.net(state)
30
31# Instantiate the model
32model = Actor(checkpoint['state_dim'], checkpoint['action_dim'], checkpoint['max_action'])
33model.load_state_dict(checkpoint['model_state_dict'])
34model.eval() # Set the model to evaluation mode
35
36# Function to select action
37def select_action(state):
38 with torch.no_grad():
39 state = torch.FloatTensor(state.reshape(1, -1))
40 return model(state).cpu().data.numpy().flatten()
41
42# Example usage
43state = np.random.rand(checkpoint['state_dim']) # Replace with actual state data
44action = select_action(state)
45print(f"Predicted action: {action}")@misc{siddheshtv-td3,
title={TD3 Model for AAPL Stock Prediction},
author={Siddhesh Kulthe},
year={2024},
howpublished={\url{https://huggingface.co/siddheshtv/td3-stock-aapl}},
note={TD3 model for predicting stock price movements of AAPL using reinforcement learning},
}