- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现“Livelinet:用于预测教育视频中的活力的多模式深度循环神经网络”中的结构。
为了简单说明,我将 10 秒音频剪辑分成 10 个 1 秒音频剪辑,并从该 1 秒音频剪辑中获取频谱图(图片)。然后我使用CNN从图片中获取表示向量,最终得到每个1秒视频片段的10个向量。
接下来,我将这 10 个向量输入 LSTM,但出现了一些错误。我的代码和错误回溯如下:
class AudioCNN(nn.Module):
def __init__(self):
super(AudioCNN,self).__init__()
self.features = alexnet.features
self.features2 = nn.Sequential(*classifier)
self.lstm = nn.LSTM(512, 256,2)
self.classifier = nn.Linear(2*256,2)
def forward(self, x):
x = self.features(x)
print x.size()
x = x.view(x.size(0),256*6*6)
x = self.features2(x)
x = x.view(10,1,512)
h_0,c_0 = self.init_hidden()
_, (_, _) = self.lstm(x,(h_0,c_0)) # x dim : 2 x 1 x 256
assert False
x = x.view(1,1,2*256)
x = self.classifier(x)
return x
def init_hidden(self):
h_0 = torch.randn(2,1,256) #layer * batch * input_dim
c_0 = torch.randn(2,1,256)
return h_0, c_0
audiocnn = AudioCNN()
input = torch.randn(10,3,223,223)
input = Variable(input)
audiocnn(input)
错误:
RuntimeErrorTraceback (most recent call last)
<ipython-input-64-2913316dbb34> in <module>()
----> 1 audiocnn(input)
/home//local/lib/python2.7/site-packages/torch/nn/modules/module.pyc in __call__(self, *input, **kwargs)
222 for hook in self._forward_pre_hooks.values():
223 hook(self, input)
--> 224 result = self.forward(*input, **kwargs)
225 for hook in self._forward_hooks.values():
226 hook_result = hook(self, input, result)
<ipython-input-60-31881982cca9> in forward(self, x)
15 x = x.view(10,1,512)
16 h_0,c_0 = self.init_hidden()
---> 17 _, (_, _) = self.lstm(x,(h_0,c_0)) # x dim : 2 x 1 x 256
18 assert False
19 x = x.view(1,1,2*256)
/home/local/lib/python2.7/site-packages/torch/nn/modules/module.pyc in __call__(self, *input, **kwargs)
222 for hook in self._forward_pre_hooks.values():
223 hook(self, input)
--> 224 result = self.forward(*input, **kwargs)
225 for hook in self._forward_hooks.values():
226 hook_result = hook(self, input, result)
/home//local/lib/python2.7/site-packages/torch/nn/modules/rnn.pyc in forward(self, input, hx)
160 flat_weight=flat_weight
161 )
--> 162 output, hidden = func(input, self.all_weights, hx)
163 if is_packed:
164 output = PackedSequence(output, batch_sizes)
/home//local/lib/python2.7/site-packages/torch/nn/_functions/rnn.pyc in forward(input, *fargs, **fkwargs)
349 else:
350 func = AutogradRNN(*args, **kwargs)
--> 351 return func(input, *fargs, **fkwargs)
352
353 return forward
/home//local/lib/python2.7/site-packages/torch/nn/_functions/rnn.pyc in forward(input, weight, hidden)
242 input = input.transpose(0, 1)
243
--> 244 nexth, output = func(input, hidden, weight)
245
246 if batch_first and batch_sizes is None:
/home//local/lib/python2.7/site-packages/torch/nn/_functions/rnn.pyc in forward(input, hidden, weight)
82 l = i * num_directions + j
83
---> 84 hy, output = inner(input, hidden[l], weight[l])
85 next_hidden.append(hy)
86 all_output.append(output)
/home//local/lib/python2.7/site-packages/torch/nn/_functions/rnn.pyc in forward(input, hidden, weight)
111 steps = range(input.size(0) - 1, -1, -1) if reverse else range(input.size(0))
112 for i in steps:
--> 113 hidden = inner(input[i], hidden, *weight)
114 # hack to handle LSTM
115 output.append(hidden[0] if isinstance(hidden, tuple) else hidden)
/home//local/lib/python2.7/site-packages/torch/nn/_functions/rnn.pyc in LSTMCell(input, hidden, w_ih, w_hh, b_ih, b_hh)
29
30 hx, cx = hidden
---> 31 gates = F.linear(input, w_ih, b_ih) + F.linear(hx, w_hh, b_hh)
32
33 ingate, forgetgate, cellgate, outgate = gates.chunk(4, 1)
/home//local/lib/python2.7/site-packages/torch/nn/functional.pyc in linear(input, weight, bias)
551 if input.dim() == 2 and bias is not None:
552 # fused op is marginally faster
--> 553 return torch.addmm(bias, input, weight.t())
554
555 output = input.matmul(weight.t())
/home//local/lib/python2.7/site-packages/torch/autograd/variable.pyc in addmm(cls, *args)
922 @classmethod
923 def addmm(cls, *args):
--> 924 return cls._blas(Addmm, args, False)
925
926 @classmethod
/home//local/lib/python2.7/site-packages/torch/autograd/variable.pyc in _blas(cls, args, inplace)
918 else:
919 tensors = args
--> 920 return cls.apply(*(tensors + (alpha, beta, inplace)))
921
922 @classmethod
RuntimeError: save_for_backward can only save input or output tensors, but argument 0 doesn't satisfy this condition
最佳答案
错误信息
RuntimeError: save_for_backward can only save input or output tensors, but argument 0 doesn't satisfy this condition
通常表示您正在传递张量或其他无法将历史记录作为模块的输入存储的东西。在您的情况下,您的问题是您在 init_hidden()
而不是 Variable
实例中返回张量。因此,当 LSTM 运行时,它无法计算隐藏层的梯度,因为其初始输入不是反向传播图的一部分。
解决方案:
def init_hidden(self):
h_0 = torch.randn(2,1,256) #layer * batch * input_dim
c_0 = torch.randn(2,1,256)
return Variable(h_0), Variable(c_0)
均值 0 和方差 1 作为 LSTM 隐藏状态的初始值也可能没有帮助。理想情况下,您也可以使初始状态可训练,例如:
h_0 = torch.zeros(2,1,256) # layer * batch * input_dim
c_0 = torch.zeros(2,1,256)
h_0_param = torch.nn.Parameter(h_0)
c_0_param = torch.nn.Parameter(c_0)
def init_hidden(self):
return h_0_param, c_0_param
在这种情况下,网络可以了解最适合的初始状态。请注意,在这种情况下,无需将 h_0_param
包装在 Variable
中,因为 Parameter
本质上是一个 Variable
与 require_grad=True
。
关于lstm - torch.nn.LSTM 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47347098/
我无法准确理解 LSTM 单元的范围——它如何映射到网络层。来自格雷夫斯 (2014): 在我看来,在单层网络中,layer = lstm 单元。这实际上如何在多层 rnn 中工作? 三层RNN LS
这是代码 model = Sequential() model.add(LSTM(256, input_shape=(None, 1), return_sequences=True)) model.a
为什么我们需要在pytorch中初始化LSTM中的隐藏状态h0。由于 h0 无论如何都会被计算并被覆盖?是不是很像 整合一个一 = 0 一个= 4 即使我们不做a=0,也应该没问题.. 最佳答案 重点
我正在尝试使用 LSTM 在 Deeplearning4j 中进行一些简单的时间序列预测,但我很难让它工作。 我有一个简单的文本文件,其中包含如下所示的数字列表,并希望网络学习预测下一个数字。 有没有
在大量阅读和绘制图表之后,我想我已经提出了一个模型,我可以将其用作更多测试我需要调整哪些参数和功能的基础。但是,我对如何实现以下测试用例感到困惑(所有数字都比最终模型小几个数量级,但我想从小处着手):
我正在尝试实现“Livelinet:用于预测教育视频中的活力的多模式深度循环神经网络”中的结构。 为了简单说明,我将 10 秒音频剪辑分成 10 个 1 秒音频剪辑,并从该 1 秒音频剪辑中获取频谱图
我正在 Tensorflow 中制作 LSTM 神经网络。 输入张量大小为 92。 import tensorflow as tf from tensorflow.contrib import rnn
我正在尝试 keras IMDB 数据的示例,数据形状是这样的: x_train shape: (25000, 80) 我只是把keras例子的原始代码改成了这样的代码: model = Sequen
我需要了解如何使用 torch.nn 的不同组件正确准备批量训练的输入。模块。具体来说,我希望为 seq2seq 模型创建一个编码器-解码器网络。 假设我有一个包含这三层的模块,按顺序: nn.Emb
我很难概念化 Keras 中有状态 LSTM 和无状态 LSTM 之间的区别。我的理解是,在每个批处理结束时,在无状态情况下“网络状态被重置”,而对于有状态情况,网络状态会为每个批处理保留,然后必须在
nn.Embedding() 是学习 LSTM 所必需的吗? 我在 PyTorch 中使用 LSTM 来预测 NER - 此处是类似任务的示例 - https://pytorch.org/tutori
我正在尝试找出适合我想要拟合的模型的正确语法。这是一个时间序列预测问题,我想在将时间序列输入 LSTM 之前使用一些密集层来改进时间序列的表示。 这是我正在使用的虚拟系列: import pandas
我在理解堆叠式 LSTM 网络中各层的输入-输出流时遇到了一些困难。假设我已经创建了一个如下所示的堆叠式 LSTM 网络: # parameters time_steps = 10 features
LSTM 类中的默认非线性激活函数是 tanh。我希望在我的项目中使用 ReLU。浏览文档和其他资源,我无法找到一种简单的方法来做到这一点。我能找到的唯一方法是定义我自己的自定义 LSTMCell,但
在 PyTorch 中,有一个 LSTM 模块,除了输入序列、隐藏状态和单元状态之外,它还接受 num_layers 参数,该参数指定我们的 LSTM 有多少层。 然而,还有另一个模块 LSTMCel
没什么好说的作为介绍:我想在 TensorFlow 中将 LSTM 堆叠在另一个 LSTM 上,但一直被错误阻止,我不太明白,更不用说单独解决了。 代码如下: def RNN(_X, _istate,
有人可以解释一下吗?我知道双向 LSTM 具有前向和反向传递,但是与单向 LSTM 相比,它有什么优势? 它们各自更适合什么? 最佳答案 LSTM 的核心是使用隐藏状态保留已经通过它的输入信息。 单向
我想构建一个带有特殊词嵌入的 LSTM,但我对它的工作原理有一些疑问。 您可能知道,一些 LSTM 对字符进行操作,因此它是字符输入,字符输出。我想做同样的事情,通过对单词的抽象来学习使用嵌套的 LS
我编写了一个LSTM回归模型。它是最后一个LSTM层的BATCH_SIZE=1和RETURN_Sequence=True的模型。我还设置了VERIFICATION_DATA和耐心进行培训。但似乎存在一
给定一个训练有素的 LSTM 模型,我想对单个时间步执行推理,即以下示例中的 seq_length = 1。在每个时间步之后,需要为下一个“批处理”记住内部 LSTM(内存和隐藏)状态。在推理的最开始
我是一名优秀的程序员,十分优秀!