ben*_*hur 6 python cython pytorch
我试图找到nn.Module用 Cython 编写的 PyTorch 类的示例以提高速度,但没有找到任何内容。假设我用 Python 编写了下面的类,最好的 Cython 翻译是什么?
class Actor(nn.Module):
def __init__(self, state_size, action_size, hidden_size):
super(Actor, self).__init__()
self.l1 = nn.Linear(state_size, hidden_size)
self.l2 = nn.Linear(hidden_size, hidden_size)
self.l3 = nn.Linear(hidden_size, hidden_size)
self.l4 = nn.Linear(hidden_size, action_size)
self.log_std = nn.Parameter(-0.5 * torch.ones(action_size, dtype=torch.float32))
def forward(self, x):
x = torch.relu(self.l1(x))
x = torch.relu(self.l2(x))
x = torch.relu(self.l3(x))
mu = self.l4(x)
return mu
def dist(self, mu):
pi = Normal(mu, torch.exp(self.log_std))
return pi
def log_prob(self, pi, action):
return pi.log_prob(action).sum(axis=-1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2784 次 |
| 最近记录: |