def sample(self, amount, temperature=1):
priorities = self.buffer.buffers['priority'].value()[:self.size()]
logprobs = tf.log(priorities / tf.reduce_sum(priorities)) / temperature
positions = tf.multinomial(logprobs[None, ...], amount)[0]
return [ tf.gather(b, positions) for key,b in self.buffer.buffers.items() if key != 'priority' ]
python类multinomial()的实例源码
def sample(self, dist_info):
prob = dist_info["prob"]
ids = tf.multinomial(tf.log(prob + TINY), num_samples=1)[:, 0]
onehot = tf.constant(np.eye(self.dim, dtype=np.float32))
return tf.nn.embedding_lookup(onehot, ids)
def sample(self):
return tf.multinomial(self.inputs, 1)[0]
def sampler(symbols_to_logits_fn, initial_ids, sample_num, decode_length,
vocab_size, eos_id, features=None):
batch_size = tf.shape(initial_ids)[0]
# Expand each batch to sample_num
seqlen = tf.constant(0)
alive_seq = tf.tile(tf.expand_dims(initial_ids, 1), [1, sample_num])
alive_seq = tf.expand_dims(alive_seq, 2) # (batch_size, sample_num, 1)
sa = tf.shape(alive_seq)
alive_seq = tf.reshape(alive_seq, [sa[0]*sa[1],1])
def _is_finished(i, alive_seq):
return i < decode_length
def inner_loop(i, alive_seq):
logit = symbols_to_logits_fn(alive_seq)[0]
new_samples = tf.multinomial(logit, 1)
new_samples = tf.to_int32(new_samples)
alive_seq = tf.concat([alive_seq, new_samples], 1)
return (i + 1, alive_seq)
(_, alive_seq) = tf.while_loop(
_is_finished,
inner_loop,
[seqlen, alive_seq],
shape_invariants=[
tf.TensorShape([]),
tf.TensorShape([None, None])
],
parallel_iterations=1,
back_prop=False
)
alive_seq.set_shape((sample_num, None))
return alive_seq
def __init__(self, dim):
self._dim = dim
weights_var = tf.placeholder(
dtype=tf.float32,
shape=(None, dim),
name="weights"
)
self._f_sample = tensor_utils.compile_function(
inputs=[weights_var],
outputs=tf.multinomial(weights_var, num_samples=1)[:, 0],
)
def sample_sym(self, dist_info):
probs = dist_info["prob"]
samples = tf.multinomial(tf.log(probs + 1e-8), num_samples=1)[:, 0]
return tf.nn.embedding_lookup(np.eye(self.dim, dtype=np.float32), samples)
image_caption.py 文件源码
项目:Optimization_of_image_description_metrics_using_policy_gradient_methods
作者: chenxinpeng
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def Monte_Carlo_Rollout(self):
images = tf.placeholder(tf.float32, [1, self.feats_dim])
images_embed = tf.matmul(images, self.encode_img_W) + self.encode_img_b
state = self.lstm.zero_state(batch_size=1, dtype=tf.float32)
gen_sentences = []
all_sample_sentences = []
with tf.variable_scope("LSTM"):
output, state = self.lstm(images_embed, state)
with tf.device("/cpu:0"):
current_emb = tf.nn.embedding_lookup(self.Wemb, tf.ones([1], dtype=tf.int64))
for i in range(0, self.lstm_step):
tf.get_variable_scope().reuse_variables()
output, state = self.lstm(current_emb, state)
logit_words = tf.matmul(output, self.embed_word_W) + self.embed_word_b
max_prob_word = tf.argmax(logit_words, 1)[0]
with tf.device("/cpu:0"):
current_emb = tf.nn.embedding_lookup(self.Wemb, max_prob_word)
current_emb = tf.expand_dims(current_emb, 0)
gen_sentences.append(max_prob_word)
if i < self.lstm_step-1:
num_sample = self.lstm_step - 1 - i
sample_sentences = []
for idx_sample in range(num_sample):
sample = tf.multinomial(logit_words, 3)
sample_sentences.append(sample[0])
all_sample_sentences.append(sample_sentences)
return images, gen_sentences, all_sample_sentences
########################################################################
#
# Class function for step 4
#
########################################################################
image_caption.py 文件源码
项目:Optimization_of_image_description_metrics_using_policy_gradient_methods
作者: chenxinpeng
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def Monte_Carlo_and_Baseline(self):
images = tf.placeholder(tf.float32, [self.batch_size, self.feats_dim])
images_embed = tf.matmul(images, self.encode_img_W) + self.encode_img_b
state = self.lstm.zero_state(batch_size=self.batch_size, dtype=tf.float32)
gen_sentences = []
all_sample_sentences = []
all_baselines = []
with tf.variable_scope("LSTM"):
output, state = self.lstm(images_embed, state)
with tf.device("/cpu:0"):
current_emb = tf.nn.embedding_lookup(self.Wemb, tf.ones([self.batch_size], dtype=tf.int64))
for i in range(0, self.lstm_step):
tf.get_variable_scope().reuse_variables()
output, state = self.lstm(current_emb, state)
logit_words = tf.matmul(output, self.embed_word_W) + self.embed_word_b
max_prob_word = tf.argmax(logit_words, 1)
with tf.device("/cpu:0"):
current_emb = tf.nn.embedding_lookup(self.Wemb, max_prob_word)
#current_emb = tf.expand_dims(current_emb, 0)
gen_sentences.append(max_prob_word)
# compute Q for gt with K Monte Carlo rollouts
if i < self.lstm_step-1:
num_sample = self.lstm_step - 1 - i
sample_sentences = []
for idx_sample in range(num_sample):
sample = tf.multinomial(logit_words, 3)
sample_sentences.append(sample)
all_sample_sentences.append(sample_sentences)
# compute eatimated baseline
baseline = tf.nn.relu(tf.matmul(state[1], self.baseline_MLP_W) + self.baseline_MLP_b)
all_baselines.append(baseline)
return images, gen_sentences, all_sample_sentences, all_baselines
def random_multinomial(logits, seed=None):
'''
Theano function for sampling from a multinomal with probability given by `logits`
'''
if K.backend() == "theano":
if seed is None:
seed = numpy.random.randint(1, 10e6)
rng = RandomStreams(seed=seed)
return rng.multinomial(n=1, pvals=logits, ndim=None, dtype=_FLOATX)
elif K.backend() == "tensorflow":
return tf.one_hot(tf.squeeze(tf.multinomial(K.log(logits), num_samples=1)),
int(logits.shape[1]))
loopprocessing.py 文件源码
项目:How_to_generate_music_in_tensorflow_LIVE
作者: llSourcell
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def __call__(self, prev_output):
""" Use TODO formula
Args:
prev_output (tf.Tensor): the ouput on which applying the transformation
Return:
tf.Ops: the processing operator
"""
# prev_output size: [batch_size, nb_labels]
nb_labels = prev_output.get_shape().as_list()[-1]
if False: # TODO: Add option to control argmax
#label_draws = tf.argmax(prev_output, 1)
label_draws = tf.multinomial(tf.log(prev_output), 1) # Draw 1 sample from the distribution
label_draws = tf.squeeze(label_draws, [1])
self.chosen_labels.append(label_draws)
next_input = tf.one_hot(label_draws, nb_labels)
return next_input
# Could use the Gumbel-Max trick to sample from a softmax distribution ?
soft_values = tf.exp(tf.div(prev_output, self.temperature)) # Pi = exp(pi/t)
# soft_values size: [batch_size, nb_labels]
normalisation_coeff = tf.expand_dims(tf.reduce_sum(soft_values, 1), -1)
# normalisation_coeff size: [batch_size, 1]
probs = tf.div(soft_values, normalisation_coeff + 1e-8) # = Pi / sum(Pk)
# probs size: [batch_size, nb_labels]
label_draws = tf.multinomial(tf.log(probs), 1) # Draw 1 sample from the log-probability distribution
# probs label_draws: [batch_size, 1]
label_draws = tf.squeeze(label_draws, [1])
# label_draws size: [batch_size,]
self.chosen_labels.append(label_draws)
next_input = tf.one_hot(label_draws, nb_labels) # Reencode the next input vector
# next_input size: [batch_size, nb_labels]
return next_input
def multinomial_sample(x, vocab_size, temperature):
"""Multinomial sampling from a n-dimensional tensor."""
if temperature > 0:
samples = tf.multinomial(tf.reshape(x, [-1, vocab_size]) / temperature, 1)
else:
samples = tf.argmax(x, axis=-1)
reshaped_samples = tf.reshape(samples, common_layers.shape_list(x)[:-1])
return tf.to_int32(reshaped_samples)
def sample(self, features):
"""Run the model and extract samples.
Args:
features: an map of string to `Tensor`.
Returns:
samples: an integer `Tensor`.
logits: a list of `Tensor`s, one per datashard.
losses: a dictionary: {loss-name (string): floating point `Scalar`}.
"""
logits, losses = self(features) # pylint: disable=not-callable
if self.hparams.sampling_method == "argmax":
samples = tf.argmax(logits, axis=-1)
else:
assert self.hparams.sampling_method == "random"
def multinomial_squeeze(logits, temperature=1.0):
logits_shape = common_layers.shape_list(logits)
reshaped_logits = (
tf.reshape(logits, [-1, logits_shape[-1]]) / temperature)
choices = tf.multinomial(reshaped_logits, 1)
choices = tf.reshape(choices, logits_shape[:-1])
return choices
samples = multinomial_squeeze(logits, self.hparams.sampling_temp)
return samples, logits, losses
def __init__(self, dim):
self._dim = dim
weights_var = tf.placeholder(
dtype=tf.float32,
shape=(None, dim),
name="weights"
)
self._f_sample = tensor_utils.compile_function(
inputs=[weights_var],
outputs=tf.multinomial(tf.log(weights_var + 1e-8), num_samples=1)[:, 0],
)
def sample_sym(self, dist_info):
probs = dist_info["prob"]
samples = tf.multinomial(tf.log(probs + 1e-8), num_samples=1)[:, 0]
return tf.nn.embedding_lookup(np.eye(self.dim, dtype=np.float32), samples)
def __init__(self, dim):
'''
DESCRIPTION:
multinomial sample one output from the softmax probability
PARAM:
dim (int): layer dimension
'''
self.diag = tf.diag(tf.ones(dim))
def _train_fprop(self, state_below):
samples = tf.multinomial(state_below, num_samples=1)
samples = tf.squeeze(samples)
return tf.gather(self.diag, samples)
def __init__(self, dim):
self._dim = dim
weights_var = tf.placeholder(
dtype=tf.float32,
shape=(None, dim),
name="weights"
)
self._f_sample = tensor_utils.compile_function(
inputs=[weights_var],
outputs=tf.multinomial(weights_var, num_samples=1)[:, 0],
)
def sample_sym(self, dist_info):
probs = dist_info["prob"]
samples = tf.multinomial(tf.log(probs + 1e-8), num_samples=1)[:, 0]
return tf.nn.embedding_lookup(np.eye(self.dim, dtype=np.float32), samples)
def scale_distortions(image, gt_bboxes, gt_cats, params):
"""Samples a random box according to overlapping
with gt objects criteria and crops it from an image"""
image, gt_bboxes = tf.cond(tf.random_uniform([], 0, 1.0) < args.zoomout_prob,
lambda: zoomout(image, gt_bboxes, params),
lambda: (image, gt_bboxes))
n_channels = image.shape[-1]
def tf_random_choice(slices, bbox):
sample = tf.multinomial(tf.log([[10.]*len(slices)]), 1)
slices = tf.convert_to_tensor(slices)
bbox = tf.convert_to_tensor(bbox)
bbox_begin, bbox_size = tf.unstack(slices[tf.cast(sample[0][0],
tf.int32)])
distort_bbox = bbox[tf.cast(sample[0][0], tf.int32)]
return bbox_begin, bbox_size, distort_bbox
bboxes = tf.expand_dims(xywh_to_yxyx(gt_bboxes), 0)
samplers = []
boxes = []
for iou in params['sample_jaccards']:
sample_distorted_bounding_box = tf.image.sample_distorted_bounding_box(
tf.shape(image),
bounding_boxes=bboxes,
min_object_covered=iou,
aspect_ratio_range=[0.5, 2.0],
area_range=[0.3, 1.0],
max_attempts=params['crop_max_tries'],
use_image_if_no_bounding_boxes=True)
samplers.append(sample_distorted_bounding_box[:2])
boxes.append(sample_distorted_bounding_box[2][0][0])
bbox_begin, bbox_size, distort_bbox = tf_random_choice(samplers, boxes)
cropped_image = tf.slice(image, bbox_begin, bbox_size)
# Nope TF, you are wrong, cropping does not change channels.
cropped_image.set_shape([None, None, n_channels])
y1, x1, y2, x2 = tf.unstack(distort_bbox)
def check(center, mini, maxi):
return tf.logical_and((center >= mini), (center <= maxi))
gt_centers = gt_bboxes[:, :2] + gt_bboxes[:, 2:] / 2
mask = tf.logical_and(check(gt_centers[:, 0], x1, x2),
check(gt_centers[:, 1], y1, y2))
gt_bboxes = tf.boolean_mask(gt_bboxes, mask)
gt_cats = tf.boolean_mask(gt_cats, mask)
w = tf.to_float(x2-x1)
h = tf.to_float(y2-y1)
gt_x, gt_y, gt_w, gt_h = tf.unstack(gt_bboxes, axis=1)
gt_x2 = gt_x + gt_w
gt_y2 = gt_y + gt_h
gt_x1_clip = tf.clip_by_value(gt_x - x1, 0, w)/w
gt_x2_clip = tf.clip_by_value(gt_x2 - x1, 0, w)/w
gt_y1_clip = tf.clip_by_value(gt_y - y1, 0, h)/h
gt_y2_clip = tf.clip_by_value(gt_y2 - y1, 0, h)/h
gt_w_clip = gt_x2_clip - gt_x1_clip
gt_h_clip = gt_y2_clip - gt_y1_clip
gt_bboxes = tf.stack([gt_x1_clip, gt_y1_clip, gt_w_clip, gt_h_clip],
axis=1)
return cropped_image, gt_bboxes, gt_cats
def create_variables(self):
with tf.name_scope("model_inputs"):
# raw state representation
self.states = tf.placeholder(tf.float32, (None, self.state_dim), name="states")
# rollout action based on current policy
with tf.name_scope("predict_actions"):
# initialize policy network
with tf.variable_scope("policy_network"):
self.policy_outputs = self.policy_network(self.states)
# predict actions from policy network
self.action_scores = tf.identity(self.policy_outputs, name="action_scores")
# Note 1: tf.multinomial is not good enough to use yet
# so we don't use self.predicted_actions for now
self.predicted_actions = tf.multinomial(self.action_scores, 1)
# regularization loss
policy_network_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="policy_network")
# compute loss and gradients
with tf.name_scope("compute_pg_gradients"):
# gradients for selecting action from policy network
self.taken_actions = tf.placeholder(tf.int32, (None,), name="taken_actions")
self.discounted_rewards = tf.placeholder(tf.float32, (None,), name="discounted_rewards")
with tf.variable_scope("policy_network", reuse=True):
self.logprobs = self.policy_network(self.states)
# compute policy loss and regularization loss
self.cross_entropy_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.logprobs, labels=self.taken_actions)
self.pg_loss = tf.reduce_mean(self.cross_entropy_loss)
self.reg_loss = tf.reduce_sum([tf.reduce_sum(tf.square(x)) for x in policy_network_variables])
self.loss = self.pg_loss + self.reg_param * self.reg_loss
# compute gradients
self.gradients = self.optimizer.compute_gradients(self.loss)
# compute policy gradients
for i, (grad, var) in enumerate(self.gradients):
if grad is not None:
self.gradients[i] = (grad * self.discounted_rewards, var)
for grad, var in self.gradients:
tf.summary.histogram(var.name, var)
if grad is not None:
tf.summary.histogram(var.name + '/gradients', grad)
# emit summaries
tf.summary.scalar("policy_loss", self.pg_loss)
tf.summary.scalar("reg_loss", self.reg_loss)
tf.summary.scalar("total_loss", self.loss)
# training update
with tf.name_scope("train_policy_network"):
# apply gradients to update policy network
self.train_op = self.optimizer.apply_gradients(self.gradients)
self.summarize = tf.summary.merge_all()
self.no_op = tf.no_op()