为什么有些人会链接两个不同网络的参数,并使用相同的优化程序对其进行训练?

问题描述 投票:2回答:1

我正在研究CycleGAN的官方pytorch实现,在那里,作者链接了这两个网络的参数,并对这两个网络使用了单个优化程序。这是如何运作的?是否比在两个不同的网络中使用两个不同的优化器更好?

all_params = chain(module_a.parameters(), module_b.parameters())
optimizer = torch.optim.Adam(all_params)
python deep-learning pytorch generative-adversarial-network gan
1个回答
1
投票

摘自chain文档:https://docs.python.org/3/library/itertools.html#itertools.chain

itertools.chain(*iterables)

    Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted.

由于parameters()为您提供了迭代,您可以使用优化器同时优化两个网络的参数。因此,两个模型都将使用相同的优化器状态(Modules),如果使用两个不同的优化器,则将分别优化参数。

如果您有一个复合网络,则有必要同时优化(所有)参数,因此,对所有参数使用单个优化器是必经之路。

© www.soinside.com 2019 - 2024. All rights reserved.