【问题标题】:My Google Colab keeps crashing because of something in this code. Not sure what it is由于此代码中的某些内容,我的 Google Colab 不断崩溃。不确定它是什么
【发布时间】:2020-03-15 14:25:47
【问题描述】:

我的 google colab 总是在火车上崩溃,即使 RAM 和磁盘足够多。我认为问题出在这段代码中,但我不知道它是什么。我正在做 LSTM。我会很感激任何帮助。我正在使用 PyTorch。

此代码后面是生成函数、编码器解码器类等。当我开始训练时,它崩溃了(由于“未知原因”)

class LSTMLM(torch.nn.Module):
  def __init__(self,
              vocab_size,
              embedding_size,
              hidden_size,
              num_layers=1,
              dropout=0.1):
    super().__init__()
    self.vocab_size = vocab_size
    self.embedding_size = embedding_size
    self.hidden_size = hidden_size
    self.num_layers = num_layers
    self.dropout = torch.nn.Dropout(dropout)

    self.embedding = torch.nn.Embedding(vocab_size, embedding_size)

    self.lstm = torch.nn.LSTM(input_size=hidden_size, hidden_size=hidden_size, num_layers=num_layers, batch_first=True)

    self.logistl = torch.nn.Linear(hidden_size, vocab_size)

    pass

  def forward(self, x, init_hidden_state=None):
    assert x.shape[0] == 1

    emb = self.embedding(x)

    emb = self.dropout(emb)

    if init_hidden_state is None:
      h0 = torch.zeros(self.num_layers, 1, self.hidden_size)
      c0 = torch.zeros(self.num_layers, 1, self.hidden_size)
    else:
      h0, c0 = init_hidden_state
    output, (hn, hc) = self.lstm(emb, (h0, c0))

    hidden_states = output
    final_hidden_state = hn
    final_cell_state = cn
    final_state = [final_hidden__state, final_cell_state]

    hidden_states = self.dropout(hidden_states) 

    output_dist = self.logistl(hidden_states)

    return output_dist, hidden_states, final_state 

【问题讨论】:

  • 如果没有您的数据,将很难有人对此进行调试。我建议删除代码行(并返回 np.zeros() 之类的占位符变量),直到您确切看到导致崩溃的行。
  • 我没有看到任何会导致 Colab 崩溃的明显错误。我认为,当您创建 LSTM 时,您应该拥有 input_size=embedding_size 但这不应该使 Colab 崩溃,如果它不正确,它应该会给您一个堆栈跟踪(如果您的 embedding_sizehidden_size 相同,它应该仍然工作)。
  • 请分享一个能够重现崩溃的独立笔记本。
  • @superbot 您应该考虑将笔记本链接添加到您的问题中,以便每个人都能立即看到。因为现在带有您的链接的评论将被隐藏,直到您点击“显示所有 cmets”。

标签: pytorch lstm google-colaboratory


【解决方案1】:

解决方案是在定义每个张量后添加 cuda()。

例如

a = a.cuda()

请注意,您必须分配它,而不是只做a.cuda()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 2021-07-17
    相关资源
    最近更新 更多