本文整理汇总了Python中torch.multiprocessing.Value方法的典型用法代码示例。如果您正苦于以下问题:Python multiprocessing.Value方法的具体用法?Python multiprocessing.Value怎么用?Python multiprocessing.Value使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.multiprocessing
的用法示例。
在下文中一共展示了multiprocessing.Value方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create
# 需要导入模块: from torch import multiprocessing [as 别名]
# 或者: from torch.multiprocessing import Value [as 别名]
def create(cls):
"""Singleton factory."""
if not hasattr(cls, 'length_to_eps'):
# Maps episode length to list of episodes
cls.length_to_eps = {}
# Set of episode indices already in the cache
cls.ep_indices = set()
# List of batches if popping batches
cls.batches = []
# If all episodes have been loaded into memory
cls.load_complete = Value(ctypes.c_bool, False)
# Lock to access batches
cls.batches_lock = Lock()
# Lock to access length_to_eps
cls.cache_lock = Lock()
# Lock for condition variables
cls.fill_cache_lock = RLock()
# Condition notifying Loader to add to cache
cls.add_to_cache_cv = Condition(lock=cls.fill_cache_lock)
# Condition notifying teacher that cache has episodes
cls.cache_filled_cv = Condition(lock=cls.fill_cache_lock)
示例2: __init__
# 需要导入模块: from torch import multiprocessing [as 别名]
# 或者: from torch.multiprocessing import Value [as 别名]
def __init__(self, config):
self.config = config
self.neat_config = self.load_neat_config()
self.neat_config.pop_size = config.pop_size
self.task_q = mp.SimpleQueue()
self.result_q = mp.SimpleQueue()
self.total_steps = 0
stop = mp.Value('i', False)
stats = SharedStats(config.state_dim)
normalizers = [StaticNormalizer(config.state_dim) for _ in range(config.num_workers)]
for normalizer in normalizers:
normalizer.offline_stats.load(stats)
workers = [Worker(id, normalizers[id], self.task_q, self.result_q, stop,
config, self.neat_config) for id in range(config.num_workers)]
for w in workers: w.start()
self.normalizers = normalizers
self.stats = stats
self.stop = stop
示例3: __init__
# 需要导入模块: from torch import multiprocessing [as 别名]
# 或者: from torch.multiprocessing import Value [as 别名]
def __init__(self, val=True):
self.val = mp.Value("b", False)
self.lock = mp.Lock()
示例4: __init__
# 需要导入模块: from torch import multiprocessing [as 别名]
# 或者: from torch.multiprocessing import Value [as 别名]
def __init__(self):
self.val = mp.Value('i', 0)
self.lock = mp.Lock()
示例5: __init__
# 需要导入模块: from torch import multiprocessing [as 别名]
# 或者: from torch.multiprocessing import Value [as 别名]
def __init__(self):
self.actor_step = mp.Value('l', 0)
self.learner_step = mp.Value('l', 0)
示例6: __init__
# 需要导入模块: from torch import multiprocessing [as 别名]
# 或者: from torch.multiprocessing import Value [as 别名]
def __init__(self, data):
self.lock = mp.Lock()
self.data = mp.Value("i", data)
示例7: __init__
# 需要导入模块: from torch import multiprocessing [as 别名]
# 或者: from torch.multiprocessing import Value [as 别名]
def __init__(self, owner):
super().__init__(owner)
self.item_pointer = mp.Value("l", 0)
示例8: __init__
# 需要导入模块: from torch import multiprocessing [as 别名]
# 或者: from torch.multiprocessing import Value [as 别名]
def __init__(self, distributor, collector,
piecewise=True, n_workers=16):
self.n_workers = n_workers
self.piecewise = piecewise
self.distributor = distributor
self.collector = collector
self.done = mp.Value("l", 0)
self.procs = []
示例9: __init__
# 需要导入模块: from torch import multiprocessing [as 别名]
# 或者: from torch.multiprocessing import Value [as 别名]
def __init__(self, owner):
self.owner = owner
self.read_lock = mp.Lock()
self.write_lock = mp.Lock()
self.read_count = mp.Value("l", 0)
self.read_count.value = 0
self.timestamp = mp.Value("l", 0)
self.local_timestamp = 0
示例10: __init__
# 需要导入模块: from torch import multiprocessing [as 别名]
# 或者: from torch.multiprocessing import Value [as 别名]
def __init__(self, opt: Opt, world):
super().__init__(opt)
self.inner_world = world
self.numthreads = opt['numthreads']
self.sync: Dict[str, Any] = { # syncronization primitives
# semaphores for counting queued examples
'queued_sem': Semaphore(0), # counts num exs to be processed
'threads_sem': Semaphore(0), # counts threads
'reset_sem': Semaphore(0), # allows threads to reset
# flags for communicating with threads
'reset_flag': Value('b', False), # threads should reset
'term_flag': Value('b', False), # threads should terminate
# counters
'epoch_done_ctr': Value('i', 0), # number of done threads
'total_parleys': Value('l', 0), # number of parleys in threads
}
self.threads: List[HogwildProcess] = []
for i in range(self.numthreads):
self.threads.append(HogwildProcess(i, opt, world.share(), self.sync))
time.sleep(0.05) # delay can help prevent deadlock in thread launches
for t in self.threads:
t.start()
for _ in self.threads:
# wait for threads to launch
# this makes sure that no threads get examples before all are set up
# otherwise they might reset one another after processing some exs
self.sync['threads_sem'].acquire() # type: ignore
logging.info(f'{self.numthreads} threads initialized')
示例11: __init__
# 需要导入模块: from torch import multiprocessing [as 别名]
# 或者: from torch.multiprocessing import Value [as 别名]
def __init__(self, opt, world):
super().__init__(opt)
self.inner_world = world
self.numthreads = opt['numthreads']
self.sync = { # syncronization primitives
# semaphores for counting queued examples
'queued_sem': Semaphore(0), # counts num exs to be processed
'threads_sem': Semaphore(0), # counts threads
'reset_sem': Semaphore(0), # allows threads to reset
# flags for communicating with threads
'reset_flag': Value('b', False), # threads should reset
'term_flag': Value('b', False), # threads should terminate
# counters
'epoch_done_ctr': Value('i', 0), # number of done threads
'total_parleys': Value('l', 0), # number of parleys in threads
}
self.threads = []
for i in range(self.numthreads):
self.threads.append(HogwildProcess(i, opt, world.share(), self.sync))
time.sleep(0.05) # delay can help prevent deadlock in thread launches
for t in self.threads:
t.start()
for _ in self.threads:
# wait for threads to launch
# this makes sure that no threads get examples before all are set up
# otherwise they might reset one another after processing some exs
self.sync['threads_sem'].acquire()
print(f'[ {self.numthreads} threads initialized ]')
示例12: __init__
# 需要导入模块: from torch import multiprocessing [as 别名]
# 或者: from torch.multiprocessing import Value [as 别名]
def __init__(self, args, env_prototype, model_prototype, memory_prototype):
super(ACERAgent, self).__init__(args, env_prototype, model_prototype, memory_prototype)
self.logger.warning("<===================================> ACER-Master {Env(dummy) & Model}")
# dummy_env just to get state_shape & action_dim
self.dummy_env = self.env_prototype(self.env_params, self.num_processes)
self.state_shape = self.dummy_env.state_shape
self.action_dim = self.dummy_env.action_dim
del self.dummy_env
# global shared model
self.model_params.state_shape = self.state_shape
self.model_params.action_dim = self.action_dim
self.model = self.model_prototype(self.model_params)
self._load_model(self.model_file) # load pretrained model if provided
self.model.share_memory() # NOTE
# learning algorithm # TODO: could also linearly anneal learning rate
self.optimizer = self.optim(self.model.parameters(), lr = self.lr)
self.optimizer.share_memory() # NOTE
self.lr_adjusted = mp.Value('d', self.lr) # adjusted lr
# global shared average model: for 1st order trpo policy update
self.avg_model = self.model_prototype(self.model_params)
self.avg_model.load_state_dict(self.model.state_dict())
self.avg_model.share_memory() # NOTE
for param in self.avg_model.parameters(): param.requires_grad = False
# global counters
self.frame_step = mp.Value('l', 0) # global frame step counter
self.train_step = mp.Value('l', 0) # global train step counter
self.on_policy_train_step = mp.Value('l', 0) # global on-policy train step counter
self.off_policy_train_step = mp.Value('l', 0) # global off-policy train step counter
# global training stats
self.p_loss_avg = mp.Value('d', 0.) # global policy loss
self.v_loss_avg = mp.Value('d', 0.) # global value loss
self.entropy_loss_avg = mp.Value('d', 0.) # global value loss
self.loss_counter = mp.Value('l', 0) # storing this many losses
self._reset_training_loggings()
示例13: __init__
# 需要导入模块: from torch import multiprocessing [as 别名]
# 或者: from torch.multiprocessing import Value [as 别名]
def __init__(self, args, env_prototype, model_prototype, memory_prototype):
super(A3CAgent, self).__init__(args, env_prototype, model_prototype, memory_prototype)
self.logger.warning("<===================================> A3C-Master {Env(dummy) & Model}")
# dummy_env just to get state_shape & action_dim
self.dummy_env = self.env_prototype(self.env_params, self.num_processes)
self.state_shape = self.dummy_env.state_shape
self.action_dim = self.dummy_env.action_dim
del self.dummy_env
# global shared model
self.model_params.state_shape = self.state_shape
self.model_params.action_dim = self.action_dim
self.model = self.model_prototype(self.model_params)
self._load_model(self.model_file) # load pretrained model if provided
self.model.share_memory() # NOTE
# learning algorithm
self.optimizer = self.optim(self.model.parameters(), lr = self.lr)
self.optimizer.share_memory() # NOTE
self.lr_adjusted = mp.Value('d', self.lr) # adjusted lr
# global counters
self.frame_step = mp.Value('l', 0) # global frame step counter
self.train_step = mp.Value('l', 0) # global train step counter
# global training stats
self.p_loss_avg = mp.Value('d', 0.) # global policy loss
self.v_loss_avg = mp.Value('d', 0.) # global value loss
self.loss_avg = mp.Value('d', 0.) # global loss
self.loss_counter = mp.Value('l', 0) # storing this many losses
self._reset_training_loggings()
示例14: __init__
# 需要导入模块: from torch import multiprocessing [as 别名]
# 或者: from torch.multiprocessing import Value [as 别名]
def __init__(self):
self.val = mp.Value('i', 0)
self.lock = mp.Lock()
示例15: create
# 需要导入模块: from torch import multiprocessing [as 别名]
# 或者: from torch.multiprocessing import Value [as 别名]
def create(cls):
if not hasattr(cls, 'length_to_eps'):
# Maps episode length to list of episodes
cls.length_to_eps = {}
if not hasattr(cls, 'ep_indices'):
# Set of episode indices already in the cache
cls.ep_indices = set()
if not hasattr(cls, 'batches'):
# List of batches if popping batches
cls.batches = []
if not hasattr(cls, 'load_complete'):
# If all episodes have been loaded into memory
cls.load_complete = Value(ctypes.c_bool, False)
if not hasattr(cls, 'batches_lock'):
# Lock to access batches
cls.batches_lock = Lock()
if not hasattr(cls, 'cache_lock'):
# Lock to access length_to_eps
cls.cache_lock = Lock()
if not hasattr(cls, 'fill_cache_lock'):
# Lock for condition variables
cls.fill_cache_lock = RLock()
if not hasattr(cls, 'add_to_cache_cv'):
# Condition notifying Loader to add to cache
cls.add_to_cache_cv = Condition(lock=cls.fill_cache_lock)
if not hasattr(cls, 'cache_filled_cv'):
# Condition notifying teacher that cache has episodes
cls.cache_filled_cv = Condition(lock=cls.fill_cache_lock)