def reorder_bpr_loss(re_x, his_x, dynamic_user, item_embedding, config):
'''
loss function for reorder prediction
re_x padded reorder baskets
his_x padded history bought items
'''
nll = 0
ub_seqs = []
for u, h, du in zip(re_x, his_x, dynamic_user):
du_p_product = torch.mm(du, item_embedding.t()) # shape: max_len, num_item
nll_u = [] # nll for user
for t, basket_t in enumerate(u):
if basket_t[0] != 0:
pos_idx = torch.cuda.LongTensor(basket_t) if config.cuda else torch.LongTensor(basket_t)
# Sample negative products
neg = [random.choice(h[t]) for _ in range(len(basket_t))] # replacement
# neg = random.sample(range(1, config.num_product), len(basket_t)) # without replacement
neg_idx = torch.cuda.LongTensor(neg) if config.cuda else torch.LongTensor(neg)
# Score p(u, t, v > v')
score = du_p_product[t - 1][pos_idx] - du_p_product[t - 1][neg_idx]
# Average Negative log likelihood for basket_t
nll_u.append(- torch.mean(torch.nn.LogSigmoid()(score)))
nll += torch.mean(torch.cat(nll_u))
return nll
python类mean()的实例源码
def _train_nn(self):
# neural network part
self.optimizer.zero_grad()
batch_state_before, batch_action, batch_reward, batch_state_after, batch_done = self.get_batch()
target = self.agent.estimate_value(batch_reward, batch_state_after, batch_done)
q_value = self.agent.q_value(batch_state_before, batch_action)
loss = self.agent.net.loss(q_value, target)
if self._step % self.gradient_update_freq == 0:
loss.backward()
self.optimizer.step()
if self._step % self.log_freq_by_step == 0:
self._writer.add_scalar("epsilon", self.agent.epsilon, self._step)
self._writer.add_scalar("q_net-target", (q_value.data - target.data).mean(), self._step)
self._writer.add_scalar("loss", loss.data.cpu()[0], self._step)
return loss.data[0]
def bn_hat_z_layers(self, hat_z_layers, z_pre_layers):
# TODO: Calculate batchnorm using GPU Tensors.
assert len(hat_z_layers) == len(z_pre_layers)
hat_z_layers_normalized = []
for i, (hat_z, z_pre) in enumerate(zip(hat_z_layers, z_pre_layers)):
if self.use_cuda:
ones = Variable(torch.ones(z_pre.size()[0], 1).cuda())
else:
ones = Variable(torch.ones(z_pre.size()[0], 1))
mean = torch.mean(z_pre, 0)
noise_var = np.random.normal(loc=0.0, scale=1 - 1e-10, size=z_pre.size())
if self.use_cuda:
var = np.var(z_pre.data.cpu().numpy() + noise_var, axis=0).reshape(1, z_pre.size()[1])
else:
var = np.var(z_pre.data.numpy() + noise_var, axis=0).reshape(1, z_pre.size()[1])
var = Variable(torch.FloatTensor(var))
if self.use_cuda:
hat_z = hat_z.cpu()
ones = ones.cpu()
mean = mean.cpu()
hat_z_normalized = torch.div(hat_z - ones.mm(mean), ones.mm(torch.sqrt(var + 1e-10)))
if self.use_cuda:
hat_z_normalized = hat_z_normalized.cuda()
hat_z_layers_normalized.append(hat_z_normalized)
return hat_z_layers_normalized
def train_ae(self, train_X, optimizer, epochs, verbose=True):
N = train_X.data.size()[0]
num_batches = N / self.batch_size
for e in range(epochs):
agg_cost = 0.
for k in range(num_batches):
start, end = k * self.batch_size, (k + 1) * self.batch_size
bX = train_X[start:end]
optimizer.zero_grad()
Z = self.forward(bX)
Z = self.decode(Z)
loss = -torch.sum(bX * torch.log(Z) + (1.0 - bX) * torch.log(1.0 - Z), 1)
cost = torch.mean(loss)
cost.backward()
optimizer.step()
agg_cost += cost
agg_cost /= num_batches
if verbose:
print("Epoch:", e, "cost:", agg_cost.data[0])
def test_Dropout(self):
p = 0.2
input = torch.Tensor(1000).fill_(1-p)
module = nn.Dropout(p)
output = module.forward(input)
self.assertLess(abs(output.mean() - (1-p)), 0.05)
gradInput = module.backward(input, input)
self.assertLess(abs(gradInput.mean() - (1-p)), 0.05)
module = nn.Dropout(p, True)
output = module.forward(input.clone())
self.assertLess(abs(output.mean() - (1-p)), 0.05)
gradInput = module.backward(input.clone(), input.clone())
self.assertLess(abs(gradInput.mean() - (1-p)), 0.05)
# Check that these don't raise errors
module.__repr__()
str(module)
def test_SpatialDropout(self):
p = 0.2
b = random.randint(1, 5)
w = random.randint(1, 5)
h = random.randint(1, 5)
nfeats = 1000
input = torch.Tensor(b, nfeats, w, h).fill_(1)
module = nn.SpatialDropout(p)
module.training()
output = module.forward(input)
self.assertLess(abs(output.mean() - (1-p)), 0.05)
gradInput = module.backward(input, input)
self.assertLess(abs(gradInput.mean() - (1-p)), 0.05)
# Check that these don't raise errors
module.__repr__()
str(module)
def test_VolumetricDropout(self):
p = 0.2
bsz = random.randint(1,5)
t = random.randint(1,5)
w = random.randint(1,5)
h = random.randint(1,5)
nfeats = 1000
input = torch.Tensor(bsz, nfeats, t, w, h).fill_(1)
module = nn.VolumetricDropout(p)
module.training()
output = module.forward(input)
self.assertLess(abs(output.mean() - (1-p)), 0.05)
gradInput = module.backward(input, input)
self.assertLess(abs(gradInput.mean() - (1-p)), 0.05)
# Check that these don't raise errors
module.__repr__()
str(module)
def _gaussian(self, enc_output):
def latent_loss(mu, sigma):
pow_mu = mu * mu
pow_sigma = sigma * sigma
return 0.5 * torch.mean(pow_mu + pow_sigma - torch.log(pow_sigma) - 1)
mu = self._enc_mu(enc_output)
sigma = torch.exp(.5 * self._enc_log_sigma(enc_output))
self.latent_loss = latent_loss(mu, sigma)
weight = next(self.parameters()).data
std_z = Variable(weight.new(*sigma.size()), requires_grad=False)
std_z.data.copy_(torch.from_numpy(
np.random.normal(size=sigma.size())))
return mu + sigma * std_z
def _layer_BatchNorm(self):
self.add_body(0, """
@staticmethod
def __batch_normalization(dim, name, **kwargs):
if dim == 1: layer = nn.BatchNorm1d(**kwargs)
elif dim == 2: layer = nn.BatchNorm2d(**kwargs)
elif dim == 3: layer = nn.BatchNorm3d(**kwargs)
else: raise NotImplementedError()
if 'scale' in __weights_dict[name]:
layer.state_dict()['weight'].copy_(torch.from_numpy(__weights_dict[name]['scale']))
else:
layer.weight.data.fill_(1)
if 'bias' in __weights_dict[name]:
layer.state_dict()['bias'].copy_(torch.from_numpy(__weights_dict[name]['bias']))
else:
layer.bias.data.fill_(0)
layer.state_dict()['running_mean'].copy_(torch.from_numpy(__weights_dict[name]['mean']))
layer.state_dict()['running_var'].copy_(torch.from_numpy(__weights_dict[name]['var']))
return layer""")
def test_Dropout(self):
p = 0.2
input = torch.Tensor(1000).fill_(1 - p)
module = nn.Dropout(p)
output = module.forward(input)
self.assertLess(abs(output.mean() - (1 - p)), 0.05)
gradInput = module.backward(input, input)
self.assertLess(abs(gradInput.mean() - (1 - p)), 0.05)
module = nn.Dropout(p, True)
output = module.forward(input.clone())
self.assertLess(abs(output.mean() - (1 - p)), 0.05)
gradInput = module.backward(input.clone(), input.clone())
self.assertLess(abs(gradInput.mean() - (1 - p)), 0.05)
# Check that these don't raise errors
module.__repr__()
str(module)
def test_SpatialDropout(self):
p = 0.2
b = random.randint(1, 5)
w = random.randint(1, 5)
h = random.randint(1, 5)
nfeats = 1000
input = torch.Tensor(b, nfeats, w, h).fill_(1)
module = nn.SpatialDropout(p)
module.training()
output = module.forward(input)
self.assertLess(abs(output.mean() - (1 - p)), 0.05)
gradInput = module.backward(input, input)
self.assertLess(abs(gradInput.mean() - (1 - p)), 0.05)
# Check that these don't raise errors
module.__repr__()
str(module)
def test_VolumetricDropout(self):
p = 0.2
bsz = random.randint(1, 5)
t = random.randint(1, 5)
w = random.randint(1, 5)
h = random.randint(1, 5)
nfeats = 1000
input = torch.Tensor(bsz, nfeats, t, w, h).fill_(1)
module = nn.VolumetricDropout(p)
module.training()
output = module.forward(input)
self.assertLess(abs(output.mean() - (1 - p)), 0.05)
gradInput = module.backward(input, input)
self.assertLess(abs(gradInput.mean() - (1 - p)), 0.05)
# Check that these don't raise errors
module.__repr__()
str(module)
def forward(self,title,content):
title_em = self.encoder(title)
content_em = self.encoder(content)
title_size = title_em.size()
content_size = content_em.size()
title_2 = self.pre1(title_em.contiguous().view(-1,256)).view(title_size[0],title_size[1],-1)
content_2 = self.pre2(content_em.contiguous().view(-1,256)).view(content_size[0],content_size[1],-1)
title_ = t.mean(title_2,dim=1)
content_ = t.mean(content_2,dim=1)
inputs=t.cat((title_.squeeze(),content_.squeeze()),1)
out=self.fc(inputs)
# content_out=self.content_fc(content.view(content.size(0),-1))
# out=torch.cat((title_out,content_out),1)
# out=self.fc(out)
return out
def forward(self,title,content):
title_em = self.encoder(title)
content_em = self.encoder(content)
title_size = title_em.size()
content_size = content_em.size()
title_2 = t.nn.functional.relu(self.bn(self.pre_fc(title_em.view(-1,256)).view(title_em.size(0),title_em.size(1),-1).transpose(1,2).contiguous()))
content_2 = t.nn.functional.relu(self.bn2(self.pre_fc2(content_em.view(-1,256)).view(content_em.size(0),content_em.size(1),-1).transpose(1,2)).contiguous())
# title_2 = self.pre(title_em.contiguous().view(-1,256)).view(title_size)
# content_2 = self.pre(content_em.contiguous().view(-1,256)).view(content_size)
title_ = t.mean(title_2,dim=2)
content_ = t.mean(content_2,dim=2)
inputs=t.cat((title_.squeeze(),content_.squeeze()),1)
out=self.fc(inputs.view(inputs.size(0),-1))
# content_out=self.content_fc(content.view(content.size(0),-1))
# out=torch.cat((title_out,content_out),1)
# out=self.fc(out)
return out
def compare_fits(x):
shape, scale = fit(x)
app_shape, app_scale = x.mean() / x.std(), x.mean()
# _, np_shape, _, np_scale = exponweib.fit(x, floc=0)
# # Plot
# def weib(x, n, a): # a == shape
# return (a / n) * (x / n)**(a - 1) * np.exp(-(x / n)**a)
#
# count, _, _ = plt.hist(x, 100)
# xx = np.linspace(x.min(), x.max(), 10000)
# yy = weib(xx, scale, shape)
# yy_app = weib(xx, app_scale, app_shape)
# yy_np = weib(xx, np_scale, np_shape)
# plt.plot(xx, yy*(count.max() / yy.max()), label='MLE')
# plt.plot(xx, yy_app*(count.max() / yy_app.max()), label='App')
# plt.plot(xx, yy_np*(count.max() / yy_np.max()), label='Scipy')
# plt.legend()
# plt.show()
return (shape, scale), (app_shape, app_scale)
def test_Dropout(self):
p = 0.2
input = torch.Tensor(1000).fill_(1 - p)
module = nn.Dropout(p)
output = module.forward(input)
self.assertLess(abs(output.mean() - (1 - p)), 0.05)
gradInput = module.backward(input, input)
self.assertLess(abs(gradInput.mean() - (1 - p)), 0.05)
module = nn.Dropout(p, True)
output = module.forward(input.clone())
self.assertLess(abs(output.mean() - (1 - p)), 0.05)
gradInput = module.backward(input.clone(), input.clone())
self.assertLess(abs(gradInput.mean() - (1 - p)), 0.05)
# Check that these don't raise errors
module.__repr__()
str(module)
def test_SpatialDropout(self):
p = 0.2
b = random.randint(1, 5)
w = random.randint(1, 5)
h = random.randint(1, 5)
nfeats = 1000
input = torch.Tensor(b, nfeats, w, h).fill_(1)
module = nn.SpatialDropout(p)
module.training()
output = module.forward(input)
self.assertLess(abs(output.mean() - (1 - p)), 0.05)
gradInput = module.backward(input, input)
self.assertLess(abs(gradInput.mean() - (1 - p)), 0.05)
# Check that these don't raise errors
module.__repr__()
str(module)
def test_VolumetricDropout(self):
p = 0.2
bsz = random.randint(1, 5)
t = random.randint(1, 5)
w = random.randint(1, 5)
h = random.randint(1, 5)
nfeats = 1000
input = torch.Tensor(bsz, nfeats, t, w, h).fill_(1)
module = nn.VolumetricDropout(p)
module.training()
output = module.forward(input)
self.assertLess(abs(output.mean() - (1 - p)), 0.05)
gradInput = module.backward(input, input)
self.assertLess(abs(gradInput.mean() - (1 - p)), 0.05)
# Check that these don't raise errors
module.__repr__()
str(module)
def forward(self, x):
arr = list()
for sentence in x:
# Sentence embedding
sent_emb = list()
for word in sentence:
word = np.array(word)
word = torch.from_numpy(word)
word = Variable(word)
# Gets the embedding for each character in
# the word
char_emb = self.embedding(word)
# Computes the mean between all character level
# embeddings. MxN -> 1xN
char_emb = torch.mean(char_emb, 0)
sent_emb.append(char_emb)
arr.append(sent_emb)
return arr
def change_key_names(old_params, in_channels):
new_params = collections.OrderedDict()
layer_count = 0
allKeyList = old_params.keys()
for layer_key in allKeyList:
if layer_count >= len(allKeyList)-2:
# exclude fc layers
continue
else:
if layer_count == 0:
rgb_weight = old_params[layer_key]
# print(type(rgb_weight))
rgb_weight_mean = torch.mean(rgb_weight, dim=1)
# TODO: ugly fix here, why torch.mean() turn tensor to Variable
# print(type(rgb_weight_mean))
flow_weight = rgb_weight_mean.unsqueeze(1).repeat(1,in_channels,1,1)
new_params[layer_key] = flow_weight
layer_count += 1
# print(layer_key, new_params[layer_key].size(), type(new_params[layer_key]))
else:
new_params[layer_key] = old_params[layer_key]
layer_count += 1
# print(layer_key, new_params[layer_key].size(), type(new_params[layer_key]))
return new_params
def change_key_names(old_params, in_channels):
new_params = collections.OrderedDict()
layer_count = 0
for layer_key in old_params.keys():
if layer_count < 26:
if layer_count == 0:
rgb_weight = old_params[layer_key]
rgb_weight_mean = torch.mean(rgb_weight, dim=1)
flow_weight = rgb_weight_mean.repeat(1,in_channels,1,1)
new_params[layer_key] = flow_weight
layer_count += 1
# print(layer_key, new_params[layer_key].size())
else:
new_params[layer_key] = old_params[layer_key]
layer_count += 1
# print(layer_key, new_params[layer_key].size())
return new_params
def sample_gaussian_2d_train(mux, muy, sx, sy, corr, nodesPresent):
o_mux, o_muy, o_sx, o_sy, o_corr = mux, muy, sx, sy, corr
numNodes = mux.size()[0]
next_x = torch.zeros(numNodes)
next_y = torch.zeros(numNodes)
for node in range(numNodes):
if node not in nodesPresent:
continue
mean = [o_mux[node], o_muy[node]]
cov = [[o_sx[node]*o_sx[node], o_corr[node]*o_sx[node]*o_sy[node]], [o_corr[node]*o_sx[node]*o_sy[node], o_sy[node]*o_sy[node]]]
next_values = np.random.multivariate_normal(mean, cov, 1)
next_x[node] = next_values[0][0]
next_y[node] = next_values[0][1]
return next_x, next_y
def stats(criterion, a, y, mask):
if mask is not None:
_, preds = t.max(a.data, 2)
batch, sLen, c = a.size()
loss = criterion(a.view(-1, c), y.view(-1))
m = t.sum(mask)
mask = _sequence_mask(mask, sLen)
acc = t.sum(mask.data.float() * (y.data == preds).float()) / float(m.data[0])
#loss = criterion(a.view(-1, c), y.view(-1))
else:
_, preds = t.max(a.data, 1)
loss = criterion(a, y)
acc = t.mean((y.data == preds).float())
return loss, acc
def test_SpatialDropout(self):
p = 0.2
b = random.randint(1, 5)
w = random.randint(1, 5)
h = random.randint(1, 5)
nfeats = 1000
input = torch.Tensor(b, nfeats, w, h).fill_(1)
module = nn.SpatialDropout(p)
module.training()
output = module.forward(input)
self.assertLess(abs(output.mean() - (1 - p)), 0.05)
gradInput = module.backward(input, input)
self.assertLess(abs(gradInput.mean() - (1 - p)), 0.05)
# Check that these don't raise errors
module.__repr__()
str(module)
def test_VolumetricDropout(self):
p = 0.2
bsz = random.randint(1, 5)
t = random.randint(1, 5)
w = random.randint(1, 5)
h = random.randint(1, 5)
nfeats = 1000
input = torch.Tensor(bsz, nfeats, t, w, h).fill_(1)
module = nn.VolumetricDropout(p)
module.training()
output = module.forward(input)
self.assertLess(abs(output.mean() - (1 - p)), 0.05)
gradInput = module.backward(input, input)
self.assertLess(abs(gradInput.mean() - (1 - p)), 0.05)
# Check that these don't raise errors
module.__repr__()
str(module)
kissgp_kronecker_product_classification_test.py 文件源码
项目:gpytorch
作者: jrg365
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def test_kissgp_classification_error():
model = GPClassificationModel()
# Find optimal model hyperparameters
model.train()
optimizer = optim.Adam(model.parameters(), lr=0.15)
optimizer.n_iter = 0
for i in range(20):
optimizer.zero_grad()
output = model.forward(train_x)
loss = -model.marginal_log_likelihood(output, train_y)
loss.backward()
optimizer.n_iter += 1
optimizer.step()
# Set back to eval mode
model.eval()
test_preds = model(train_x).mean().ge(0.5).float().mul(2).sub(1).squeeze()
mean_abs_error = torch.mean(torch.abs(train_y - test_preds) / 2)
assert(mean_abs_error.data.squeeze()[0] < 1e-5)
def test_kissgp_classification_error():
model = GPClassificationModel()
# Find optimal model hyperparameters
model.train()
optimizer = optim.Adam(model.parameters(), lr=0.15)
optimizer.n_iter = 0
for i in range(200):
optimizer.zero_grad()
output = model.forward(train_x)
loss = -model.marginal_log_likelihood(output, train_y)
loss.backward()
optimizer.n_iter += 1
optimizer.step()
# Set back to eval mode
model.eval()
test_preds = model(train_x).mean().ge(0.5).float().mul(2).sub(1).squeeze()
mean_abs_error = torch.mean(torch.abs(train_y - test_preds) / 2)
assert(mean_abs_error.data.squeeze()[0] < 1e-5)
def test_kissgp_classification_error():
train_x, train_y = train_data()
model = GPClassificationModel(train_x.data)
# Find optimal model hyperparameters
model.train()
optimizer = optim.Adam(model.parameters(), lr=0.1)
optimizer.n_iter = 0
for i in range(50):
optimizer.zero_grad()
output = model.forward(train_x)
loss = -model.marginal_log_likelihood(output, train_y)
loss.backward()
optimizer.n_iter += 1
optimizer.step()
# Set back to eval mode
model.eval()
test_preds = model(train_x).mean().ge(0.5).float().mul(2).sub(1).squeeze()
mean_abs_error = torch.mean(torch.abs(train_y - test_preds) / 2)
assert(mean_abs_error.data.squeeze()[0] < 1e-5)
def test_kissgp_classification_error_cuda():
if torch.cuda.is_available():
train_x, train_y = train_data(cuda=True)
model = GPClassificationModel(train_x.data).cuda()
model.condition(train_x, train_y)
# Find optimal model hyperparameters
model.train()
optimizer = optim.Adam(model.parameters(), lr=0.1)
optimizer.n_iter = 0
for i in range(50):
optimizer.zero_grad()
output = model.forward(train_x)
loss = -model.marginal_log_likelihood(output, train_y)
loss.backward()
optimizer.n_iter += 1
optimizer.step()
# Set back to eval mode
model.eval()
test_preds = model(train_x).mean().ge(0.5).float().mul(2).sub(1).squeeze()
mean_abs_error = torch.mean(torch.abs(train_y - test_preds) / 2)
assert(mean_abs_error.data.squeeze()[0] < 1e-5)
def test_kissgp_classification_error():
gpytorch.functions.use_toeplitz = False
model = GPClassificationModel()
# Find optimal model hyperparameters
model.train()
optimizer = optim.Adam(model.parameters(), lr=0.15)
optimizer.n_iter = 0
for i in range(100):
optimizer.zero_grad()
output = model.forward(train_x)
loss = -model.marginal_log_likelihood(output, train_y)
loss.backward()
optimizer.n_iter += 1
optimizer.step()
# Set back to eval mode
model.eval()
test_preds = model(train_x).mean().ge(0.5).float().mul(2).sub(1).squeeze()
mean_abs_error = torch.mean(torch.abs(train_y - test_preds) / 2)
gpytorch.functions.use_toeplitz = True
assert(mean_abs_error.data.squeeze()[0] < 5e-2)