本文整理汇总了Python中tensorflow.session方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.session方法的具体用法?Python tensorflow.session怎么用?Python tensorflow.session使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.session方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: initialize_helpers
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import session [as 别名]
def initialize_helpers(self):
"""Initialize the model and call all graph constructing ops.
This function is a wrapper for all initialization ops in the model.
"""
if self._opts._allowsoftplacement == 'True':
config = tf.ConfigProto(allow_soft_placement=True)
else:
config = tf.ConfigProto(allow_soft_placement=False)
# allow growth to surveil the consumed GPU memory
config.gpu_options.allow_growth = True
# open a session:
self.session = tf.Session(config=config)
self.log_file.write('Initialized Batch_Generator with MODE: %s\n' % self._opts._batchgenmode)
self.batchgen = helpers.BatchGenerator(self._opts)
self.log_file.write('Initialized ROC_tracker\n')
self.ROCtracker = helpers.RocTracker(self._opts)
示例2: _output_training_stat
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import session [as 别名]
def _output_training_stat(self,
X: np.ndarray,
intents_for_X: np.ndarray,
is_training: 'tf.Tensor') -> np.ndarray:
"""Output training statistics"""
n = self.evaluate_on_num_examples
ids = np.random.permutation(len(X))[:n]
all_Y = self._create_all_Y(X[ids].shape[0])
train_sim = self.session.run(self.sim_op,
feed_dict={self.a_in: X[ids],
self.b_in: all_Y,
is_training: False})
train_acc = np.mean(np.argmax(train_sim, -1) == intents_for_X[ids])
return train_acc
示例3: _calculate_message_sim
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import session [as 别名]
def _calculate_message_sim(self,
X: np.ndarray,
all_Y: np.ndarray
) -> Tuple[np.ndarray, List[float]]:
"""Load tf graph and calculate message similarities"""
message_sim = self.session.run(self.sim_op,
feed_dict={self.a_in: X,
self.b_in: all_Y})
message_sim = message_sim.flatten() # sim is a matrix
intent_ids = message_sim.argsort()[::-1]
message_sim[::-1].sort()
if self.similarity_type == 'cosine':
# clip negative values to zero
message_sim[message_sim < 0] = 0
elif self.similarity_type == 'inner':
# normalize result to [0, 1] with softmax
message_sim = np.exp(message_sim)
message_sim /= np.sum(message_sim)
# transform sim to python list for JSON serializing
return intent_ids, message_sim.tolist()
示例4: _calculate_message_sim
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import session [as 别名]
def _calculate_message_sim(self, X, all_Y):
"""Load tf graph and calculate message similarities"""
a_in = self.embedding_placeholder
b_in = self.intent_placeholder
sim = self.similarity_op
sess = self.session
message_sim = sess.run(sim, feed_dict={a_in: X,
b_in: all_Y})
message_sim = message_sim.flatten() # sim is a matrix
intent_ids = message_sim.argsort()[::-1]
message_sim[::-1].sort()
# transform sim to python list for JSON serializing
message_sim = message_sim.tolist()
return intent_ids, message_sim
示例5: predict
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import session [as 别名]
def predict(self, feed, batch_size=None):
""" Get predictions from the model in the correct session.
This method is a wrapper for :func:`keras.predict()` function.
Parameters
----------
feed: numpy.ndarray or list
The feed to be provided to the model as input. This should be a ``numpy.ndarray``
for single inputs or a ``list`` of ``numpy.ndarrays`` for multiple inputs.
"""
if self._session is None:
if batch_size is None:
return self._model.predict(feed)
return self._amd_predict_with_optimized_batchsizes(feed, batch_size)
with self._session.as_default(): # pylint: disable=not-context-manager
with self._session.graph.as_default():
return self._model.predict(feed, batch_size=batch_size)
示例6: _set_session
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import session [as 别名]
def _set_session(self, allow_growth):
""" Sets the session and graph.
If the backend is AMD then this does nothing and the global ``Keras`` ``Session``
is used
"""
if get_backend() == "amd":
return None
self.graph = tf.Graph()
config = tf.ConfigProto()
if allow_growth and get_backend() == "nvidia":
config.gpu_options.allow_growth = True
try:
session = tf.Session(graph=tf.Graph(), config=config)
except tf_error.InternalError as err:
if "driver version is insufficient" in str(err):
msg = ("Your Nvidia Graphics Driver is insufficient for running Faceswap. "
"Please upgrade to the latest version.")
raise FaceswapError(msg) from err
raise err
logger.debug("Created tf.session: (graph: %s, session: %s, config: %s)",
session.graph, session, config)
return session
示例7: define_model
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import session [as 别名]
def define_model(self, function):
""" Defines a given model in the correct session.
This method acts as a wrapper for :class:`keras.models.Model()` to ensure that the model
is defined within it's own graph.
Parameters
----------
function: function
A function that defines a :class:`keras.Model` and returns it's ``inputs`` and
``outputs``. The function that generates these results should be passed in, NOT the
results themselves, as the function needs to be executed within the correct context.
"""
if self._session is None:
self._model = Model(*function())
else:
with self._session.as_default(): # pylint: disable=not-context-manager
with self._session.graph.as_default():
self._model = Model(*function())
示例8: run_generator
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import session [as 别名]
def run_generator(self, sess, local_lr):
"""
Runs generator part of GAN
Arguments:
sess(tf.session): Tensorflow Session
local_lr(float): Learning rate
Returns:
Returns number of words processed
"""
# Generate random ids to look up
src_ids = np.random.choice(self.vocab_size, self.batch_size, replace=False)
tgt_ids = np.random.choice(self.vocab_size, self.batch_size, replace=False)
train_dict = {
self.generator.src_ph: src_ids,
self.generator.tgt_ph: tgt_ids,
self.discriminator.do_ph: 1.0,
self.lr_ph: local_lr,
}
sess.run(self.generator.map_opt, feed_dict=train_dict)
# Run orthogonalize
sess.run(self.generator.assign_weight)
return 2 * self.batch_size
示例9: run_discriminator
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import session [as 别名]
def run_discriminator(self, sess, local_lr):
"""
Runs discriminator part of GAN
Arguments:
sess(tf.session): Tensorflow Session
local_lr(float): Learning rate
"""
# Generate random ids to look up
src_ids = np.random.choice(self.most_freq, self.batch_size, replace=False)
tgt_ids = np.random.choice(self.most_freq, self.batch_size, replace=False)
train_dict = {
self.generator.src_ph: src_ids,
self.generator.tgt_ph: tgt_ids,
self.discriminator.do_ph: 0.9,
self.lr_ph: local_lr,
}
return sess.run(
[self.discriminator.disc_cost, self.discriminator.disc_opt], feed_dict=train_dict
)
示例10: run
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import session [as 别名]
def run(self, sess, local_lr):
"""
Runs whole GAN
Arguments:
sess(tf.session): Tensorflow Session
local_lr(float): Learning rate
"""
disc_cost_acc = []
n_words_proc = 0
tic = time.time()
for iters in range(0, self.iters_epoch, self.batch_size):
# 1.Run the discriminator
for _ in range(self.disc_runs):
disc_result = self.run_discriminator(sess, local_lr)
disc_cost_acc.append(disc_result[0])
# 2.Run the Generator
n_words_proc += self.run_generator(sess, local_lr)
# 3.Report the metrics
self.report_metrics(iters, n_words_proc, disc_cost_acc, tic)
示例11: apply_procrustes
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import session [as 别名]
def apply_procrustes(self, sess, final_pairs):
"""
Applies procrustes to W matrix for better mapping
Arguments:
sess(tf.session): Tensorflow Session
final_pairs(ndarray): Array of pairs which are mutual neighbors
"""
print("Applying solution of Procrustes problem to get better mapping...")
proc_dict = {
self.generator.src_ph: final_pairs[:, 0],
self.generator.tgt_ph: final_pairs[:, 1],
}
A, B = sess.run([self.generator.src_emb, self.generator.tgt_emb], feed_dict=proc_dict)
# pylint: disable=no-member
R = scipy.linalg.orthogonal_procrustes(A, B)
sess.run(tf.assign(self.generator.W, R[0]))
示例12: calc_nn_acc
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import session [as 别名]
def calc_nn_acc(self, sess, batch_size=512):
"""
Evaluates accuracy of mapping using Nearest neighbors
Arguments:
sess(tf.session): Tensorflow Session
batch_size(int): Size of batch
"""
top_matches = []
eval_size = len(self.src_ind)
# Loop through all the eval dataset
for i in range(0, eval_size, batch_size):
src_ids = [self.src_ind[x] for x in range(i, min(i + batch_size, eval_size))]
eval_dict = {self.src_ph: src_ids, self.tgt_ph: self.tgt_ids}
matches = sess.run(self.eval_nn, feed_dict=eval_dict)
top_matches.append(matches[1])
top_matches = np.concatenate(top_matches)
print("Accuracy using Nearest Neighbors is")
self.calc_accuracy(top_matches)
示例13: calc_csls_score
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import session [as 别名]
def calc_csls_score(self, sess, batch_size=512):
"""
Calculates similarity score between two embeddings
Arguments:
sess(tf.session): Tensorflow Session
batch_size(int): Size of batch to process
Returns:
Returns similarity score numpy array
"""
score_val = []
eval_size = len(self.src_ind)
# Calculate scores
for i in range(0, eval_size, batch_size):
score_src_ids = [self.src_ind[x] for x in range(i, min(i + batch_size, eval_size))]
eval_dict = {self.src_ph: score_src_ids, self.tgt_ph: self.tgt_ids}
score_val.append(sess.run(self.csls_subgraphs["ScoreGraph"], feed_dict=eval_dict))
score_val = np.concatenate(score_val)
return score_val
示例14: calc_avg_dist
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import session [as 别名]
def calc_avg_dist(self, sess, batch_size=512):
"""
Calculates average distance between two embeddings
Arguments:
sess(tf.session): Tensorflow session
batch_size(int): batch_size
Returns:
Returns numpy array of average values of size vocab_size
"""
avg1_val = []
avg2_val = []
# Calculate Average
for i in range(0, self.vocab_size, batch_size):
avg_src_ids = [x for x in range(i, min(i + batch_size, self.vocab_size))]
avg1_dict = {self.src_ph: avg_src_ids, self.tgt_ph: self.tgt_ids}
avg1_val.append(sess.run(self.csls_subgraphs["Avg1S2T"], feed_dict=avg1_dict))
avg2_val.append(sess.run(self.csls_subgraphs["Avg2S2T"], feed_dict=avg1_dict))
avg1_val = np.concatenate(avg1_val)
avg2_val = np.concatenate(avg2_val)
return avg1_val, avg2_val
示例15: run_csls_metrics
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import session [as 别名]
def run_csls_metrics(self, sess, batch_size=512):
"""
Runs the whole CSLS metrics
Arguments:
sess(tf.session): Tensorflow Session
batch_size(int): Batch Size
"""
top_matches = []
score = self.calc_csls_score(sess)
avg1, avg2 = self.calc_avg_dist(sess)
csls_scores = 2 * score - (avg1[self.src_ind][:, None] + avg2[None, :])
# Calculate top matches
for i in range(0, len(self.src_ind), batch_size):
scores = [csls_scores[x] for x in range(i, min(i + batch_size, len(self.src_ind)))]
top_matches_val = sess.run(
self.csls_subgraphs["Top100"], feed_dict={self.score_ph: scores}
)[1]
top_matches.append(top_matches_val)
top_matches = np.concatenate(top_matches)
print("Accuracy using CSLS is")
self.calc_accuracy(top_matches)
self.calc_csls(sess)