def cell_from_spec(cell_classname, cell_params):
"""Create a RNN Cell instance from a JSON string.
Args:
cell_classname: Name of the cell class, e.g. "BasicLSTMCell".
cell_params: A dictionary of parameters to pass to the cell constructor.
Returns:
A RNNCell instance.
"""
cell_params = cell_params.copy()
# Find the cell class
cell_class = locate(cell_classname) or getattr(rnn_cell, cell_classname)
# Make sure additional arguments are valid
cell_args = set(inspect.getargspec(cell_class.__init__).args[1:])
for key in cell_params.keys():
if key not in cell_args:
raise ValueError(
"""{} is not a valid argument for {} class. Available arguments
are: {}""".format(key, cell_class.__name__, cell_args))
# Create cell
return cell_class(**cell_params)
python类locate()的实例源码
def _load_model_from_config(config_path, hparam_overrides, vocab_file, mode):
"""Loads model from a configuration file"""
with gfile.GFile(config_path) as config_file:
config = yaml.load(config_file)
model_cls = locate(config["model"]) or getattr(models, config["model"])
model_params = config["model_params"]
if hparam_overrides:
model_params.update(hparam_overrides)
# Change the max decode length to make the test run faster
model_params["decoder.params"]["max_decode_length"] = 5
model_params["vocab_source"] = vocab_file
model_params["vocab_target"] = vocab_file
return model_cls(params=model_params, mode=mode)
def __init__(self, params, name):
# We don't call the super constructor on purpose
#pylint: disable=W0231
"""Initializer"""
Configurable.__init__(self, params, tf.contrib.learn.ModeKeys.EVAL)
self._name = name
self._eos_token = self.params["eos_token"]
self._sos_token = self.params["sos_token"]
self._separator = self.params["separator"]
self._postproc_fn = None
if self.params["postproc_fn"]:
self._postproc_fn = locate(self.params["postproc_fn"])
if self._postproc_fn is None:
raise ValueError("postproc_fn not found: {}".format(
self.params["postproc_fn"]))
def __init__(self, params, mode, name="conv_encoder"):
super(ConvEncoder, self).__init__(params, mode, name)
self._combiner_fn = locate(self.params["position_embeddings.combiner_fn"])
def __init__(self, params, mode, name="pooling_encoder"):
super(PoolingEncoder, self).__init__(params, mode, name)
self._pooling_fn = locate(self.params["pooling_fn"])
self._combiner_fn = locate(self.params["position_embeddings.combiner_fn"])
def generate():
hw = locate('uchroma.server.Hardware')
assert hw is not None
hwdb = ""
for hw_type in hw.Type:
for model in hw.get_type(hw_type):
hwdb += ('uchroma:usb:v%04Xp%04X*\n'
' UCHROMA_DEVICE=%s\n\n'
% (model.vendor_id, model.product_id, model.type.name.lower()))
return hwdb
def _read_xml_generator(self, xml):
"""
:param lxml.etree.Element xml:
"""
module = locate('enarksh.xml_reader.node')
node = module.create_node('CommandJob', self)
node.read_xml(xml)
# Add child node to map of child nodes.
self._generator = node
# ------------------------------------------------------------------------------------------------------------------
def _read_xml_worker(self, xml):
"""
:param lxml.etree.Element xml:
"""
module = locate('enarksh.xml_reader.node')
node = module.create_node('DynamicOuterWorker', self)
node.read_xml(xml)
# Add child node to map of child nodes.
self._worker = node
# ------------------------------------------------------------------------------------------------------------------
def load_exploit(self, exploit_to_load):
"""Loads a given exploit to the active session
Checking for exploit validity happens in this method. The check is a
dynamic module load from the exploits module dir. If we don't get an
error, then the exploit exists. If we get an error, the exploit was
not entered correctly. Accession-by-title.
"""
#try:
exploit_module = locate("exploits." + exploit_to_load.strip() + "." + exploit_to_load.strip())
exploit_instance = exploit_module()
self.active_session.set_exploit(exploit_instance)
#except ModuleNotFoundError as mnfe:
# print ("{}\n\t[*] Entered: {}".format(INVALID_EXPLOIT_ERROR, exploit_module_name))
def raise_exception(result):
exc = locate(result["exception"])
if exc:
raise exc(*result.get("args", []), **result.get("kwargs", {}))
else:
raise TypeError("Couldn't resolve exception {}", result["exception"])
def locate_with_hint(class_path, prefix_hints=[]):
module_or_class = locate(class_path)
if module_or_class is None:
# for hint in iscanr(lambda x, y: x + "." + y, prefix_hints):
# module_or_class = locate(hint + "." + class_path)
# if module_or_class:
# break
hint = ".".join(prefix_hints)
module_or_class = locate(hint + "." + class_path)
return module_or_class
def __init__(self, encoder_outputs, decoder_state_size, params, mode):
super(InitialStateBridge, self).__init__(encoder_outputs,
decoder_state_size, params, mode)
if not hasattr(encoder_outputs, self.params["bridge_input"]):
raise ValueError("Invalid bridge_input not in encoder outputs.")
self._bridge_input = getattr(encoder_outputs, self.params["bridge_input"])
self._activation_fn = locate(self.params["activation_fn"])
def __init__(self, params, mode, name="basic_seq2seq"):
super(BasicSeq2Seq, self).__init__(params, mode, name)
self.encoder_class = locate(self.params["encoder.class"])
self.decoder_class = locate(self.params["decoder.class"])
def _create_bridge(self, encoder_outputs, decoder_state_size):
"""Creates the bridge to be used between encoder and decoder"""
bridge_class = locate(self.params["bridge.class"]) or \
getattr(bridges, self.params["bridge.class"])
return bridge_class(
encoder_outputs=encoder_outputs,
decoder_state_size=decoder_state_size,
params=self.params["bridge.params"],
mode=self.mode)
def __init__(self, params, mode, name="conv_seq2seq"):
super(ConvSeq2Seq, self).__init__(params, mode, name)
self.encoder_class = locate(self.params["encoder.class"])
self.decoder_class = locate(self.params["decoder.class"])
def cell_from_spec(cell_classname, cell_params):
"""Create a RNN Cell instance from a JSON string.
Args:
cell_classname: Name of the cell class, e.g. "BasicLSTMCell".
cell_params: A dictionary of parameters to pass to the cell constructor.
Returns:
A RNNCell instance.
"""
cell_params = cell_params.copy()
# Find the cell class
cell_class = locate(cell_classname) or getattr(rnn_cell, cell_classname)
# Make sure additional arguments are valid
cell_args = set(inspect.getargspec(cell_class.__init__).args[1:])
for key in cell_params.keys():
if key not in cell_args:
raise ValueError(
"""{} is not a valid argument for {} class. Available arguments
are: {}""".format(key, cell_class.__name__, cell_args))
# Create cell
return cell_class(**cell_params)
def _create_from_dict(dict_, default_module, *args, **kwargs):
"""Creates a configurable class from a dictionary. The dictionary must have
"class" and "params" properties. The class can be either fully qualified, or
it is looked up in the modules passed via `default_module`.
"""
class_ = locate(dict_["class"]) or getattr(default_module, dict_["class"])
params = {}
if "params" in dict_:
params = dict_["params"]
instance = class_(params, *args, **kwargs)
return instance
def _load_model_from_config(config_path, hparam_overrides, vocab_file, mode):
"""Loads model from a configuration file"""
with gfile.GFile(config_path) as config_file:
config = yaml.load(config_file)
model_cls = locate(config["model"]) or getattr(models, config["model"])
model_params = config["model_params"]
if hparam_overrides:
model_params.update(hparam_overrides)
# Change the max decode length to make the test run faster
model_params["decoder.params"]["max_decode_length"] = 5
model_params["vocab_source"] = vocab_file
model_params["vocab_target"] = vocab_file
return model_cls(params=model_params, mode=mode)
def __init__(self, params, name):
# We don't call the super constructor on purpose
#pylint: disable=W0231
"""Initializer"""
Configurable.__init__(self, params, tf.contrib.learn.ModeKeys.EVAL)
self._name = name
self._eos_token = self.params["eos_token"]
self._sos_token = self.params["sos_token"]
self._separator = self.params["separator"]
self._postproc_fn = None
if self.params["postproc_fn"]:
self._postproc_fn = locate(self.params["postproc_fn"])
if self._postproc_fn is None:
raise ValueError("postproc_fn not found: {}".format(
self.params["postproc_fn"]))
def __init__(self, params, mode, name="pooling_encoder"):
super(PoolingEncoder, self).__init__(params, mode, name)
self._pooling_fn = locate(self.params["pooling_fn"])
self._combiner_fn = locate(self.params["position_embeddings.combiner_fn"])