Merge pull request #3898 from R-N/lr-comma
Allow trailing comma in learning rate
This commit is contained in:
commit
3dc9a43f7e
1 changed files with 21 additions and 14 deletions
|
@ -4,30 +4,37 @@ import tqdm
|
||||||
class LearnScheduleIterator:
|
class LearnScheduleIterator:
|
||||||
def __init__(self, learn_rate, max_steps, cur_step=0):
|
def __init__(self, learn_rate, max_steps, cur_step=0):
|
||||||
"""
|
"""
|
||||||
specify learn_rate as "0.001:100, 0.00001:1000, 1e-5:10000" to have lr of 0.001 until step 100, 0.00001 until 1000, 1e-5:10000 until 10000
|
specify learn_rate as "0.001:100, 0.00001:1000, 1e-5:10000" to have lr of 0.001 until step 100, 0.00001 until 1000, and 1e-5 until 10000
|
||||||
"""
|
"""
|
||||||
|
|
||||||
pairs = learn_rate.split(',')
|
pairs = learn_rate.split(',')
|
||||||
self.rates = []
|
self.rates = []
|
||||||
self.it = 0
|
self.it = 0
|
||||||
self.maxit = 0
|
self.maxit = 0
|
||||||
for i, pair in enumerate(pairs):
|
try:
|
||||||
tmp = pair.split(':')
|
for i, pair in enumerate(pairs):
|
||||||
if len(tmp) == 2:
|
if not pair.strip():
|
||||||
step = int(tmp[1])
|
continue
|
||||||
if step > cur_step:
|
tmp = pair.split(':')
|
||||||
self.rates.append((float(tmp[0]), min(step, max_steps)))
|
if len(tmp) == 2:
|
||||||
self.maxit += 1
|
step = int(tmp[1])
|
||||||
if step > max_steps:
|
if step > cur_step:
|
||||||
|
self.rates.append((float(tmp[0]), min(step, max_steps)))
|
||||||
|
self.maxit += 1
|
||||||
|
if step > max_steps:
|
||||||
|
return
|
||||||
|
elif step == -1:
|
||||||
|
self.rates.append((float(tmp[0]), max_steps))
|
||||||
|
self.maxit += 1
|
||||||
return
|
return
|
||||||
elif step == -1:
|
else:
|
||||||
self.rates.append((float(tmp[0]), max_steps))
|
self.rates.append((float(tmp[0]), max_steps))
|
||||||
self.maxit += 1
|
self.maxit += 1
|
||||||
return
|
return
|
||||||
else:
|
assert self.rates
|
||||||
self.rates.append((float(tmp[0]), max_steps))
|
except (ValueError, AssertionError):
|
||||||
self.maxit += 1
|
raise Exception('Invalid learning rate schedule. It should be a number or, for example, like "0.001:100, 0.00001:1000, 1e-5:10000" to have lr of 0.001 until step 100, 0.00001 until 1000, and 1e-5 until 10000.')
|
||||||
return
|
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return self
|
return self
|
||||||
|
|
Loading…
Reference in a new issue