本文整理汇总了Python中dragnn.protos.spec_pb2.MasterSpec方法的典型用法代码示例。如果您正苦于以下问题:Python spec_pb2.MasterSpec方法的具体用法?Python spec_pb2.MasterSpec怎么用?Python spec_pb2.MasterSpec使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dragnn.protos.spec_pb2
的用法示例。
在下文中一共展示了spec_pb2.MasterSpec方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testRuntimeConcatentatedMatricesLinked
# 需要导入模块: from dragnn.protos import spec_pb2 [as 别名]
# 或者: from dragnn.protos.spec_pb2 import MasterSpec [as 别名]
def testRuntimeConcatentatedMatricesLinked(self):
"""Test generation of concatenated matrices."""
# TODO(googleuser): Make MockComponent support runtime graph generation.
master = MockMaster(build_runtime_graph=False)
master.spec = spec_pb2.MasterSpec()
text_format.Parse(self.test_spec_linked, master.spec)
lstm_network_unit = self.construct_lstm_network_unit(master)
with tf.variable_scope('bi_lstm', reuse=True):
lstm_network_unit.create(
self.fixed_word_embeddings(), [],
self.get_context_tensor_arrays(lstm_network_unit), None, False)
x_to_ico = lstm_network_unit.derived_params[0]()
h_to_ico = lstm_network_unit.derived_params[1]()
ico_bias = lstm_network_unit.derived_params[2]()
# Should be the word dimension (32) to 3x the hidden dimension (128).
self.assertEqual(x_to_ico.shape, (32, 384))
# Should be the hidden dimension (128) to 3x the hidden dimension (128).
self.assertEqual(h_to_ico.shape, (128, 384))
# Should be equal to the hidden dimension (128) times 3.
self.assertEqual(ico_bias.shape, (384,))
示例2: __init__
# 需要导入模块: from dragnn.protos import spec_pb2 [as 别名]
# 或者: from dragnn.protos.spec_pb2 import MasterSpec [as 别名]
def __init__(self):
self.spec = spec_pb2.MasterSpec()
self.hyperparams = spec_pb2.GridPoint()
self.lookup_component = {
'previous': MockComponent(self, spec_pb2.ComponentSpec())
}
示例3: setUp
# 需要导入模块: from dragnn.protos import spec_pb2 [as 别名]
# 或者: from dragnn.protos.spec_pb2 import MasterSpec [as 别名]
def setUp(self):
# Clear the graph and all existing variables. Otherwise, variables created
# in different tests may collide with each other.
tf.reset_default_graph()
self._master = MockMaster()
self._master.spec = spec_pb2.MasterSpec()
# Add a component with a linked feature.
component_spec = self._master.spec.component.add()
component_spec.name = 'fake_linked'
component_spec.backend.registered_name = 'FakeComponent'
linked_feature = component_spec.linked_feature.add()
linked_feature.source_component = 'fake_linked'
linked_feature.source_translator = 'identity'
linked_feature.embedding_dim = -1
linked_feature.size = 2
self._linked_component = MockComponent(self._master, component_spec)
# Add a feature with a fixed feature.
component_spec = self._master.spec.component.add()
component_spec.name = 'fake_fixed'
component_spec.backend.registered_name = 'FakeComponent'
fixed_feature = component_spec.fixed_feature.add()
fixed_feature.fml = 'input.word'
fixed_feature.embedding_dim = 1
fixed_feature.size = 1
self._fixed_component = MockComponent(self._master, component_spec)
示例4: _make_basic_master_spec
# 需要导入模块: from dragnn.protos import spec_pb2 [as 别名]
# 或者: from dragnn.protos.spec_pb2 import MasterSpec [as 别名]
def _make_basic_master_spec():
"""Constructs a simple spec.
Modified version of nlp/saft/opensource/dragnn/tools/parser_trainer.py
Returns:
spec_pb2.MasterSpec instance.
"""
# Construct the "lookahead" ComponentSpec. This is a simple right-to-left RNN
# sequence model, which encodes the context to the right of each token. It has
# no loss except for the downstream components.
lookahead = spec_builder.ComponentSpecBuilder('lookahead')
lookahead.set_network_unit(
name='FeedForwardNetwork', hidden_layer_sizes='256')
lookahead.set_transition_system(name='shift-only', left_to_right='true')
lookahead.add_fixed_feature(name='words', fml='input.word', embedding_dim=64)
lookahead.add_rnn_link(embedding_dim=-1)
# Construct the ComponentSpec for parsing.
parser = spec_builder.ComponentSpecBuilder('parser')
parser.set_network_unit(name='FeedForwardNetwork', hidden_layer_sizes='256')
parser.set_transition_system(name='arc-standard')
parser.add_token_link(source=lookahead, fml='input.focus', embedding_dim=32)
master_spec = spec_pb2.MasterSpec()
master_spec.component.extend([lookahead.spec, parser.spec])
return master_spec
示例5: LoadSpec
# 需要导入模块: from dragnn.protos import spec_pb2 [as 别名]
# 或者: from dragnn.protos.spec_pb2 import MasterSpec [as 别名]
def LoadSpec(self, spec_path):
master_spec = spec_pb2.MasterSpec()
testdata = os.path.join(FLAGS.test_srcdir,
'dragnn/core/testdata')
with file(os.path.join(testdata, spec_path), 'r') as fin:
text_format.Parse(fin.read().replace('TESTDATA', testdata), master_spec)
return master_spec
示例6: master_spec_graph
# 需要导入模块: from dragnn.protos import spec_pb2 [as 别名]
# 或者: from dragnn.protos.spec_pb2 import MasterSpec [as 别名]
def master_spec_graph(master_spec):
"""Constructs a master spec graph.
Args:
master_spec: MasterSpec proto.
Raises:
TypeError, if master_spec is not the right type. N.B. that this may be
raised if you import proto classes in non-standard ways (e.g. dynamically).
Returns:
SVG graph contents as a string.
"""
if not isinstance(master_spec, spec_pb2.MasterSpec):
raise TypeError("master_spec_graph() expects a MasterSpec input.")
graph = pygraphviz.AGraph(directed=True)
graph.node_attr.update(
shape="box",
style="filled",
fillcolor="white",
fontname="roboto, helvetica, arial",
fontsize=11)
graph.edge_attr.update(fontname="roboto, helvetica, arial", fontsize=11)
for component in master_spec.component:
graph.add_node(component.name, label=_component_contents(component))
for component in master_spec.component:
for linked_feature in component.linked_feature:
graph.add_edge(
linked_feature.source_component,
component.name,
label=_linked_feature_label(linked_feature))
with warnings.catch_warnings():
# Fontconfig spews some warnings, suppress them for now. (Especially because
# they can clutter IPython notebooks).
warnings.simplefilter("ignore")
return graph.draw(format="svg", prog="dot")
示例7: _get_master_spec
# 需要导入模块: from dragnn.protos import spec_pb2 [as 别名]
# 或者: from dragnn.protos.spec_pb2 import MasterSpec [as 别名]
def _get_master_spec():
return spec_pb2.MasterSpec(
component=[spec_pb2.ComponentSpec(name='jalapeño')])
示例8: LoadSpec
# 需要导入模块: from dragnn.protos import spec_pb2 [as 别名]
# 或者: from dragnn.protos.spec_pb2 import MasterSpec [as 别名]
def LoadSpec(self, spec_path):
master_spec = spec_pb2.MasterSpec()
root_dir = os.path.join(FLAGS.test_srcdir,
'dragnn/python')
with file(os.path.join(root_dir, 'testdata', spec_path), 'r') as fin:
text_format.Parse(fin.read().replace('TOPDIR', root_dir), master_spec)
return master_spec
示例9: export
# 需要导入模块: from dragnn.protos import spec_pb2 [as 别名]
# 或者: from dragnn.protos.spec_pb2 import MasterSpec [as 别名]
def export(master_spec_path, params_path, export_path,
export_moving_averages):
"""Restores a model and exports it in SavedModel form.
This method loads a graph specified by the spec at master_spec_path and the
params in params_path. It then saves the model in SavedModel format to the
location specified in export_path.
Args:
master_spec_path: Path to a proto-text master spec.
params_path: Path to the parameters file to export.
export_path: Path to export the SavedModel to.
export_moving_averages: Whether to export the moving average parameters.
"""
graph = tf.Graph()
master_spec = spec_pb2.MasterSpec()
with tf.gfile.FastGFile(master_spec_path) as fin:
text_format.Parse(fin.read(), master_spec)
# Remove '/' if it exists at the end of the export path, ensuring that
# path utils work correctly.
stripped_path = export_path.rstrip('/')
saver_lib.clean_output_paths(stripped_path)
short_to_original = saver_lib.shorten_resource_paths(master_spec)
saver_lib.export_master_spec(master_spec, graph)
saver_lib.export_to_graph(master_spec, params_path, stripped_path, graph,
export_moving_averages)
saver_lib.export_assets(master_spec, short_to_original, stripped_path)
示例10: __init__
# 需要导入模块: from dragnn.protos import spec_pb2 [as 别名]
# 或者: from dragnn.protos.spec_pb2 import MasterSpec [as 别名]
def __init__(self):
self.spec = spec_pb2.MasterSpec()
self.hyperparams = spec_pb2.GridPoint()
self.lookup_component = {'mock': MockComponent()}
示例11: LoadSpec
# 需要导入模块: from dragnn.protos import spec_pb2 [as 别名]
# 或者: from dragnn.protos.spec_pb2 import MasterSpec [as 别名]
def LoadSpec(self, spec_path):
master_spec = spec_pb2.MasterSpec()
root_dir = os.path.join(FLAGS.test_srcdir,
'dragnn/python')
with open(os.path.join(root_dir, 'testdata', spec_path), 'r') as fin:
text_format.Parse(fin.read().replace('TOPDIR', root_dir), master_spec)
return master_spec
示例12: LoadSpec
# 需要导入模块: from dragnn.protos import spec_pb2 [as 别名]
# 或者: from dragnn.protos.spec_pb2 import MasterSpec [as 别名]
def LoadSpec(self, spec_path):
master_spec = spec_pb2.MasterSpec()
testdata = os.path.join(FLAGS.test_srcdir,
'dragnn/core/testdata')
with open(os.path.join(testdata, spec_path), 'r') as fin:
text_format.Parse(fin.read().replace('TESTDATA', testdata), master_spec)
return master_spec
示例13: load_model
# 需要导入模块: from dragnn.protos import spec_pb2 [as 别名]
# 或者: from dragnn.protos.spec_pb2 import MasterSpec [as 别名]
def load_model(base_dir, master_spec_name, checkpoint_name):
"""
Function to load the syntaxnet models. Highly specific to the tutorial
format right now.
"""
# Read the master spec
master_spec = spec_pb2.MasterSpec()
with open(os.path.join(base_dir, master_spec_name), "r") as f:
text_format.Merge(f.read(), master_spec)
spec_builder.complete_master_spec(master_spec, None, base_dir)
logging.set_verbosity(logging.WARN) # Turn off TensorFlow spam.
# Initialize a graph
graph = tf.Graph()
with graph.as_default():
hyperparam_config = spec_pb2.GridPoint()
builder = graph_builder.MasterBuilder(master_spec, hyperparam_config)
# This is the component that will annotate test sentences.
annotator = builder.add_annotation(enable_tracing=True)
builder.add_saver() # "Savers" can save and load models; here, we're only going to load.
sess = tf.Session(graph=graph)
with graph.as_default():
#sess.run(tf.global_variables_initializer())
#sess.run('save/restore_all', {'save/Const:0': os.path.join(base_dir, checkpoint_name)})
builder.saver.restore(sess, os.path.join(base_dir, checkpoint_name))
def annotate_sentence(sentence):
with graph.as_default():
return sess.run([annotator['annotations'], annotator['traces']],
feed_dict={annotator['input_batch']: [sentence]})
return annotate_sentence