我正在研究GRU,当我尝试进行预测时,出现错误,表明我需要为forward()定义h。在谷歌搜索和搜索堆栈溢出数小时后,我尝试了几件事,并没有耐心。
这是课程:
Rscript -e "Sys.setenv(RSTUDIO_PANDOC='/usr/lib/rstudio/bin/pandoc'); bookdown::render_book('index.Rmd', 'bookdown::gitbook')"
,然后在这里加载模型并尝试进行预测。两者都在同一脚本中。
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome('/Users/toanhac/Downloads/chromedriver')
url = 'https://mail.google.com/mail/u/0/#inbox'
driver.get(url)
driver.implicitly_wait(15)
driver.find_element_by_id("identifierId").send_keys(mail)
driver.find_element_by_id("identifierNext").click()
driver.find_element_by_name("password").send_keys(password)
driver.find_element_by_id("passwordNext").click()
没有Gmodel的任何其他参数,我得到以下信息:
class GRUNet(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, n_layers, drop_prob = 0.2):
super(GRUNet, self).__init__()
self.hidden_dim = hidden_dim
self.n_layers = n_layers
self.gru = nn.GRU(input_dim, hidden_dim, n_layers, batch_first=True, dropout=drop_prob)
self.fc = nn.Linear(hidden_dim, output_dim)
self.relu = nn.ReLU()
def forward(self, x, h):
out, h = self.gru(x,h)
out = self.fc(self.relu(out[:,-1]))
return out, h
def init_hidden(self, batch_size):
weight = next(self.parameters()).data
hidden = weight.new(self.n_layers, batch_size, self.hidden_dim).zero_().to(device)
return hidden
答案 0 :(得分:0)
您还需要提供隐藏状态,通常最初是全零或只是None
!
那就是您要么需要显式提供这样的内容:
hidden_state = torch.zeros(size=(num_layers*direction, batch_size, hidden_dim)).to(device)
pred = Gmodel(inputs, hidden_state)
或干脆做:
hidden_state = None
pred = Gmodel(inputs, hidden_state)