Reputation: 13088
Given the string
optimizer = "tensorflow.train.ProximalAdagradOptimizer"
How can I achieve the following:
import tensorflow
optimizer = tensorflow.train.ProximalAdagradOptimizer()
To add context to my specific use case (to address one of the comments): I'm trying to create a text-based config that describes how my model (specifically: estimator) was configured. That way I can easily re-instantiate them after training if I want to train it more or do other stuff with them. I haven't found a simple way to do this; I'm not looking for a saved_model for this. My use case is to easily reload models prior to committing them to a saved_model state. The config would look something like this:
model_config = {
"type": "DNNClassifier",
"n_classes": 10,
"feature_columns": [
{
"numeric_column": [
{
"key": "x"
},
{
"key": "y"
}
]
}
],
"optimizer": {
"AdamOptimizer": {
"learning_rate": 1.0
}
}
}
Given that "config" I can instantiate my estimator with:
estimator = load_estimator(model_config, model_dir=model_dir)
The value of type
would resolve to tensorflow.estimator.DNNClassifier
. The value of feature_column[0].<key>
would resolve to tensorflow.feature_column.numeric_column
. Finally, the value of optimizer.<key>
would resolve to tensorflow.train.AdamOptimizer
.
Upvotes: 2
Views: 78
Reputation: 59731
You can do something like this:
import importlib
def get_object_by_name(qualname):
module, _, object = qualname.rpartition(".")
if module:
# package parameter is only necessary for relative imports
# (here relative to this package)
vs = vars(importlib.import_module(module, package=__package__))
else:
# If no module name we assume it is from the current module
vs = globals()
return vs[object]
optimizer_qualname = "tensorflow.train.AdamOptimizer"
optimizer_class = get_object_by_name(optimizer_qualname)
optimizer = optimizer_class()
I changed the optimizer to avoid the error due to the missing learning rate parameter in the example.
Upvotes: 3