本文整理汇总了Python中model.NamignizerModel方法的典型用法代码示例。如果您正苦于以下问题:Python model.NamignizerModel方法的具体用法?Python model.NamignizerModel怎么用?Python model.NamignizerModel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类model
的用法示例。
在下文中一共展示了model.NamignizerModel方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_epoch
# 需要导入模块: import model [as 别名]
# 或者: from model import NamignizerModel [as 别名]
def run_epoch(session, m, names, counts, epoch_size, eval_op, verbose=False):
"""Runs the model on the given data for one epoch
Args:
session: the tf session holding the model graph
m: an instance of the NamignizerModel
names: a set of lowercase names of 26 characters
counts: a list of the frequency of the above names
epoch_size: the number of batches to run
eval_op: whether to change the params or not, and how to do it
Kwargs:
verbose: whether to print out state of training during the epoch
Returns:
cost: the average cost during the last stage of the epoch
"""
start_time = time.time()
costs = 0.0
iters = 0
for step, (x, y) in enumerate(data_utils.namignizer_iterator(names, counts,
m.batch_size, m.num_steps, epoch_size)):
cost, _ = session.run([m.cost, eval_op],
{m.input_data: x,
m.targets: y,
m.weights: np.ones(m.batch_size * m.num_steps)})
costs += cost
iters += m.num_steps
if verbose and step % (epoch_size // 10) == 9:
print("%.3f perplexity: %.3f speed: %.0f lps" %
(step * 1.0 / epoch_size, np.exp(costs / iters),
iters * m.batch_size / (time.time() - start_time)))
if step >= epoch_size:
break
return np.exp(costs / iters)
示例2: train
# 需要导入模块: import model [as 别名]
# 或者: from model import NamignizerModel [as 别名]
def train(data_dir, checkpoint_path, config):
"""Trains the model with the given data
Args:
data_dir: path to the data for the model (see data_utils for data
format)
checkpoint_path: the path to save the trained model checkpoints
config: one of the above configs that specify the model and how it
should be run and trained
Returns:
None
"""
# Prepare Name data.
print("Reading Name data in %s" % data_dir)
names, counts = data_utils.read_names(data_dir)
with tf.Graph().as_default(), tf.Session() as session:
initializer = tf.random_uniform_initializer(-config.init_scale,
config.init_scale)
with tf.variable_scope("model", reuse=None, initializer=initializer):
m = NamignizerModel(is_training=True, config=config)
tf.global_variables_initializer().run()
for i in range(config.max_max_epoch):
lr_decay = config.lr_decay ** max(i - config.max_epoch, 0.0)
m.assign_lr(session, config.learning_rate * lr_decay)
print("Epoch: %d Learning rate: %.3f" % (i + 1, session.run(m.lr)))
train_perplexity = run_epoch(session, m, names, counts, config.epoch_size, m.train_op,
verbose=True)
print("Epoch: %d Train Perplexity: %.3f" %
(i + 1, train_perplexity))
m.saver.save(session, checkpoint_path, global_step=i)
示例3: namignize
# 需要导入模块: import model [as 别名]
# 或者: from model import NamignizerModel [as 别名]
def namignize(names, checkpoint_path, config):
"""Recognizes names and prints the Perplexity of the model for each names
in the list
Args:
names: a list of names in the model format
checkpoint_path: the path to restore the trained model from, should not
include the model name, just the path to
config: one of the above configs that specify the model and how it
should be run and trained
Returns:
None
"""
with tf.Graph().as_default(), tf.Session() as session:
with tf.variable_scope("model"):
m = NamignizerModel(is_training=False, config=config)
m.saver.restore(session, checkpoint_path)
for name in names:
x, y = data_utils.name_to_batch(name, m.batch_size, m.num_steps)
cost, loss, _ = session.run([m.cost, m.loss, tf.no_op()],
{m.input_data: x,
m.targets: y,
m.weights: np.concatenate((
np.ones(len(name)), np.zeros(m.batch_size * m.num_steps - len(name))))})
print("Name {} gives us a perplexity of {}".format(
name, np.exp(cost)))
示例4: namignator
# 需要导入模块: import model [as 别名]
# 或者: from model import NamignizerModel [as 别名]
def namignator(checkpoint_path, config):
"""Generates names randomly according to a given model
Args:
checkpoint_path: the path to restore the trained model from, should not
include the model name, just the path to
config: one of the above configs that specify the model and how it
should be run and trained
Returns:
None
"""
# mutate the config to become a name generator config
config.num_steps = 1
config.batch_size = 1
with tf.Graph().as_default(), tf.Session() as session:
with tf.variable_scope("model"):
m = NamignizerModel(is_training=False, config=config)
m.saver.restore(session, checkpoint_path)
activations, final_state, _ = session.run([m.activations, m.final_state, tf.no_op()],
{m.input_data: np.zeros((1, 1)),
m.targets: np.zeros((1, 1)),
m.weights: np.ones(1)})
# sample from our softmax activations
next_letter = np.random.choice(27, p=activations[0])
name = [next_letter]
while next_letter != 0:
activations, final_state, _ = session.run([m.activations, m.final_state, tf.no_op()],
{m.input_data: [[next_letter]],
m.targets: np.zeros((1, 1)),
m.initial_state: final_state,
m.weights: np.ones(1)})
next_letter = np.random.choice(27, p=activations[0])
name += [next_letter]
print(map(lambda x: chr(x + 96), name))
示例5: run_epoch
# 需要导入模块: import model [as 别名]
# 或者: from model import NamignizerModel [as 别名]
def run_epoch(session, m, names, counts, epoch_size, eval_op, verbose=False):
"""Runs the model on the given data for one epoch
Args:
session: the tf session holding the model graph
m: an instance of the NamignizerModel
names: a set of lowercase names of 26 characters
counts: a list of the frequency of the above names
epoch_size: the number of batches to run
eval_op: whether to change the params or not, and how to do it
Kwargs:
verbose: whether to print out state of training during the epoch
Returns:
cost: the average cost during the last stage of the epoch
"""
start_time = time.time()
costs = 0.0
iters = 0
for step, (x, y) in enumerate(data_utils.namignizer_iterator(names, counts,
m.batch_size, m.num_steps, epoch_size)):
cost, _ = session.run([m.cost, eval_op],
{m.input_data: x,
m.targets: y,
m.initial_state: m.initial_state.eval(),
m.weights: np.ones(m.batch_size * m.num_steps)})
costs += cost
iters += m.num_steps
if verbose and step % (epoch_size // 10) == 9:
print("%.3f perplexity: %.3f speed: %.0f lps" %
(step * 1.0 / epoch_size, np.exp(costs / iters),
iters * m.batch_size / (time.time() - start_time)))
if step >= epoch_size:
break
return np.exp(costs / iters)
示例6: train
# 需要导入模块: import model [as 别名]
# 或者: from model import NamignizerModel [as 别名]
def train(data_dir, checkpoint_path, config):
"""Trains the model with the given data
Args:
data_dir: path to the data for the model (see data_utils for data
format)
checkpoint_path: the path to save the trained model checkpoints
config: one of the above configs that specify the model and how it
should be run and trained
Returns:
None
"""
# Prepare Name data.
print("Reading Name data in %s" % data_dir)
names, counts = data_utils.read_names(data_dir)
with tf.Graph().as_default(), tf.Session() as session:
initializer = tf.random_uniform_initializer(-config.init_scale,
config.init_scale)
with tf.variable_scope("model", reuse=None, initializer=initializer):
m = NamignizerModel(is_training=True, config=config)
tf.initialize_all_variables().run()
for i in range(config.max_max_epoch):
lr_decay = config.lr_decay ** max(i - config.max_epoch, 0.0)
m.assign_lr(session, config.learning_rate * lr_decay)
print("Epoch: %d Learning rate: %.3f" % (i + 1, session.run(m.lr)))
train_perplexity = run_epoch(session, m, names, counts, config.epoch_size, m.train_op,
verbose=True)
print("Epoch: %d Train Perplexity: %.3f" %
(i + 1, train_perplexity))
m.saver.save(session, checkpoint_path, global_step=i)
示例7: namignize
# 需要导入模块: import model [as 别名]
# 或者: from model import NamignizerModel [as 别名]
def namignize(names, checkpoint_path, config):
"""Recognizes names and prints the Perplexity of the model for each names
in the list
Args:
names: a list of names in the model format
checkpoint_path: the path to restore the trained model from, should not
include the model name, just the path to
config: one of the above configs that specify the model and how it
should be run and trained
Returns:
None
"""
with tf.Graph().as_default(), tf.Session() as session:
with tf.variable_scope("model"):
m = NamignizerModel(is_training=False, config=config)
m.saver.restore(session, checkpoint_path)
for name in names:
x, y = data_utils.name_to_batch(name, m.batch_size, m.num_steps)
cost, loss, _ = session.run([m.cost, m.loss, tf.no_op()],
{m.input_data: x,
m.targets: y,
m.initial_state: m.initial_state.eval(),
m.weights: np.concatenate((
np.ones(len(name)), np.zeros(m.batch_size * m.num_steps - len(name))))})
print("Name {} gives us a perplexity of {}".format(
name, np.exp(cost)))