如何向Python生成器对象添加新元素?

时间:2020-07-30 16:55:34

标签: python generator itertools pyro

我正在尝试将新项目(torch.tensor([0]))添加到Python生成器对象model_RobertaForMultipleChoice.parameters()。我尝试通过使用list中的chainitertoools来完成任务,但是无法将新项目添加到生成器对象中。我的代码如下所示:

from transformers import RobertaTokenizer, RobertaForMultipleChoice, AdamW, get_constant_schedule
import pyro
import pyro.infer
import pyro.optim
import pyro.distributions as dist
import pyro.nn.module as module
import pyro.infer.autoguide.guides as guides
from torch import nn
from pyro.optim import Adam
from pyro.infer import SVI
from pyro.infer import Trace_ELBO
from numpy import nan
from itertools import chain
import torch

# get the pre-trained HuggingFace RobertaForMultipleChoice and resize the token embeddings after adding the special token

torch.device('cpu')
model_RobertaForMultipleChoice = RobertaForMultipleChoice.from_pretrained('Roberta-base', output_hidden_states = True)
        
module.to_pyro_module_(model_RobertaForMultipleChoice)

# Now we can attempt to be fully Bayesian:
for m in model_RobertaForMultipleChoice.modules():
   for name, value in list(m.named_parameters(recurse=False)):

      options =  dict(dtype=float, device="cpu")
      prior_loc = torch.zeros(1, 1, **options)
      prior_scale = torch.ones(1, 1, **options)
      zs = module.PyroSample(dist.Normal(prior_loc, prior_scale).to_event(1))
                
      setattr(m, name, zs)

# model_RobertaForMultipleChoice.parameters() is a generator object
[x for x in (y for y in model_RobertaForMultipleChoice.parameters())]
# Out: []

g=(torch.tensor([0])) # object I want to add to the generator

# add g to the generator object model_RobertaForMultipleChoice.parameters()
model_RobertaForMultipleChoice.parameters() = list(chain([x for x in (y for y in model_RobertaForMultipleChoice.parameters())], g))
# Out: 
    File "<ipython-input-58-ad6f05a8e796>", line 1
    model_RobertaForMultipleChoice.parameters() = list(chain([x for x in (y for y in model_RobertaForMultipleChoice.parameters())], g))
                                                                                                                                             ^
SyntaxError: can't assign to function call

我在做什么错了?

谢谢

0 个答案:

没有答案
相关问题