fallback to hparams str (#2259)

This commit is contained in:
William Falcon 2020-06-19 00:49:40 -04:00 committed by GitHub
parent 57d5f6e74a
commit 81720d9ee5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 6 deletions

View File

@ -1702,11 +1702,14 @@ class LightningModule(ABC, DeviceDtypeModuleMixin, GradInformation, ModelIO, Mod
looks at the code of the class to figure out what the user named self.hparams
this only happens when the user explicitly sets self.hparams
"""
class_code = inspect.getsource(self.__class__)
lines = class_code.split('\n')
for line in lines:
line = re.sub(r"\s+", "", line, flags=re.UNICODE)
if 'self.hparams=' in line:
return line.split('=')[1]
try:
class_code = inspect.getsource(self.__class__)
lines = class_code.split('\n')
for line in lines:
line = re.sub(r"\s+", "", line, flags=re.UNICODE)
if 'self.hparams=' in line:
return line.split('=')[1]
except Exception as e:
return 'hparams'
return None