python - 无法插入需要 grad 作为常量的张量
问题描述
我在 pytorch 上使用 pyro 制作了一个模型。但是,我收到以下错误:
h_0_contig = self.h_0.expand(1,size,self.rnn.hidden_size)
类型错误:expand():参数“大小”必须是整数元组,但在位置 2 找到了张量类型的元素
我认为这是因为我将张量作为输入。然后我明确地将它作为一个整数给出,然后它给了我以下错误:
RuntimeError:无法插入需要 grad 作为常量的张量。考虑将其作为参数或输入,或分离梯度
张量:
(1,.,.) = 列 1 到 26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
我在下面显示了我的代码,其中出现了错误(功能指南)。mini_batch、mini_batch_reversed、mini_batch_mask 和退火因子,它们都是张量。我正在使用以下代码:
https://github.com/pyro-ppl/pyro/blob/dev/examples/dmm/dmm.py
我不知道是什么导致了这个错误。见解将不胜感激。
def guide(self,mini_batch,mini_batch_reversed,mini_batch_mask,annealing_factor = torch.tensor(1.0)):
#T_max = mini_batch.size(1)
T_max = 247
pyro.module('DMM',self)
#size = mini_batch.size(0)
size = 5
print('type of size : ',type(size))
print('rnn hidden size : ',self.rnn.hidden_size)
h_0_contig = self.h_0.expand(1,size,self.rnn.hidden_size)
rnn_output,_ = self.rnn(mini_batch_reversed,h_0_contig)
rnn_output = dp.pad_and_reverse(rnn_output, mini_batch.size(1))
z_prev = self.z_q_0.expand(mini_batch.size(0),self.z_q_0.size(0))
with pyro.plate('z_minibatch',len(mini_batch)):
for t in range(1,T_max+1):
z_loc,z_scale = self.combiner(z_prev,rnn_output[:,t-1,:])
z_dist = dist.Normal(z_loc,z_scale)
with pyro.poutine.scale(None,annealing_factor):
z_t = pyro.sample("z_%d" % t,z_dist.mask(mini_batch_mask[:, t - 1:t]).to_event(1))
z_prev = z_t
解决方案
推荐阅读
- javascript - 当这个 prop 在组件内部时,在 Hoc 函数中传递一个 prop
- google-bigquery - 从 Firebase Analytics 导入时更改新架构后 BigQuery 中只有空值
- http - HTTP HEAD 替代方案
- c++ - GL_BLEND 不能与顶点数组一起正常工作
- c++ - delete[] 的析构函数问题
- android - 外投影类型禁止使用方法
- azure - VMSS 自定义映像的优缺点
- angular - 在 Edge 中渲染 PDF,在嵌入中渲染 IE11
- javascript - 在 React 中播放依赖于状态的音频文件
- c# - Microsoft.CognitiveServices.Speech.SpeechRecognizer - 获取文件中结果的时间偏移,持续识别