本文整理汇总了Python中tensorflow.Session方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.Session方法的具体用法?Python tensorflow.Session怎么用?Python tensorflow.Session使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.Session方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: savepb
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import Session [as 别名]
def savepb(self):
"""
Create a standalone const graph def that
C++ can load and run.
"""
darknet_pb = self.to_darknet()
flags_pb = self.FLAGS
flags_pb.verbalise = False
flags_pb.train = False
# rebuild another tfnet. all const.
tfnet_pb = TFNet(flags_pb, darknet_pb)
tfnet_pb.sess = tf.Session(graph = tfnet_pb.graph)
# tfnet_pb.predict() # uncomment for unit testing
name = 'built_graph/{}.pb'.format(self.meta['name'])
os.makedirs(os.path.dirname(name), exist_ok=True)
#Save dump of everything in meta
with open('built_graph/{}.meta'.format(self.meta['name']), 'w') as fp:
json.dump(self.meta, fp)
self.say('Saving const graph def to {}'.format(name))
graph_def = tfnet_pb.sess.graph_def
tf.train.write_graph(graph_def,'./', name, False)
示例2: create_session
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import Session [as 别名]
def create_session(config_dict=dict(), force_as_default=False):
config = tf.ConfigProto()
for key, value in config_dict.items():
fields = key.split('.')
obj = config
for field in fields[:-1]:
obj = getattr(obj, field)
setattr(obj, fields[-1], value)
session = tf.Session(config=config)
if force_as_default:
session._default_session = session.as_default()
session._default_session.enforce_nesting = False
session._default_session.__enter__()
return session
#----------------------------------------------------------------------------
# Initialize all tf.Variables that have not already been initialized.
# Equivalent to the following, but more efficient and does not bloat the tf graph:
# tf.variables_initializer(tf.report_unitialized_variables()).run()
示例3: test_feature_pairing
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import Session [as 别名]
def test_feature_pairing(self):
fgsm = FastGradientMethod(self.model)
attack = lambda x: fgsm.generate(x)
loss = FeaturePairing(self.model, weight=0.1, attack=attack)
l = loss.fprop(self.x, self.y)
with tf.Session() as sess:
vl1 = sess.run(l, feed_dict={self.x: self.vx, self.y: self.vy})
vl2 = sess.run(l, feed_dict={self.x: self.vx, self.y: self.vy})
self.assertClose(vl1, sum([4.296023369, 2.963884830]) / 2., atol=1e-6)
self.assertClose(vl2, sum([4.296023369, 2.963884830]) / 2., atol=1e-6)
loss = FeaturePairing(self.model, weight=10., attack=attack)
l = loss.fprop(self.x, self.y)
with tf.Session() as sess:
vl1 = sess.run(l, feed_dict={self.x: self.vx, self.y: self.vy})
vl2 = sess.run(l, feed_dict={self.x: self.vx, self.y: self.vy})
self.assertClose(vl1, sum([4.333082676, 3.00094414]) / 2., atol=1e-6)
self.assertClose(vl2, sum([4.333082676, 3.00094414]) / 2., atol=1e-6)
示例4: __init__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import Session [as 别名]
def __init__(self, env, dueling, noisy, fname):
self.g = tf.Graph()
self.noisy = noisy
self.dueling = dueling
self.env = env
with self.g.as_default():
self.act = deepq.build_act_enjoy(
make_obs_ph=lambda name: U.Uint8Input(
env.observation_space.shape, name=name),
q_func=dueling_model if dueling else model,
num_actions=env.action_space.n,
noisy=noisy
)
self.saver = tf.train.Saver()
self.sess = tf.Session(graph=self.g)
if fname is not None:
print('Loading Model...')
self.saver.restore(self.sess, fname)
示例5: cleverhans_attack_wrapper
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import Session [as 别名]
def cleverhans_attack_wrapper(cleverhans_attack_fn, reset=True):
def attack(a):
session = tf.Session()
with session.as_default():
model = RVBCleverhansModel(a)
adversarial_image = cleverhans_attack_fn(model, session, a)
adversarial_image = np.squeeze(adversarial_image, axis=0)
if reset:
# optionally, reset to ignore other adversarials
# found during the search
a._reset()
# run predictions to make sure the returned adversarial
# is taken into account
min_, max_ = a.bounds()
adversarial_image = np.clip(adversarial_image, min_, max_)
a.predictions(adversarial_image)
return attack
示例6: setUp
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import Session [as 别名]
def setUp(self):
super(TestRunnerMultiGPU, self).setUp()
self.sess = tf.Session()
inputs = []
outputs = []
self.niter = 10
niter = self.niter
# A Simple graph with `niter` sub-graphs.
with tf.variable_scope(None, 'runner'):
for i in range(niter):
v = tf.get_variable('v%d' % i, shape=(100, 10))
w = tf.get_variable('w%d' % i, shape=(100, 1))
inputs += [{'v': v, 'w': w}]
outputs += [{'v': v, 'w': w}]
self.runner = RunnerMultiGPU(inputs, outputs, sess=self.sess)
示例7: test_drop
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import Session [as 别名]
def test_drop():
# Make sure dropout is activated successfully
# We would like to configure the test to deterministically drop,
# so that the test does not need to use multiple runs.
# However, tf.nn.dropout divides by include_prob, so zero or
# infinitesimal include_prob causes NaNs.
# 1e-8 does not cause NaNs and shouldn't be a significant source
# of test flakiness relative to dependency downloads failing, etc.
model = MLP(input_shape=[1, 1], layers=[Dropout(name='output',
include_prob=1e-8)])
x = tf.constant([[1]], dtype=tf.float32)
y = model.get_layer(x, 'output', dropout=True)
sess = tf.Session()
y_value = sess.run(y)
# Subject to very rare random failure because include_prob is not exact 0
assert y_value == 0., y_value
示例8: test_override
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import Session [as 别名]
def test_override():
# Make sure dropout_dict changes dropout probabilities successful
# We would like to configure the test to deterministically drop,
# so that the test does not need to use multiple runs.
# However, tf.nn.dropout divides by include_prob, so zero or
# infinitesimal include_prob causes NaNs.
# For this test, random failure to drop will not cause the test to fail.
# The stochastic version should not even run if everything is working
# right.
model = MLP(input_shape=[1, 1], layers=[Dropout(name='output',
include_prob=1e-8)])
x = tf.constant([[1]], dtype=tf.float32)
dropout_dict = {'output': 1.}
y = model.get_layer(x, 'output', dropout=True, dropout_dict=dropout_dict)
sess = tf.Session()
y_value = sess.run(y)
assert y_value == 1., y_value
示例9: test_separate
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import Session [as 别名]
def test_separate(test_file, configuration, backend):
""" Test separation from raw data. """
with tf.Session() as sess:
instruments = MODEL_TO_INST[configuration]
adapter = get_default_audio_adapter()
waveform, _ = adapter.load(test_file)
separator = Separator(configuration, stft_backend=backend)
prediction = separator.separate(waveform, test_file)
assert len(prediction) == len(instruments)
for instrument in instruments:
assert instrument in prediction
for instrument in instruments:
track = prediction[instrument]
assert waveform.shape[:-1] == track.shape[:-1]
assert not np.allclose(waveform, track)
for compared in instruments:
if instrument != compared:
assert not np.allclose(track, prediction[compared])
示例10: main
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import Session [as 别名]
def main():
rgb = False
if rgb:
kernels_list = [kernels.BLUR_FILTER_RGB,
kernels.SHARPEN_FILTER_RGB,
kernels.EDGE_FILTER_RGB,
kernels.TOP_SOBEL_RGB,
kernels.EMBOSS_FILTER_RGB]
else:
kernels_list = [kernels.BLUR_FILTER,
kernels.SHARPEN_FILTER,
kernels.EDGE_FILTER,
kernels.TOP_SOBEL,
kernels.EMBOSS_FILTER]
kernels_list = kernels_list[1:]
image = read_one_image('data/images/naruto.jpeg')
if not rgb:
image = tf.image.rgb_to_grayscale(image)
image = tf.expand_dims(image, 0) # make it into a batch of 1 element
images = convolve(image, kernels_list, rgb)
with tf.Session() as sess:
images = sess.run(images) # convert images from tensors to float values
show_images(images, rgb)
示例11: setup_graph
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import Session [as 别名]
def setup_graph(self, input_audio_batch, target_phrase):
batch_size = input_audio_batch.shape[0]
weird = (input_audio_batch.shape[1] - 1) // 320
logits_arg2 = np.tile(weird, batch_size)
dense_arg1 = np.array(np.tile(target_phrase, (batch_size, 1)), dtype=np.int32)
dense_arg2 = np.array(np.tile(target_phrase.shape[0], batch_size), dtype=np.int32)
pass_in = np.clip(input_audio_batch, -2**15, 2**15-1)
seq_len = np.tile(weird, batch_size).astype(np.int32)
with tf.variable_scope('', reuse=tf.AUTO_REUSE):
inputs = tf.placeholder(tf.float32, shape=pass_in.shape, name='a')
len_batch = tf.placeholder(tf.float32, name='b')
arg2_logits = tf.placeholder(tf.int32, shape=logits_arg2.shape, name='c')
arg1_dense = tf.placeholder(tf.float32, shape=dense_arg1.shape, name='d')
arg2_dense = tf.placeholder(tf.int32, shape=dense_arg2.shape, name='e')
len_seq = tf.placeholder(tf.int32, shape=seq_len.shape, name='f')
logits = get_logits(inputs, arg2_logits)
target = ctc_label_dense_to_sparse(arg1_dense, arg2_dense, len_batch)
ctcloss = tf.nn.ctc_loss(labels=tf.cast(target, tf.int32), inputs=logits, sequence_length=len_seq)
decoded, _ = tf.nn.ctc_greedy_decoder(logits, arg2_logits, merge_repeated=True)
sess = tf.Session()
saver = tf.train.Saver(tf.global_variables())
saver.restore(sess, "models/session_dump")
func1 = lambda a, b, c, d, e, f: sess.run(ctcloss,
feed_dict={inputs: a, len_batch: b, arg2_logits: c, arg1_dense: d, arg2_dense: e, len_seq: f})
func2 = lambda a, b, c, d, e, f: sess.run([ctcloss, decoded],
feed_dict={inputs: a, len_batch: b, arg2_logits: c, arg1_dense: d, arg2_dense: e, len_seq: f})
return (func1, func2)
示例12: make_app
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import Session [as 别名]
def make_app(model_dir):
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 1.0
config.allow_soft_placement = True
config.log_device_placement = True
sess = tf.Session(config=config)
tagger = Tagger(sess=sess, model_dir=model_dir, scope=TASK.scope, batch_size=200)
return tornado.web.Application([
(r"/", MainHandler),
(r"/%s" % TASK.scope, TaskHandler, {'tagger': tagger})
])
示例13: load
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import Session [as 别名]
def load(self, ckpt, ignore):
meta = ckpt + '.meta'
with tf.Graph().as_default() as graph:
with tf.Session().as_default() as sess:
saver = tf.train.import_meta_graph(meta)
saver.restore(sess, ckpt)
for var in tf.global_variables():
name = var.name.split(':')[0]
packet = [name, var.get_shape().as_list()]
self.src_key += [packet]
self.vals += [var.eval(sess)]
示例14: setup_meta_ops
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import Session [as 别名]
def setup_meta_ops(self):
cfg = dict({
'allow_soft_placement': False,
'log_device_placement': False
})
utility = min(self.FLAGS.gpu, 1.)
if utility > 0.0:
self.say('GPU mode with {} usage'.format(utility))
cfg['gpu_options'] = tf.GPUOptions(
per_process_gpu_memory_fraction = utility)
cfg['allow_soft_placement'] = True
else:
self.say('Running entirely on CPU')
cfg['device_count'] = {'GPU': 0}
if self.FLAGS.train: self.build_train_op()
if self.FLAGS.summary:
self.summary_op = tf.summary.merge_all()
self.writer = tf.summary.FileWriter(self.FLAGS.summary + 'train')
self.sess = tf.Session(config = tf.ConfigProto(**cfg))
self.sess.run(tf.global_variables_initializer())
if not self.ntrain: return
self.saver = tf.train.Saver(tf.global_variables(),
max_to_keep = self.FLAGS.keep)
if self.FLAGS.load != 0: self.load_from_ckpt()
if self.FLAGS.summary:
self.writer.add_graph(self.sess.graph)
示例15: init_tf
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import Session [as 别名]
def init_tf(config_dict=dict()):
if tf.get_default_session() is None:
tf.set_random_seed(np.random.randint(1 << 31))
create_session(config_dict, force_as_default=True)
#----------------------------------------------------------------------------
# Create tf.Session based on config dict of the form
# {'gpu_options.allow_growth': True}