本文整理汇总了Python中tensorflow_hub.add_signature方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow_hub.add_signature方法的具体用法?Python tensorflow_hub.add_signature怎么用?Python tensorflow_hub.add_signature使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow_hub
的用法示例。
在下文中一共展示了tensorflow_hub.add_signature方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testUnsupportedCollections
# 需要导入模块: import tensorflow_hub [as 别名]
# 或者: from tensorflow_hub import add_signature [as 别名]
def testUnsupportedCollections(self):
def module_fn():
scale = tf_v1.get_variable("x", (), collections=["my_scope"])
x = tf_v1.placeholder(tf.float32, shape=[None, 3])
native_module.add_signature("my_func", {"x": x}, {"y": x*scale})
with self.assertRaises(ValueError) as cm:
_ = native_module.create_module_spec(module_fn)
self.assertIn("Unsupported collections in graph", cm)
with tf.Graph().as_default() as tmp_graph:
module_fn()
unsupported_collections = native_module.get_unsupported_collections(
tmp_graph.get_all_collection_keys())
self.assertEqual(["my_scope"], unsupported_collections)
_ = native_module.create_module_spec(
module_fn, drop_collections=unsupported_collections)
示例2: stateful_rv_with_input_module_fn
# 需要导入模块: import tensorflow_hub [as 别名]
# 或者: from tensorflow_hub import add_signature [as 别名]
def stateful_rv_with_input_module_fn():
x = tf_v1.placeholder(dtype=tf.float32, name="x")
# Add a placeholder/variable that doesn't go to an output.
y = tf_v1.placeholder(dtype=tf.float32, name="y")
r = tf_v1.get_variable(
"rv_var123",
shape=[],
initializer=tf_v1.constant_initializer(10.0),
use_resource=True)
t = tf_v1.get_variable(
"rv_var456",
shape=[],
initializer=tf_v1.constant_initializer(10.0),
use_resource=True)
t.assign(y)
res = x + r
hub.add_signature(inputs={"x": x}, outputs=res)
示例3: layers_module_fn
# 需要导入模块: import tensorflow_hub [as 别名]
# 或者: from tensorflow_hub import add_signature [as 别名]
def layers_module_fn():
"""Module that exercises the use of layers."""
# This is a plain linear map Mx+b regularized by the sum of the squares
# of the coefficients in M and b.
x = tf_v1.placeholder(dtype=tf.float32, shape=[None, 2], name="x")
def l2(weights):
"""Applies l2 regularization to weights."""
with tf.control_dependencies([weights]):
return 2.0 * tf_v1.nn.l2_loss(weights)
h = tf_v1.layers.dense(
x, 2,
activation=None,
kernel_regularizer=l2,
bias_regularizer=l2)
hub.add_signature(inputs=x, outputs=h)
示例4: valid_colocation_module_fn
# 需要导入模块: import tensorflow_hub [as 别名]
# 或者: from tensorflow_hub import add_signature [as 别名]
def valid_colocation_module_fn():
w = tf.Variable(42 + 69, name="w")
# w.op has the same name on resource and non-resource variables
with tf_v1.colocate_with(w.op):
# A colocation reference among state nodes is ok.
v = tf.Variable(1.0, name="v")
assert v.op.colocation_groups() == [tf.compat.as_bytes("loc:@w")]
# A colocation reference from other nodes to state nodes is ok.
y = tf.add(v, 1, name="y")
assert y.op.colocation_groups() == [tf.compat.as_bytes("loc:@w")]
x = tf_v1.placeholder(dtype=tf.float32, name="x")
with tf_v1.colocate_with(x):
# A colocation reference from other nodes to input nodes is ok.
z = tf.add(x, 1, name="z")
assert z.op.colocation_groups() == [tf.compat.as_bytes("loc:@x")]
hub.add_signature(inputs=dict(x=x), outputs=dict(y=y, z=z))
示例5: testDuplicateAssetCopy
# 需要导入模块: import tensorflow_hub [as 别名]
# 或者: from tensorflow_hub import add_signature [as 别名]
def testDuplicateAssetCopy(self):
export_path = os.path.join(self.get_temp_dir(), "assets-module")
def module_with_duplicate_asset():
vocabulary_file = self.create_vocab_file("tokens2.txt", ["1", "2", "3"])
indices1 = tf_v1.placeholder(dtype=tf.int64, name="indices1")
indices2 = tf_v1.placeholder(dtype=tf.int64, name="indices2")
hub.add_signature(
inputs={
"indices_1": indices1,
"indices_2": indices2,
},
outputs={
"x": do_table_lookup(indices1, vocabulary_file),
"y": do_table_lookup(indices2, vocabulary_file),
})
with tf.Graph().as_default():
spec = hub.create_module_spec(module_with_duplicate_asset)
module_a = hub.Module(spec)
module_a({"indices_1": tf.constant([1, 2], dtype=tf.int64),
"indices_2": tf.constant([1, 2], dtype=tf.int64)}, as_dict=True)
with tf_v1.Session() as sess:
sess.run(tf_v1.tables_initializer())
module_a.export(export_path, sess)
示例6: text_module_fn
# 需要导入模块: import tensorflow_hub [as 别名]
# 或者: from tensorflow_hub import add_signature [as 别名]
def text_module_fn():
embeddings = [
("", [0, 0, 0, 0]), # OOV items are mapped to this embedding.
("hello world", [1, 2, 3, 4]),
("pair-programming", [5, 5, 5, 5]),
]
keys = tf.constant([item[0] for item in embeddings], dtype=tf.string)
indices = tf.constant(list(range(len(embeddings))), dtype=tf.int64)
tbl_init = KeyValueTensorInitializer(keys, indices)
table = HashTable(tbl_init, 0)
weights_initializer = tf.cast(
tf.constant(list([item[1] for item in embeddings])), tf.float32)
weights = tf_v1.get_variable(
"weights", dtype=tf.float32, initializer=weights_initializer)
text_tensor = tf_v1.placeholder(dtype=tf.string, name="text", shape=[None])
indices_tensor = table.lookup(text_tensor)
embedding_tensor = tf.gather(weights, indices_tensor)
hub.add_signature(inputs=text_tensor, outputs=embedding_tensor)
示例7: _save_half_plus_one_hub_module_v1
# 需要导入模块: import tensorflow_hub [as 别名]
# 或者: from tensorflow_hub import add_signature [as 别名]
def _save_half_plus_one_hub_module_v1(path):
"""Writes TF1.x hub.Module to compute y = wx + 1, with w trainable."""
def half_plus_one():
x = tf.compat.v1.placeholder(shape=(None,1), dtype=tf.float32)
# Use TF1 native tf.compat.v1.layers instead of tf.keras.layers as they
# correctly update TF collections, such as REGULARIZATION_LOSS.
times_w = tf.compat.v1.layers.Dense(
units=1,
kernel_initializer=tf.keras.initializers.Constant([[0.5]]),
kernel_regularizer=tf.keras.regularizers.l2(0.01),
use_bias=False)
plus_1 = tf.compat.v1.layers.Dense(
units=1,
kernel_initializer=tf.keras.initializers.Constant([[1.0]]),
bias_initializer=tf.keras.initializers.Constant([1.0]),
trainable=False)
y = plus_1(times_w(x))
hub.add_signature(inputs=x, outputs=y)
spec = hub.create_module_spec(half_plus_one)
_export_module_spec_with_init_weights(spec, path)
示例8: apply_model
# 需要导入模块: import tensorflow_hub [as 别名]
# 或者: from tensorflow_hub import add_signature [as 别名]
def apply_model(image_fn, # pylint: disable=missing-docstring
is_training,
num_outputs,
make_signature=False):
# Image tensor needs to be created lazily in order to satisfy tf-hub
# restriction: all tensors should be created inside tf-hub helper function.
images = image_fn()
net = get_net(num_classes=num_outputs)
output, end_points = net(images, is_training)
if make_signature:
hub.add_signature(inputs={'image': images}, outputs=output)
hub.add_signature(inputs={'image': images}, outputs=end_points,
name='representation')
return output
示例9: apply_model
# 需要导入模块: import tensorflow_hub [as 别名]
# 或者: from tensorflow_hub import add_signature [as 别名]
def apply_model(image_fn, # pylint: disable=missing-docstring
is_training,
num_outputs,
make_signature=False):
# Image tensor needs to be created lazily in order to satisfy tf-hub
# restriction: all tensors should be created inside tf-hub helper function.
images = image_fn()
net = get_net(num_classes=num_outputs)
output, end_points = net(images, is_training)
if make_signature:
hub.add_signature(inputs={'image': images}, outputs=output)
hub.add_signature(
name='representation',
inputs={'image': images},
outputs=end_points)
return output
示例10: make_module_spec_for_testing
# 需要导入模块: import tensorflow_hub [as 别名]
# 或者: from tensorflow_hub import add_signature [as 别名]
def make_module_spec_for_testing(input_image_height=289,
input_image_width=289,
output_feature_dim=64):
"""Makes a stub image feature module for use in `TFImageProcessor` tests.
The resulting module has the signature expected by `TFImageProcessor`, but it
has no trainable variables and its initialization loads nothing from disk.
Args:
input_image_height: int, height of the module's input images.
input_image_width: int, width of module's input images.
output_feature_dim: int, dimension of the output feature vectors.
Returns:
`hub.ModuleSpec`
"""
def module_fn():
"""Builds the graph and signature for the stub TF-hub module."""
image_data = tf.placeholder(
shape=[1, input_image_height, input_image_width, 3], dtype=tf.float32)
# Linearly project image_data to shape [1, output_feature_dim] features.
projection_matrix = tf.ones([tf.size(image_data), output_feature_dim],
dtype=tf.float32)
encoder_output = tf.matmul(
tf.reshape(image_data, [1, -1]), projection_matrix)
# NB: the input feature must be named 'images' to satisfy
# hub.image_util.get_expected_image_size().
hub.add_signature(
'default', inputs={'images': image_data}, outputs=encoder_output)
return hub.create_module_spec(module_fn)
示例11: create_dummy_hub_module
# 需要导入模块: import tensorflow_hub [as 别名]
# 或者: from tensorflow_hub import add_signature [as 别名]
def create_dummy_hub_module(num_outputs):
"""Creates minimal hub module for testing purposes."""
def module_fn(is_training):
x = tf.placeholder(dtype=tf.float32, shape=[32, 224, 224, 3])
h = tf.reduce_mean(x, axis=[1, 2])
if is_training:
h = tf.nn.dropout(h, 0.5)
y = tf.layers.dense(h, num_outputs)
hub.add_signature(inputs=x, outputs={"pre_logits": h, "logits": y})
return hub.create_module_spec(
module_fn,
tags_and_args=[({"train"}, {"is_training": True}),
(set(), {"is_training": False})])
示例12: _stateless_module_fn
# 需要导入模块: import tensorflow_hub [as 别名]
# 或者: from tensorflow_hub import add_signature [as 别名]
def _stateless_module_fn(self):
"""Simple module that squares an input."""
x = tf_v1.placeholder(tf.int64)
y = x*x
hub.add_signature(inputs=x, outputs=y)
示例13: test_module_export_vocab_on_custom_fs
# 需要导入模块: import tensorflow_hub [as 别名]
# 或者: from tensorflow_hub import add_signature [as 别名]
def test_module_export_vocab_on_custom_fs(self):
root_dir = "file://%s" % self.get_temp_dir()
export_dir = "%s_%s" % (root_dir, "export")
tf_v1.gfile.MakeDirs(export_dir)
# Create a module with a vocab file located on a custom filesystem.
vocab_dir = os.path.join(root_dir, "vocab_location")
tf_v1.gfile.MakeDirs(vocab_dir)
vocab_filename = os.path.join(vocab_dir, "tokens.txt")
tf_utils.atomic_write_string_to_file(vocab_filename, "one", False)
def create_assets_module_fn():
def assets_module_fn():
indices = tf_v1.placeholder(dtype=tf.int64, name="indices")
table = index_to_string_table_from_file(
vocabulary_file=vocab_filename, default_value="UNKNOWN")
outputs = table.lookup(indices)
hub.add_signature(inputs=indices, outputs=outputs)
return assets_module_fn
with tf.Graph().as_default():
assets_module_fn = create_assets_module_fn()
spec = hub.create_module_spec(assets_module_fn)
embedding_module = hub.Module(spec)
with tf_v1.Session() as sess:
sess.run(tf_v1.tables_initializer())
embedding_module.export(export_dir, sess)
module_files = tf_v1.gfile.ListDirectory(export_dir)
self.assertListEqual(
["assets", "saved_model.pb", "tfhub_module.pb", "variables"],
sorted(module_files))
module_files = tf_v1.gfile.ListDirectory(os.path.join(export_dir, "assets"))
self.assertListEqual(["tokens.txt"], module_files)
示例14: _save_plus_one_hub_module_v1
# 需要导入模块: import tensorflow_hub [as 别名]
# 或者: from tensorflow_hub import add_signature [as 别名]
def _save_plus_one_hub_module_v1(path):
def plus_one():
x = tf.compat.v1.placeholder(dtype=tf.float32, name='x')
y = x + 1
hub.add_signature(inputs=x, outputs=y)
spec = hub.create_module_spec(plus_one)
with tf.compat.v1.Graph().as_default():
module = hub.Module(spec, trainable=True)
with tf.compat.v1.Session() as session:
session.run(tf.compat.v1.global_variables_initializer())
module.export(path, session)
示例15: multi_signature_module
# 需要导入模块: import tensorflow_hub [as 别名]
# 或者: from tensorflow_hub import add_signature [as 别名]
def multi_signature_module():
x = tf_v1.placeholder(tf.float32, shape=[None])
native_module.add_signature("double", {"x": x}, {"y": 2*x})
z = tf_v1.placeholder(tf.float32, shape=[None])
native_module.add_signature("square", {"z": z}, {"z_out": z*z})