# Custom optimizer got an empty parameter list

**URL:** <https://discuss.pytorch.org/t/custom-optimizer-got-an-empty-parameter-list/76917>\
**Category:** Uncategorized\
**Created:** [April 15, 2020, 8:02pm UTC](https://discuss.pytorch.org/t/custom-optimizer-got-an-empty-parameter-list/76917 "2020-04-15T20:02:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZeroMax](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/z/6de8d8/32.png) [@ZeroMax](https://discuss.pytorch.org/u/ZeroMax)\
**Post date:** [April 15, 2020, 8:02pm UTC](https://discuss.pytorch.org/t/custom-optimizer-got-an-empty-parameter-list/76917/1 "2020-04-15T20:02:16Z")

</div>

Hello, new here. I am trying to create a custom optimizer in PyTorch, where the backprop takes place in a meta RL policy. However, I am seeing the above error. My models work fine on Adam and SGD, but not my optimizer.

Code:

```auto
class MetaBackProp(torch.optim.Optimizer):
	def __init__ (self, params):
		
		self.param_shape_list = np.array([])
		for param in list(params):
			np.append(self.param_shape_list, list(param.size()))

		pseudo_lr = 1e-4
		pseudo_defaults = dict(lr=pseudo_lr)
		length = 100 #TODO: get shape, flatten, multiply...
		self.policy = AEPolicy(length)
		self.policy_optim = torch.optim.Adam(self.policy.parameters(), lr=pseudo_lr)
		super(MetaBackProp, self). __init__ (params, pseudo_defaults)

	def step(self, closure=None):
		params = torch.cat([p.view(-1) for p in self.param_groups])
		self.policy_optim.zero_grad()
		quit()

```

Traceback:

```auto
Traceback (most recent call last):
  File "main.py", line 6, in <module>
    gan = CycleGAN()
  File "/home/ai/Projects_v2/R/cycle_gan.py", line 32, in __init__
    self.discriminator2_optim = MetaBackProp(self.discriminator2.parameters())
  File "/home/ai/Projects_v2/R/lr_schedule.py", line 34, in __init__
    super(MetaBackProp, self). __init__ (params, pseudo_defaults)
  File "/home/ai/anaconda3/lib/python3.7/site-packages/torch/optim/optimizer.py", line 46, in __init__
    raise ValueError("optimizer got an empty parameter list")
ValueError: optimizer got an empty parameter list

```

---

<div class="post-metadata">

**Author:** ![JuanFMontesinos](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/juanfmontesinos/32/76115_2.png) [@JuanFMontesinos](https://discuss.pytorch.org/u/JuanFMontesinos)\
**Post date:** [April 16, 2020, 12:45am UTC](https://discuss.pytorch.org/t/custom-optimizer-got-an-empty-parameter-list/76917/2 "2020-04-16T00:45:17Z")

</div>

Once you call list(params) you are exhausting the generator. Thus, when you call super init, it’s empty.
