Skip to content

Commit

Permalink
Adding description for Optimizers (#4371)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishwakftw authored and apaszke committed Dec 28, 2017
1 parent 5c33400 commit 89acc10
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions test/test_optim.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def _test_rosenbrock_sparse(self, constructor, sparse_only=False):

params = Variable(params_t, requires_grad=True)
optimizer = constructor([params])

if not sparse_only:
params_c = Variable(params_t.clone(), requires_grad=True)
optimizer_c = constructor([params_c])
Expand Down Expand Up @@ -113,6 +114,9 @@ def _test_basic_cases_template(self, weight, bias, input, constructor):
input = Variable(input)
optimizer = constructor(weight, bias)

# to check if the optimizer can be printed as a string
optimizer.__repr__()

def fn():
optimizer.zero_grad()
y = weight.mv(input)
Expand Down Expand Up @@ -177,6 +181,7 @@ def fn_base(optimizer, weight, bias):
state_dict = deepcopy(optimizer.state_dict())
state_dict_c = deepcopy(optimizer.state_dict())
optimizer_cuda.load_state_dict(state_dict_c)

# Make sure state dict wasn't modified
self.assertEqual(state_dict, state_dict_c)

Expand Down
11 changes: 11 additions & 0 deletions torch/optim/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ def __getstate__(self):
def __setstate__(self, state):
self.__dict__.update(state)

def __repr__(self):
format_string = self.__class__.__name__ + ' ('
for i, group in enumerate(self.param_groups):
format_string += '\n'
format_string += 'Parameter Group {0}\n'.format(i)
for key in sorted(group.keys()):
if key != 'params':
format_string += ' {0}: {1}\n'.format(key, group[key])
format_string += ')'
return format_string

def state_dict(self):
"""Returns the state of the optimizer as a :class:`dict`.
Expand Down

0 comments on commit 89acc10

Please sign in to comment.