本文整理汇总了Python中tensorflow.contrib.session_bundle.exporter.generic_signature方法的典型用法代码示例。如果您正苦于以下问题:Python exporter.generic_signature方法的具体用法?Python exporter.generic_signature怎么用?Python exporter.generic_signature使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.contrib.session_bundle.exporter
的用法示例。
在下文中一共展示了exporter.generic_signature方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Export
# 需要导入模块: from tensorflow.contrib.session_bundle import exporter [as 别名]
# 或者: from tensorflow.contrib.session_bundle.exporter import generic_signature [as 别名]
def Export():
export_path = "/tmp/half_plus_two"
with tf.Session() as sess:
# Make model parameters a&b variables instead of constants to
# exercise the variable reloading mechanisms.
a = tf.Variable(0.5)
b = tf.Variable(2.0)
# Calculate, y = a*x + b
# here we use a placeholder 'x' which is fed at inference time.
x = tf.placeholder(tf.float32)
y = tf.add(tf.multiply(a, x), b)
# Run an export.
tf.global_variables_initializer().run()
export = exporter.Exporter(tf.train.Saver())
export.init(named_graph_signatures={
"inputs": exporter.generic_signature({"x": x}),
"outputs": exporter.generic_signature({"y": y}),
"regress": exporter.regression_signature(x, y)
})
export.export(export_path, tf.constant(123), sess)
示例2: generic_signature_fn
# 需要导入模块: from tensorflow.contrib.session_bundle import exporter [as 别名]
# 或者: from tensorflow.contrib.session_bundle.exporter import generic_signature [as 别名]
def generic_signature_fn(examples, unused_features, predictions):
"""Creates generic signature from given examples and predictions.
This is needed for backward compatibility with default behaviour of
export_estimator.
Args:
examples: `Tensor`.
unused_features: `dict` of `Tensor`s.
predictions: `Tensor` or `dict` of `Tensor`s.
Returns:
Tuple of default signature and empty named signatures.
Raises:
ValueError: If examples is `None`.
"""
if examples is None:
raise ValueError('examples cannot be None when using this signature fn.')
tensors = {'inputs': examples}
if not isinstance(predictions, dict):
predictions = {'outputs': predictions}
tensors.update(predictions)
default_signature = exporter.generic_signature(tensors)
return default_signature, {}
示例3: export_session_bundle
# 需要导入模块: from tensorflow.contrib.session_bundle import exporter [as 别名]
# 或者: from tensorflow.contrib.session_bundle.exporter import generic_signature [as 别名]
def export_session_bundle(self):
export_dir_base = self.saver_spec.get('export_directory')
if not export_dir_base:
print("export_directory is None")
checkpoint = tf.train.latest_checkpoint(self.saver_directory)
if not checkpoint:
raise NotFittedError("Couldn't find trained model at %s." % self.saver_directory)
export_dir = saved_model_export_utils.get_timestamped_export_dir(export_dir_base)
if self.distributed_spec:
sess = tf.Session(target=self.server.target, graph=self.graph, config=self.session_config)
else:
sess = tf.Session(graph=self.graph)
self.scaffold.saver.restore(sess, checkpoint)
signature = {name: ts for name, ts in self.states_input.items()}
signature["deterministic"] = self.deterministic_input
signature["update"] = self.update_input
exporter = Exporter(self.scaffold.saver)
exporter.init(self.graph.as_graph_def(),
clear_devices=True,
default_graph_signature=generic_signature(signature))
exporter.export(export_dir_base=export_dir,
global_step_tensor=self.timestep,
sess=sess)
return export_dir
示例4: export_model
# 需要导入模块: from tensorflow.contrib.session_bundle import exporter [as 别名]
# 或者: from tensorflow.contrib.session_bundle.exporter import generic_signature [as 别名]
def export_model(sess, inputs_signature, outputs_signature):
# Export the model for generic inference service
print("Exporting trained model to {}".format(FLAGS.model_path))
saver = tf.train.Saver(sharded=True)
model_exporter = exporter.Exporter(saver)
model_exporter.init(
sess.graph.as_graph_def(),
named_graph_signatures={
"inputs": exporter.generic_signature(inputs_signature),
"outputs": exporter.generic_signature(outputs_signature)
})
model_exporter.export(FLAGS.model_path, tf.constant(FLAGS.model_version),
sess)
print("Done exporting!")
示例5: main
# 需要导入模块: from tensorflow.contrib.session_bundle import exporter [as 别名]
# 或者: from tensorflow.contrib.session_bundle.exporter import generic_signature [as 别名]
def main():
# Define training data
x = np.ones(FLAGS.batch_size)
y = np.ones(FLAGS.batch_size)
# Define the model
X = tf.placeholder(tf.float32, shape=[None])
Y = tf.placeholder(tf.float32, shape=[None])
w = tf.Variable(1.0, name="weight")
b = tf.Variable(1.0, name="bias")
loss = tf.square(Y - tf.mul(X, w) - b)
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
predict_op = tf.mul(X, w) + b
saver = tf.train.Saver()
checkpoint_dir = FLAGS.checkpoint_dir
checkpoint_file = checkpoint_dir + "/checkpoint.ckpt"
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
# Start the session
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
if ckpt and ckpt.model_checkpoint_path:
print("Continue training from the model {}".format(ckpt.model_checkpoint_path))
saver.restore(sess, ckpt.model_checkpoint_path)
# Start training
start_time = time.time()
for epoch in range(FLAGS.epoch_number):
sess.run(train_op, feed_dict={X: x, Y: y})
# Start validating
if epoch % FLAGS.steps_to_validate == 0:
end_time = time.time()
print("[{}] Epoch: {}".format(end_time - start_time, epoch))
saver.save(sess, checkpoint_file)
start_time = end_time
# Print model variables
w_value, b_value = sess.run([w, b])
print("The model of w: {}, b: {}".format(w_value, b_value))
# Export the model
print("Exporting trained model to {}".format(FLAGS.model_path))
model_exporter = exporter.Exporter(saver)
model_exporter.init(
sess.graph.as_graph_def(),
named_graph_signatures={
'inputs': exporter.generic_signature({"features": X}),
'outputs': exporter.generic_signature({"prediction": predict_op})
})
model_exporter.export(FLAGS.model_path, tf.constant(FLAGS.export_version), sess)
print 'Done exporting!'