【发布时间】:2022-03-01 04:17:42
【问题描述】:
我已经用 pytorch 训练了一个 RNN 模型。由于 glibc 存在一些奇怪的依赖问题,我需要在无法安装 pytorch 的环境中使用该模型进行预测。但是,我可以安装 numpy 和 scipy 等库。所以,我想使用经过训练的模型,有网络定义,没有 pytorch。
当我将模型及其状态字典和权重保存在 the standard way 时,我拥有模型的权重,但我也可以仅使用 json/pickle 文件或类似文件来保存它。
我也有网络定义,它在很多方面依赖于 pytorch。这是我的 RNN 网络定义。
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import random
torch.manual_seed(1)
random.seed(1)
device = torch.device('cpu')
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size,num_layers, matching_in_out=False, batch_size=1):
super(RNN, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.num_layers = num_layers
self.batch_size = batch_size
self.matching_in_out = matching_in_out #length of input vector matches the length of output vector
self.lstm = nn.LSTM(input_size, hidden_size,num_layers)
self.hidden2out = nn.Linear(hidden_size, output_size)
self.hidden = self.init_hidden()
def forward(self, feature_list):
feature_list=torch.tensor(feature_list)
if self.matching_in_out:
lstm_out, _ = self.lstm( feature_list.view(len( feature_list), 1, -1))
output_space = self.hidden2out(lstm_out.view(len( feature_list), -1))
output_scores = torch.sigmoid(output_space) #we'll need to check if we need this sigmoid
return output_scores #output_scores
else:
for i in range(len(feature_list)):
cur_ft_tensor=feature_list[i]#.view([1,1,self.input_size])
cur_ft_tensor=cur_ft_tensor.view([1,1,self.input_size])
lstm_out, self.hidden = self.lstm(cur_ft_tensor, self.hidden)
outs=self.hidden2out(lstm_out)
return outs
def init_hidden(self):
#return torch.rand(self.num_layers, self.batch_size, self.hidden_size)
return (torch.rand(self.num_layers, self.batch_size, self.hidden_size).to(device),
torch.rand(self.num_layers, self.batch_size, self.hidden_size).to(device))
我知道this question,但我愿意尽可能降低级别。我可以使用 numpy 数组而不是张量,并且可以使用 reshape 代替视图,而且我不需要设备设置。
基于上面的类定义,我在这里可以看到,我只需要来自torch的以下组件就可以从forward函数中获取输出:
- nn.LSTM
- nn.线性
- torch.sigmoid
我想我可以轻松implement the sigmoid function using numpy。但是,我可以使用不涉及 pytorch 的东西来实现 nn.LSTM 和 nn.Linear 吗?另外,我将如何将 state dict 中的权重用于新类?
那么,问题是,我怎样才能将这个 RNN 定义“翻译”成一个不需要 pytorch 的类,以及如何为它使用 state dict 权重? 或者,是否有 pytorch 的“轻量级”版本,我可以使用它来运行模型并产生结果?
编辑
我认为包含 nn.LSTM 和 nn.linear 的 numpy/scipy 等效项可能很有用。它将帮助我们将相同代码的 numpy 输出与 torch 输出进行比较,并为我们提供一些模块化代码/函数以供使用。具体来说,以下内容的 numpy 等价物会很棒:
rnn = nn.LSTM(10, 20, 2)
input = torch.randn(5, 3, 10)
h0 = torch.randn(2, 3, 20)
c0 = torch.randn(2, 3, 20)
output, (hn, cn) = rnn(input, (h0, c0))
也适用于线性:
m = nn.Linear(20, 30)
input = torch.randn(128, 20)
output = m(input)
【问题讨论】:
-
出于兴趣,您面临的 glibc 依赖问题是什么?
-
@OllieGraham 在我的主机上,glibc 版本是 2.12,但 pytorch 需要 2.14,我尝试了很多方法来安装它,但我不能(主要是因为我没有 root 访问权限)
-
谢谢@hmghaly - 我知道它不能回答您的问题,但如果您的托管平台允许,我建议将模型部署在容器中(例如使用 Docker),以便您可以配置根据需要环境并安装所有相关依赖项
-
感谢@OllieGraham,但不幸的是,主机有很多限制,包括安装东西、RAM 和文件数量
标签: python machine-learning neural-network pytorch recurrent-neural-network