-
Notifications
You must be signed in to change notification settings - Fork 0
Composite Sampler #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Composite Sampler #250
Changes from all commits
1d4cbaf
8dfbc8a
3535b0f
e279769
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from __future__ import annotations | ||
|
||
import torch | ||
|
||
from confopt.oneshot.archsampler import BaseSampler | ||
from confopt.oneshot.base import OneShotComponent | ||
|
||
|
||
class CompositeSampler(OneShotComponent): | ||
def __init__( | ||
self, | ||
arch_samplers: list[BaseSampler], | ||
arch_parameters: list[torch.Tensor], | ||
) -> None: | ||
super().__init__() | ||
self.arch_samplers = arch_samplers | ||
self.arch_parameters = arch_parameters | ||
|
||
# get sample frequency from the samplers | ||
self.sample_frequency = arch_samplers[0].sample_frequency | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still use sampling frequency in the code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, we never change it to epoch in our experiments, but we have option to do that. I'm not sure how handy this can come later, but I think it doesn't hurt to leave it there |
||
for sampler in arch_samplers: | ||
assert ( | ||
self.sample_frequency == sampler.sample_frequency | ||
), "All samplers must have the same sample frequency" | ||
|
||
def sample(self, alpha: torch.Tensor) -> torch.Tensor: | ||
sampled_alphas = alpha | ||
for sampler in self.arch_samplers: | ||
sampled_alphas = sampler.sample(sampled_alphas) | ||
|
||
return sampled_alphas | ||
|
||
def new_epoch(self) -> None: | ||
super().new_epoch() | ||
for sampler in self.arch_samplers: | ||
sampler.new_epoch() | ||
|
||
def new_step(self) -> None: | ||
super().new_step() | ||
for sampler in self.arch_samplers: | ||
sampler.new_step() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
) | ||
from confopt.oneshot.archsampler import ( | ||
BaseSampler, | ||
CompositeSampler, | ||
DARTSSampler, | ||
DRNASSampler, | ||
GDASSampler, | ||
|
@@ -374,17 +375,36 @@ def _set_sampler( | |
config: dict, | ||
) -> None: | ||
arch_params = self.search_space.arch_parameters | ||
self.sampler: BaseSampler | None = None | ||
if sampler == SamplerType.DARTS: | ||
self.sampler = DARTSSampler(**config, arch_parameters=arch_params) | ||
elif sampler == SamplerType.DRNAS: | ||
self.sampler = DRNASSampler(**config, arch_parameters=arch_params) | ||
elif sampler == SamplerType.GDAS: | ||
self.sampler = GDASSampler(**config, arch_parameters=arch_params) | ||
elif sampler == SamplerType.SNAS: | ||
self.sampler = SNASSampler(**config, arch_parameters=arch_params) | ||
elif sampler == SamplerType.REINMAX: | ||
self.sampler = ReinMaxSampler(**config, arch_parameters=arch_params) | ||
self.sampler: BaseSampler | CompositeSampler | None = None | ||
|
||
def _get_sampler_class(sampler: SamplerType) -> Callable: | ||
if sampler == SamplerType.DARTS: | ||
return DARTSSampler | ||
if sampler == SamplerType.DRNAS: | ||
return DRNASSampler | ||
if sampler == SamplerType.GDAS: | ||
return GDASSampler | ||
if sampler == SamplerType.SNAS: | ||
return SNASSampler | ||
if sampler == SamplerType.REINMAX: | ||
return ReinMaxSampler | ||
|
||
raise ValueError(f"Illegal sampler {sampler} provided") | ||
|
||
if sampler == SamplerType.COMPOSITE: | ||
sub_samplers: list[BaseSampler] = [] | ||
for _, sampler_config in config.items(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm not mistaken, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think with python versions 3.7 and above, the insertion order is preserved |
||
sampler_type = sampler_config["sampler_type"] | ||
del sampler_config["sampler_type"] | ||
sampler_component = _get_sampler_class(sampler_type)( | ||
**sampler_config, arch_parameters=arch_params | ||
) | ||
sub_samplers.append(sampler_component) | ||
self.sampler = CompositeSampler(sub_samplers, arch_parameters=arch_params) | ||
else: | ||
self.sampler = _get_sampler_class(sampler)( | ||
**config, arch_parameters=arch_params | ||
) | ||
|
||
def _set_perturbator( | ||
self, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why inherit from
OneShotComponent
? Why not inherit fromBaseSampler
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was in 2 minds with it as well, but for me, functionality wise- I don't see it as a child of BaseSampler, since it takes a list of BaseSampler within its initialisation which i thought is a special case. So I thought a OneShotComponent is a better option for it. Let me know your opinion about it.