本文整理汇总了Python中torch.autograd.Variable.expand方法的典型用法代码示例。如果您正苦于以下问题:Python Variable.expand方法的具体用法?Python Variable.expand怎么用?Python Variable.expand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.autograd.Variable
的用法示例。
在下文中一共展示了Variable.expand方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TransformedGridLoss
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import expand [as 别名]
class TransformedGridLoss(nn.Module):
def __init__(self, geometric_model='affine', use_cuda=True, grid_size=20):
super(TransformedGridLoss, self).__init__()
self.geometric_model = geometric_model
# define virtual grid of points to be transformed
axis_coords = np.linspace(-1,1,grid_size)
self.N = grid_size*grid_size
X,Y = np.meshgrid(axis_coords,axis_coords)
X = np.reshape(X,(1,1,self.N))
Y = np.reshape(Y,(1,1,self.N))
P = np.concatenate((X,Y),1)
self.P = Variable(torch.FloatTensor(P),requires_grad=False)
self.pointTnf = PointTnf(use_cuda=use_cuda)
if use_cuda:
self.P = self.P.cuda();
def forward(self, theta, theta_GT):
# expand grid according to batch size
batch_size = theta.size()[0]
P = self.P.expand(batch_size,2,self.N)
# compute transformed grid points using estimated and GT tnfs
if self.geometric_model=='affine':
P_prime = self.pointTnf.affPointTnf(theta,P)
P_prime_GT = self.pointTnf.affPointTnf(theta_GT,P)
elif self.geometric_model=='tps':
P_prime = self.pointTnf.tpsPointTnf(theta.unsqueeze(2).unsqueeze(3),P)
P_prime_GT = self.pointTnf.tpsPointTnf(theta_GT,P)
# compute MSE loss on transformed grid points
loss = torch.sum(torch.pow(P_prime - P_prime_GT,2),1)
loss = torch.mean(loss)
return loss
示例2: accumulate_gradient
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import expand [as 别名]
def accumulate_gradient(self, batch_sz, states, actions, rewards,
next_states, mask):
""" Compute the difference between the return distributions of Q(s,a)
and TQ(s_,a).
"""
states = Variable(states)
actions = Variable(actions)
rewards = Variable(rewards)
next_states = Variable(next_states, volatile=True)
# Compute probabilities of Q(s,a*)
q_probs = self.policy(states)
actions = actions.view(batch_sz, 1, 1)
action_mask = actions.expand(batch_sz, 1, self.atoms_no)
qa_probs = q_probs.gather(1, action_mask).squeeze()
# Compute distribution of Q(s_,a)
target_qa_probs = self._get_categorical(next_states, rewards, mask)
# Compute the cross-entropy of phi(TZ(x_,a)) || Z(x,a)
qa_probs.data.clamp_(0.01, 0.99) # Tudor's trick for avoiding nans
loss = - torch.sum(target_qa_probs * torch.log(qa_probs))
# Accumulate gradients
loss.backward()
示例3: test_setitem_expansion_error
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import expand [as 别名]
def test_setitem_expansion_error(self):
true = variable(1).byte()
a = Variable(torch.randn(2, 3))
# check prefix with non-1s doesn't work
a_expanded = a.expand(torch.Size([5, 1]) + a.size())
with self.assertRaises(RuntimeError):
a[True] = a_expanded
with self.assertRaises(RuntimeError):
a[true] = torch.autograd.Variable(a_expanded)
示例4: enumerate_support
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import expand [as 别名]
def enumerate_support(self):
probs = self._categorical.probs
n = self.event_shape[0]
if isinstance(probs, Variable):
values = Variable(torch.eye(n, out=probs.data.new(n, n)))
else:
values = torch.eye(n, out=probs.new(n, n))
values = values.view((n,) + (1,) * len(self.batch_shape) + (n,))
return values.expand((n,) + self.batch_shape + (n,))
示例5: sample
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import expand [as 别名]
def sample(self):
"""
Ref: :py:meth:`pyro.distributions.distribution.Distribution.sample.`
"""
np_sample = spr.beta.rvs(self.alpha.data.cpu().numpy(), self.beta.data.cpu().numpy())
if isinstance(np_sample, numbers.Number):
np_sample = [np_sample]
x = Variable(torch.Tensor(np_sample).type_as(self.alpha.data))
x = x.expand(self.shape())
return x
示例6: learn
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import expand [as 别名]
def learn(self, optimizer, history_of_rewards, gamma, reward_to_go):
total_weighted_reward=Variable(torch.zeros(1,1))
gradient=Variable(torch.zeros(1,1))
loss=0
if !reward_to_go:
#sum up all the reward along the trajectory
for i in reversed(range(len(history_of_rewards))):
total_weighted_reward = gamma * total_weighted_reward + rewards[i]
gradient+=self.history_of_log_probs[i]
loss=loss-(gradient*total_weighted_reward.expand(gradient.size())).sum()
loss=loss/len(history_of_rewards) #in case the episode terminates early
else:
#reward to go mode
for i in reversed(range(len(history_of_rewards))):
total_weighted_reward=gamma*total_weighted_reward+rewards[i]
loss=loss-(self.history_of_log_probs[i]*total_weighted_reward.expand(self.history_of_log_probs[i].size())).sum()
loss=loss/len(history_of_rewards) #in case the episode terminates early
optimizer.zero_grad()
loss.backward()
optimizer.step()
self.history_of_log_probs=[]
示例7: normalize_image
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import expand [as 别名]
def normalize_image(image, forward=True, mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225]):
im_size = image.size()
mean=torch.FloatTensor(mean).unsqueeze(1).unsqueeze(2)
std=torch.FloatTensor(std).unsqueeze(1).unsqueeze(2)
if image.is_cuda:
mean = mean.cuda()
std = std.cuda()
if isinstance(image,torch.autograd.variable.Variable):
mean = Variable(mean,requires_grad=False)
std = Variable(std,requires_grad=False)
if forward:
if len(im_size)==3:
result = image.sub(mean.expand(im_size)).div(std.expand(im_size))
elif len(im_size)==4:
result = image.sub(mean.unsqueeze(0).expand(im_size)).div(std.unsqueeze(0).expand(im_size))
else:
if len(im_size)==3:
result = image.mul(std.expand(im_size)).add(mean.expand(im_size))
elif len(im_size)==4:
result = image.mul(std.unsqueeze(0).expand(im_size)).add(mean.unsqueeze(0).expand(im_size))
return result
示例8: TestDelta
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import expand [as 别名]
class TestDelta(TestCase):
def setUp(self):
self.v = Variable(torch.Tensor([3]))
self.vs = Variable(torch.Tensor([[0], [1], [2], [3]]))
self.vs_expanded = self.vs.expand(4, 3)
self.test_data = Variable(torch.Tensor([[3], [3], [3]]))
self.batch_test_data_1 = Variable(torch.arange(0, 4).unsqueeze(1).expand(4, 3))
self.batch_test_data_2 = Variable(torch.arange(4, 8).unsqueeze(1).expand(4, 3))
self.batch_test_data_3 = Variable(torch.Tensor([[3], [3], [3], [3]]))
self.expected_support = [[0], [1], [2], [3]]
self.expected_support_non_vec = [3]
self.analytic_mean = 3
self.analytic_var = 0
self.n_samples = 10
def test_log_pdf(self):
log_px_torch = dist.delta.log_pdf(self.test_data, self.v).data
self.assertEqual(torch.sum(log_px_torch), 0)
def test_batch_log_pdf(self):
log_px_torch = dist.delta.batch_log_pdf(self.batch_test_data_1, self.vs_expanded).data
self.assertEqual(torch.sum(log_px_torch), 0)
log_px_torch = dist.delta.batch_log_pdf(self.batch_test_data_2, self.vs_expanded).data
self.assertEqual(torch.sum(log_px_torch), float('-inf'))
def test_batch_log_pdf_shape(self):
assert dist.delta.batch_log_pdf(self.batch_test_data_3, self.vs).size() == (4, 1)
assert dist.delta.batch_log_pdf(self.batch_test_data_3, self.v).size() == (4, 1)
def test_mean_and_var(self):
torch_samples = [dist.delta(self.v).data.cpu().numpy()
for _ in range(self.n_samples)]
torch_mean = np.mean(torch_samples)
torch_var = np.var(torch_samples)
self.assertEqual(torch_mean, self.analytic_mean)
self.assertEqual(torch_var, self.analytic_var)
def test_support(self):
actual_support = dist.delta.enumerate_support(self.vs)
actual_support_non_vec = dist.delta.enumerate_support(self.v)
assert_equal(actual_support.data, torch.Tensor(self.expected_support))
assert_equal(actual_support_non_vec.data, torch.Tensor(self.expected_support_non_vec))
示例9: Variable
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import expand [as 别名]
#!/usr/bin/python3
import torch
from torch.autograd import Variable
# 3 dimension vector
# tensor0 length 6
# tensor1 length 1
tensor0 = Variable(torch.randn(1, 3, 6))
tensor1 = Variable(torch.randn(1, 3, 1))
K = 5
M = Variable(torch.randn(K, 3, 3))
batchTensor0 = tensor0.transpose(1, 2).expand(K, 6, 3)
batchTensor1 = tensor1.expand(K, 3, 1)
result = batchTensor0.bmm(M).bmm(batchTensor1)
print(result)
示例10: forward
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import expand [as 别名]
def forward(self, xs, ys=None, cands=None, valid_cands=None):
bsz = len(xs)
if ys is not None:
# keep track of longest label we've ever seen
# we'll never produce longer ones than that during prediction
self.longest_label = max(self.longest_label, ys.size(1))
enc_out, hidden = self.encoder(xs)
attn_mask = xs.ne(0).float() if self.attn_type != 'none' else None
start = Variable(self.START, requires_grad=False)
starts = start.expand(bsz, 1)
predictions = []
scores = []
text_cand_inds = None
if self.training:
y_in = ys.narrow(1, 0, ys.size(1) - 1)
xs = torch.cat([starts, y_in], 1)
if self.attn_type == 'none':
preds, score, _h = self.decoder(xs, hidden, enc_out, attn_mask)
predictions.append(preds)
scores.append(score)
else:
for i in range(ys.size(1)):
xi = xs.select(1, i)
preds, score, _h = self.decoder(xi, hidden, enc_out, attn_mask)
predictions.append(preds)
scores.append(score)
else:
# just predict
done = [False for _ in range(bsz)]
total_done = 0
xs = starts
for _ in range(self.longest_label):
# generate at most longest_label tokens
preds, score, hidden = self.decoder(xs, hidden, enc_out, attn_mask)
scores.append(score)
xs = preds
predictions.append(preds)
# check if we've produced the end token
for b in range(bsz):
if not done[b]:
# only add more tokens for examples that aren't done
if preds.data[b][0] == self.END_IDX:
# if we produced END, we're done
done[b] = True
total_done += 1
if total_done == bsz:
# no need to generate any more
break
if self.rank and cands is not None:
text_cand_inds = self.ranker(cands, valid_cands, start,
hidden, enc_out, attn_mask)
if predictions:
predictions = torch.cat(predictions, 1)
if scores:
scores = torch.cat(scores, 1)
return predictions, scores, text_cand_inds
示例11: train
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import expand [as 别名]
def train(self):
if self.T - self.target_sync_T > self.args.target:
self.sync_target_network()
self.target_sync_T = self.T
info = {}
for _ in range(self.args.iters):
self.dqn.eval()
batch, indices, is_weights = self.replay.Sample_N(self.args.batch_size, self.args.n_step, self.args.gamma)
columns = list(zip(*batch))
states = Variable(torch.from_numpy(np.array(columns[0])).float().transpose_(1, 3))
actions = Variable(torch.LongTensor(columns[1]))
terminal_states = Variable(torch.FloatTensor(columns[5]))
rewards = Variable(torch.FloatTensor(columns[2]))
# Have to clip rewards for DQN
rewards = torch.clamp(rewards, -1, 1)
steps = Variable(torch.FloatTensor(columns[4]))
new_states = Variable(torch.from_numpy(np.array(columns[3])).float().transpose_(1, 3))
target_dqn_qvals = self.target_dqn(new_states).cpu()
# Make a new variable with those values so that these are treated as constants
target_dqn_qvals_data = Variable(target_dqn_qvals.data)
q_value_gammas = (Variable(torch.ones(terminal_states.size()[0])) - terminal_states)
inter = Variable(torch.ones(terminal_states.size()[0]) * self.args.gamma)
# print(steps)
q_value_gammas = q_value_gammas * torch.pow(inter, steps)
values = torch.linspace(self.args.v_min, self.args.v_max, steps=self.args.atoms)
values = Variable(values)
values = values.view(1, 1, self.args.atoms)
values = values.expand(self.args.batch_size, self.args.actions, self.args.atoms)
# print(values)
q_value_gammas = q_value_gammas.view(self.args.batch_size, 1, 1)
q_value_gammas = q_value_gammas.expand(self.args.batch_size, self.args.actions, self.args.atoms)
# print(q_value_gammas)
gamma_values = q_value_gammas * values
# print(gamma_values)
rewards = rewards.view(self.args.batch_size, 1, 1)
rewards = rewards.expand(self.args.batch_size, self.args.actions, self.args.atoms)
# print(rewards)
operator_q_values = rewards + gamma_values
# print(operator_q_values)
clipped_operator_q_values = torch.clamp(operator_q_values, self.args.v_min, self.args.v_max)
delta_z = (self.args.v_max - self.args.v_min) / (self.args.atoms - 1)
# Using the notation from the categorical paper
b_j = (clipped_operator_q_values - self.args.v_min) / delta_z
# print(b_j)
lower_bounds = torch.floor(b_j)
upper_bounds = torch.ceil(b_j)
# Work out the max action
atom_values = Variable(torch.linspace(self.args.v_min, self.args.v_max, steps=self.args.atoms))
atom_values = atom_values.view(1, 1, self.args.atoms)
atom_values = atom_values.expand(self.args.batch_size, self.args.actions, self.args.atoms)
# Sum over the atoms dimension
target_expected_qvalues = torch.sum(target_dqn_qvals_data * atom_values, dim=2)
# Get the maximum actions index across the batch size
max_actions = target_expected_qvalues.max(dim=1)[1].view(-1)
# Project back onto the original support for the max actions
q_value_distribution_targets = torch.zeros(self.args.batch_size, self.args.atoms)
# Distributions for the max actions
# print(target_dqn_qvals_data, max_actions)
q_value_max_actions_distribs = target_dqn_qvals_data.index_select(dim=1, index=max_actions)[:,0,:]
# print(q_value_max_actions_distribs)
# Lower_bounds_actions
lower_bounds_actions = lower_bounds.index_select(dim=1, index=max_actions)[:,0,:]
upper_bounds_actions = upper_bounds.index_select(dim=1, index=max_actions)[:,0,:]
b_j_actions = b_j.index_select(dim=1, index=max_actions)[:,0,:]
lower_bound_values_to_add = q_value_max_actions_distribs * (upper_bounds_actions - b_j_actions)
upper_bound_values_to_add = q_value_max_actions_distribs * (b_j_actions - lower_bounds_actions)
# print(lower_bounds_actions)
# print(lower_bound_values_to_add)
# Naive looping
for b in range(self.args.batch_size):
for l, pj in zip(lower_bounds_actions.data.type(torch.LongTensor)[b], lower_bound_values_to_add[b].data):
q_value_distribution_targets[b][l] += pj
for u, pj in zip(upper_bounds_actions.data.type(torch.LongTensor)[b], upper_bound_values_to_add[b].data):
q_value_distribution_targets[b][u] += pj
self.dqn.train()
if self.args.gpu:
actions = actions.cuda()
# q_value_targets = q_value_targets.cuda()
q_value_distribution_targets = q_value_distribution_targets.cuda()
model_predictions = self.dqn(states).index_select(1, actions.view(-1))[:,0,:]
q_value_distribution_targets = Variable(q_value_distribution_targets)
# print(q_value_distribution_targets)
#.........这里部分代码省略.........
示例12: TpsGridGen
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import expand [as 别名]
class TpsGridGen(Module):
def __init__(self, out_h=240, out_w=240, use_regular_grid=True, grid_size=3, reg_factor=0, use_cuda=True):
super(TpsGridGen, self).__init__()
self.out_h, self.out_w = out_h, out_w
self.reg_factor = reg_factor
self.use_cuda = use_cuda
# create grid in numpy
# self.grid = np.zeros( [self.out_h, self.out_w, 3], dtype=np.float32)
# sampling grid with dim-0 coords (Y)
self.grid_X,self.grid_Y = np.meshgrid(np.linspace(-1,1,out_w),np.linspace(-1,1,out_h))
# grid_X,grid_Y: size [1,H,W,1,1]
self.grid_X = torch.FloatTensor(self.grid_X).unsqueeze(0).unsqueeze(3)
self.grid_Y = torch.FloatTensor(self.grid_Y).unsqueeze(0).unsqueeze(3)
self.grid_X = Variable(self.grid_X,requires_grad=False)
self.grid_Y = Variable(self.grid_Y,requires_grad=False)
if use_cuda:
self.grid_X = self.grid_X.cuda()
self.grid_Y = self.grid_Y.cuda()
# initialize regular grid for control points P_i
if use_regular_grid:
axis_coords = np.linspace(-1,1,grid_size)
self.N = grid_size*grid_size
P_Y,P_X = np.meshgrid(axis_coords,axis_coords)
P_X = np.reshape(P_X,(-1,1)) # size (N,1)
P_Y = np.reshape(P_Y,(-1,1)) # size (N,1)
P_X = torch.FloatTensor(P_X)
P_Y = torch.FloatTensor(P_Y)
self.Li = Variable(self.compute_L_inverse(P_X,P_Y).unsqueeze(0),requires_grad=False)
self.P_X = P_X.unsqueeze(2).unsqueeze(3).unsqueeze(4).transpose(0,4)
self.P_Y = P_Y.unsqueeze(2).unsqueeze(3).unsqueeze(4).transpose(0,4)
self.P_X = Variable(self.P_X,requires_grad=False)
self.P_Y = Variable(self.P_Y,requires_grad=False)
if use_cuda:
self.P_X = self.P_X.cuda()
self.P_Y = self.P_Y.cuda()
def forward(self, theta):
warped_grid = self.apply_transformation(theta,torch.cat((self.grid_X,self.grid_Y),3))
return warped_grid
def compute_L_inverse(self,X,Y):
N = X.size()[0] # num of points (along dim 0)
# construct matrix K
Xmat = X.expand(N,N)
Ymat = Y.expand(N,N)
P_dist_squared = torch.pow(Xmat-Xmat.transpose(0,1),2)+torch.pow(Ymat-Ymat.transpose(0,1),2)
P_dist_squared[P_dist_squared==0]=1 # make diagonal 1 to avoid NaN in log computation
K = torch.mul(P_dist_squared,torch.log(P_dist_squared))
if self.reg_factor != 0:
K+=torch.eye(K.size(0),K.size(1))*self.reg_factor
# construct matrix L
O = torch.FloatTensor(N,1).fill_(1)
Z = torch.FloatTensor(3,3).fill_(0)
P = torch.cat((O,X,Y),1)
L = torch.cat((torch.cat((K,P),1),torch.cat((P.transpose(0,1),Z),1)),0)
Li = torch.inverse(L)
if self.use_cuda:
Li = Li.cuda()
return Li
def apply_transformation(self,theta,points):
if theta.dim()==2:
theta = theta.unsqueeze(2).unsqueeze(3)
# points should be in the [B,H,W,2] format,
# where points[:,:,:,0] are the X coords
# and points[:,:,:,1] are the Y coords
# input are the corresponding control points P_i
batch_size = theta.size()[0]
# split theta into point coordinates
Q_X=theta[:,:self.N,:,:].squeeze(3)
Q_Y=theta[:,self.N:,:,:].squeeze(3)
# get spatial dimensions of points
points_b = points.size()[0]
points_h = points.size()[1]
points_w = points.size()[2]
# repeat pre-defined control points along spatial dimensions of points to be transformed
P_X = self.P_X.expand((1,points_h,points_w,1,self.N))
P_Y = self.P_Y.expand((1,points_h,points_w,1,self.N))
# compute weigths for non-linear part
W_X = torch.bmm(self.Li[:,:self.N,:self.N].expand((batch_size,self.N,self.N)),Q_X)
W_Y = torch.bmm(self.Li[:,:self.N,:self.N].expand((batch_size,self.N,self.N)),Q_Y)
# reshape
# W_X,W,Y: size [B,H,W,1,N]
W_X = W_X.unsqueeze(3).unsqueeze(4).transpose(1,4).repeat(1,points_h,points_w,1,1)
W_Y = W_Y.unsqueeze(3).unsqueeze(4).transpose(1,4).repeat(1,points_h,points_w,1,1)
# compute weights for affine part
A_X = torch.bmm(self.Li[:,self.N:,:self.N].expand((batch_size,3,self.N)),Q_X)
A_Y = torch.bmm(self.Li[:,self.N:,:self.N].expand((batch_size,3,self.N)),Q_Y)
# reshape
# A_X,A,Y: size [B,H,W,1,3]
A_X = A_X.unsqueeze(3).unsqueeze(4).transpose(1,4).repeat(1,points_h,points_w,1,1)
A_Y = A_Y.unsqueeze(3).unsqueeze(4).transpose(1,4).repeat(1,points_h,points_w,1,1)
#.........这里部分代码省略.........