Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
print('* Number of trainable parameters:', tally_parameters(model))
epoch_str = 'Epoch {0:d}'.format(epoch + 1)
print('===> ', run_type, epoch_str)
batch_end = time.time()
# The tqdm-bar for Jupyter notebook is under development.
if progress_style == 'tqdm-bar':
pbar = tqdm(
total=len(run_iter) // log_freq,
bar_format='{l_bar}{bar}{postfix}',
file=sys.stdout)
# Use the pyprind bar as the default progress bar.
if progress_style == 'bar':
pbar = pyprind.ProgBar(len(run_iter) // log_freq, bar_char='█', width=30)
for batch_idx, batch in enumerate(run_iter):
batch_start = time.time()
datatime += batch_start - batch_end
output = model(batch)
# from torchviz import make_dot, make_dot_from_trace
# dot = make_dot(output.mean(), params=dict(model.named_parameters()))
# pdb.set_trace()
loss = float('NaN')
if criterion:
loss = criterion(output, getattr(batch, label_attr))
if hasattr(batch, label_attr):
import sys
import pyprind
n = 1000000
mbar = pyprind.ProgBar(n, stream=sys.stdout)
for i in range(n):
mbar.update()
mper = pyprind.ProgPercent(n, stream=sys.stdout)
for i in range(n):
mper.update()
mbar2 = pyprind.ProgBar(n, stream='test')
for i in range(n):
mbar2.update()
for i in pyprind.prog_bar(range(n), stream=sys.stdout):
# do something
pass
for i in pyprind.prog_percent(range(n), stream=sys.stdout):
# do something
pass
for i in pyprind.prog_bar(range(n), stream='test'):
print('%s\n' % (80 * '='))
print('Testing Basic Percentage Indicator\n')
n = 100
sleeptime = 0.02
perc = pyprind.ProgPercent(n)
for i in range(n):
time.sleep(sleeptime)
perc.update()
print('\n%s' % (80 * '='))
print('%s\n' % (80 * '='))
print('Testing stdout Stream\n')
perc = pyprind.ProgPercent(n, stream=sys.stdout)
for i in range(n):
time.sleep(sleeptime)
perc.update()
print('\n%s' % (80 * '='))
print('%s\n' % (80 * '='))
print('Testing Percentage Indicator Generator\n')
for i in pyprind.prog_percent(range(n), stream=sys.stdout):
time.sleep(sleeptime)
print('\n%s' % (80 * '='))
print('%s\n' % (80 * '='))
print('Testing monitor function\n')
def test_generator():
for i in pyprind.prog_bar(range(n), stream=sys.stdout):
time.sleep(sleeptime)
import pyprind
n = 1000000
mbar = pyprind.ProgBar(n, stream=sys.stdout)
for i in range(n):
mbar.update()
mper = pyprind.ProgPercent(n, stream=sys.stdout)
for i in range(n):
mper.update()
mbar2 = pyprind.ProgBar(n, stream='test')
for i in range(n):
mbar2.update()
for i in pyprind.prog_bar(range(n), stream=sys.stdout):
# do something
pass
for i in pyprind.prog_percent(range(n), stream=sys.stdout):
# do something
pass
for i in pyprind.prog_bar(range(n), stream='test'):
# do something
pass
for i in range(n):
mbar.update()
mper = pyprind.ProgPercent(n, stream=sys.stdout)
for i in range(n):
mper.update()
mbar2 = pyprind.ProgBar(n, stream='test')
for i in range(n):
mbar2.update()
for i in pyprind.prog_bar(range(n), stream=sys.stdout):
# do something
pass
for i in pyprind.prog_percent(range(n), stream=sys.stdout):
# do something
pass
for i in pyprind.prog_bar(range(n), stream='test'):
# do something
pass
perc.update()
print('\n%s' % (80 * '='))
print('%s\n' % (80 * '='))
print('Testing stdout Stream\n')
perc = pyprind.ProgPercent(n, stream=sys.stdout)
for i in range(n):
time.sleep(sleeptime)
perc.update()
print('\n%s' % (80 * '='))
print('%s\n' % (80 * '='))
print('Testing Percentage Indicator Generator\n')
for i in pyprind.prog_percent(range(n), stream=sys.stdout):
time.sleep(sleeptime)
print('\n%s' % (80 * '='))
print('%s\n' % (80 * '='))
print('Testing monitor function\n')
perc = pyprind.ProgPercent(n, monitor=True)
for i in range(n):
time.sleep(sleeptime)
perc.update()
print(perc)
print('\n%s' % (80 * '='))
print('%s\n' % (80 * '='))
log (boolean): whether to log sampling times
log_prefix (str) : prefix for logger
Returns:
(dict) : A dict of paths of size [meta_batch_size] x (batch_size) x [5] x (max_path_length)
"""
# initial setup / preparation
paths = OrderedDict()
for i in range(self.meta_batch_size):
paths[i] = []
n_samples = 0
running_paths = [_get_empty_running_paths_dict() for _ in range(self.vec_env.num_envs)]
pbar = ProgBar(self.total_samples)
policy_time, env_time = 0, 0
policy = self.policy
policy.reset(dones=[True] * self.meta_batch_size)
# initial reset of envs
obses = self.vec_env.reset()
while n_samples < self.total_samples:
# execute policy
t = time.time()
obs_per_task = np.split(np.asarray(obses), self.meta_batch_size)
actions, agent_infos = policy.get_actions(obs_per_task)
policy_time += time.time() - t
try:
with io.open(emb_file, encoding="utf8") as f:
lines = [line for line in f]
# If there are malformed lines, read in binary mode
# and manually decode each word from utf-8
except:
logger.warning("Could not read {} as UTF8 file, "
"reading file as bytes and skipping "
"words with malformed UTF8.".format(emb_file))
with open(emb_file, 'rb') as f:
lines = [line for line in f]
binary_lines = True
logger.info("Loading vectors from {}".format(emb_file))
process_bar = pyprind.ProgPercent(len(lines))
for line in lines:
process_bar.update()
# Explicitly splitting on " " is important, so we don't
# get rid of Unicode non-breaking spaces in the vectors.
entries = line.rstrip().split(b" " if binary_lines else " ")
word, entries = entries[0], entries[1:]
if dim is None and len(entries) > 1:
dim = len(entries)
# add pad_word
itos.append(pad_word)
vectors.extend(np.zeros(dim, ))
# add unk_word
itos.append(unk_word)
vectors.extend(np.random.uniform(-0.25, 0.25, (dim, )))