def calc_gradient_penalty(self, netD, real_data, fake_data):
alpha = torch.rand(1, 1)
alpha = alpha.expand(real_data.size())
alpha = alpha.cuda()
interpolates = alpha * real_data + ((1 - alpha) * fake_data)
interpolates = interpolates.cuda()
interpolates = Variable(interpolates, requires_grad=True)
disc_interpolates = netD.forward(interpolates)
gradients = autograd.grad(outputs=disc_interpolates, inputs=interpolates,
grad_outputs=torch.ones(disc_interpolates.size()).cuda(),
create_graph=True, retain_graph=True, only_inputs=True)[0]
gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean() * self.LAMBDA
return gradient_penalty
python类ones()的实例源码
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 __init__(self, nOutput, eps=1e-5, momentum=0.1, affine=True):
super(BatchNormalization, self).__init__()
assert nOutput != 0
self.affine = affine
self.eps = eps
self.train = True
self.momentum = momentum
self.running_mean = torch.zeros(nOutput)
self.running_var = torch.ones(nOutput)
self.save_mean = None
self.save_std = None
if self.affine:
self.weight = torch.Tensor(nOutput)
self.bias = torch.Tensor(nOutput)
self.gradWeight = torch.Tensor(nOutput)
self.gradBias = torch.Tensor(nOutput)
self.reset()
else:
self.weight = None
self.bias = None
self.gradWeight = None
self.gradBias = None
def _test_pool(self):
def do_test():
p = mp.Pool(2)
for proc in p._pool:
lc.check_pid(proc.pid)
buffers = (torch.zeros(2, 2) for i in range(4))
results = p.map(simple_pool_fill, buffers, 1)
for r in results:
self.assertEqual(r, torch.ones(2, 2) * 5, 0)
self.assertEqual(len(results), 4)
p.close()
p.join()
with leak_checker(self) as lc:
do_test()
def test_gpu(self):
compile_extension(
name='gpulib',
header=test_dir + '/ffi/src/cuda/cudalib.h',
sources=[
test_dir + '/ffi/src/cuda/cudalib.c',
],
with_cuda=True,
verbose=False,
)
import gpulib
tensor = torch.ones(2, 2).float()
gpulib.good_func(tensor, 2, 1.5)
self.assertEqual(tensor, torch.ones(2, 2) * 2 + 1.5)
ctensor = tensor.cuda().fill_(1)
gpulib.cuda_func(ctensor, 2, 1.5)
self.assertEqual(ctensor, torch.ones(2, 2) * 2 + 1.5)
self.assertRaises(TypeError,
lambda: gpulib.cuda_func(tensor, 2, 1.5))
self.assertRaises(TypeError,
lambda: gpulib.cuda_func(ctensor.storage(), 2, 1.5))
def test_L1Penalty(self):
weight = 1
m = nn.L1Penalty(weight, False, False)
input = torch.rand(2,10).add_(-0.5)
input[0][0] = 0
m.forward(input)
grad = m.backward(input, torch.ones(input.size()))
self.assertEqual(input.abs().sum() * weight, m.loss)
true_grad = (input.gt(0).type_as(grad) +
input.lt(0).type_as(grad).mul_(-1)).mul_(weight)
self.assertEqual(true_grad, grad)
# Check that these don't raise errors
m.__repr__()
str(m)
def test_all_any(self):
def test(size):
x = torch.ones(*size).byte()
self.assertTrue(x.all())
self.assertTrue(x.any())
x[3] = 0
self.assertFalse(x.all())
self.assertTrue(x.any())
x.zero_()
self.assertFalse(x.all())
self.assertFalse(x.any())
x.fill_(2)
self.assertTrue(x.all())
self.assertTrue(x.any())
test((10,))
test((5, 5))
def test_accuracy_computation(self):
accuracy = BooleanAccuracy()
predictions = torch.Tensor([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
targets = torch.Tensor([[0, 1],
[2, 2],
[4, 5],
[7, 7]])
accuracy(predictions, targets)
assert accuracy.get_metric() == 2. / 4
mask = torch.ones(4, 2)
mask[1, 1] = 0
accuracy(predictions, targets, mask)
assert accuracy.get_metric() == 5. / 8
targets[1, 1] = 3
accuracy(predictions, targets)
assert accuracy.get_metric() == 8. / 12
accuracy.reset()
accuracy(predictions, targets)
assert accuracy.get_metric() == 3. / 4
def setUp(self):
super(TestEncoderBase, self).setUp()
self.lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=7, batch_first=True)
self.encoder_base = _EncoderBase(stateful=True)
tensor = Variable(torch.rand([5, 7, 3]))
tensor[1, 6:, :] = 0
tensor[3, 2:, :] = 0
self.tensor = tensor
mask = Variable(torch.ones(5, 7))
mask[1, 6:] = 0
mask[2, :] = 0 # <= completely masked
mask[3, 2:] = 0
mask[4, :] = 0 # <= completely masked
self.mask = mask
self.batch_size = 5
self.num_valid = 3
sequence_lengths = get_lengths_from_binary_sequence_mask(mask)
_, _, restoration_indices, sorting_indices = sort_batch_by_length(tensor, sequence_lengths)
self.sorting_indices = sorting_indices
self.restoration_indices = restoration_indices
def test_forward_works_even_with_empty_sequences(self):
lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=11, batch_first=True)
encoder = PytorchSeq2VecWrapper(lstm)
tensor = torch.autograd.Variable(torch.rand([5, 7, 3]))
tensor[1, 6:, :] = 0
tensor[2, :, :] = 0
tensor[3, 2:, :] = 0
tensor[4, :, :] = 0
mask = torch.autograd.Variable(torch.ones(5, 7))
mask[1, 6:] = 0
mask[2, :] = 0
mask[3, 2:] = 0
mask[4, :] = 0
results = encoder(tensor, mask)
for i in (0, 1, 3):
assert not (results[i] == 0.).data.all()
for i in (2, 4):
assert (results[i] == 0.).data.all()
def test_forward_works_even_with_empty_sequences(self):
lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=7, batch_first=True)
encoder = PytorchSeq2SeqWrapper(lstm)
tensor = torch.autograd.Variable(torch.rand([5, 7, 3]))
tensor[1, 6:, :] = 0
tensor[2, :, :] = 0
tensor[3, 2:, :] = 0
tensor[4, :, :] = 0
mask = torch.autograd.Variable(torch.ones(5, 7))
mask[1, 6:] = 0
mask[2, :] = 0
mask[3, 2:] = 0
mask[4, :] = 0
results = encoder(tensor, mask)
for i in (0, 1, 3):
assert not (results[i] == 0.).data.all()
for i in (2, 4):
assert (results[i] == 0.).data.all()
def test_forward_pulls_out_correct_tensor_with_sequence_lengths(self):
lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=7, batch_first=True)
encoder = PytorchSeq2SeqWrapper(lstm)
tensor = torch.rand([5, 7, 3])
tensor[1, 6:, :] = 0
tensor[2, 4:, :] = 0
tensor[3, 2:, :] = 0
tensor[4, 1:, :] = 0
mask = torch.ones(5, 7)
mask[1, 6:] = 0
mask[2, 4:] = 0
mask[3, 2:] = 0
mask[4, 1:] = 0
input_tensor = Variable(tensor)
mask = Variable(mask)
sequence_lengths = get_lengths_from_binary_sequence_mask(mask)
packed_sequence = pack_padded_sequence(input_tensor, sequence_lengths.data.tolist(), batch_first=True)
lstm_output, _ = lstm(packed_sequence)
encoder_output = encoder(input_tensor, mask)
lstm_tensor, _ = pad_packed_sequence(lstm_output, batch_first=True)
assert_almost_equal(encoder_output.data.numpy(), lstm_tensor.data.numpy())
def test_wrapper_stateful(self):
lstm = LSTM(bidirectional=True, num_layers=2, input_size=3, hidden_size=7, batch_first=True)
encoder = PytorchSeq2SeqWrapper(lstm, stateful=True)
# To test the stateful functionality we need to call the encoder multiple times.
# Different batch sizes further tests some of the logic.
batch_sizes = [5, 10, 8]
sequence_lengths = [4, 6, 7]
states = []
for batch_size, sequence_length in zip(batch_sizes, sequence_lengths):
tensor = Variable(torch.rand([batch_size, sequence_length, 3]))
mask = Variable(torch.ones(batch_size, sequence_length))
mask.data[0, 3:] = 0
encoder_output = encoder(tensor, mask)
states.append(encoder._states) # pylint: disable=protected-access
# Check that the output is masked properly.
assert_almost_equal(encoder_output[0, 3:, :].data.numpy(), numpy.zeros((4, 14)))
for k in range(2):
assert_almost_equal(
states[-1][k][:, -2:, :].data.numpy(), states[-2][k][:, -2:, :].data.numpy()
)
def test_wrapper_stateful_single_state_gru(self):
gru = GRU(bidirectional=True, num_layers=2, input_size=3, hidden_size=7, batch_first=True)
encoder = PytorchSeq2SeqWrapper(gru, stateful=True)
batch_sizes = [10, 5]
states = []
for batch_size in batch_sizes:
tensor = Variable(torch.rand([batch_size, 5, 3]))
mask = Variable(torch.ones(batch_size, 5))
mask.data[0, 3:] = 0
encoder_output = encoder(tensor, mask)
states.append(encoder._states) # pylint: disable=protected-access
assert_almost_equal(encoder_output[0, 3:, :].data.numpy(), numpy.zeros((2, 14)))
assert_almost_equal(
states[-1][0][:, -5:, :].data.numpy(), states[-2][0][:, -5:, :].data.numpy()
)
def __call__(self, # type: ignore
logits: torch.Tensor,
mask: Optional[torch.Tensor] = None):
"""
Parameters
----------
logits : ``torch.Tensor``, required.
A tensor of unnormalized log probabilities of shape (batch_size, ..., num_classes).
mask: ``torch.Tensor``, optional (default = None).
A masking tensor of shape (batch_size, ...).
"""
# Get the data from the Variables.
logits, mask = self.unwrap_to_tensors(logits, mask)
if mask is None:
mask = torch.ones(logits.size()[:-1])
log_probs = torch.nn.functional.log_softmax(Variable(logits), dim=-1).data
probabilities = torch.exp(log_probs) * mask.unsqueeze(-1)
weighted_negative_likelihood = - log_probs * probabilities
entropy = weighted_negative_likelihood.sum(-1)
self._entropy += entropy.sum() / mask.sum()
self._count += 1
def test_mlpg_gradcheck():
# MLPG is performed dimention by dimention, so static_dim 1 is enough,
# 2 just for in case.
static_dim = 2
T = 10
for windows in _get_windows_set():
torch.manual_seed(1234)
means = Variable(torch.rand(T, static_dim * len(windows)),
requires_grad=True)
inputs = (means,)
# Unit variances case
variances = torch.ones(static_dim * len(windows)
).expand(T, static_dim * len(windows))
assert gradcheck(MLPG(variances, windows),
inputs, eps=1e-3, atol=1e-3)
# Rand variances case
variances = torch.rand(static_dim * len(windows)
).expand(T, static_dim * len(windows))
assert gradcheck(MLPG(variances, windows),
inputs, eps=1e-3, atol=1e-3)
def forward(self, inputs, batch_size, hidden_cell=None):
if hidden_cell is None:
# then must init with zeros
if use_cuda:
hidden = Variable(torch.zeros(2, batch_size, hp.enc_hidden_size).cuda())
cell = Variable(torch.zeros(2, batch_size, hp.enc_hidden_size).cuda())
else:
hidden = Variable(torch.zeros(2, batch_size, hp.enc_hidden_size))
cell = Variable(torch.zeros(2, batch_size, hp.enc_hidden_size))
hidden_cell = (hidden, cell)
_, (hidden,cell) = self.lstm(inputs.float(), hidden_cell)
# hidden is (2, batch_size, hidden_size), we want (batch_size, 2*hidden_size):
hidden_forward, hidden_backward = torch.split(hidden,1,0)
hidden_cat = torch.cat([hidden_forward.squeeze(0), hidden_backward.squeeze(0)],1)
# mu and sigma:
mu = self.fc_mu(hidden_cat)
sigma_hat = self.fc_sigma(hidden_cat)
sigma = torch.exp(sigma_hat/2.)
# N ~ N(0,1)
z_size = mu.size()
if use_cuda:
N = Variable(torch.normal(torch.zeros(z_size),torch.ones(z_size)).cuda())
else:
N = Variable(torch.normal(torch.zeros(z_size),torch.ones(z_size)))
z = mu + sigma*N
# mu and sigma_hat are needed for LKL loss
return z, mu, sigma_hat
def model(data):
# Create unit normal priors over the parameters
mu = Variable(torch.zeros(p, 1)).type_as(data)
sigma = Variable(torch.ones(p, 1)).type_as(data)
bias_mu = Variable(torch.zeros(1)).type_as(data)
bias_sigma = Variable(torch.ones(1)).type_as(data)
w_prior, b_prior = Normal(mu, sigma), Normal(bias_mu, bias_sigma)
priors = {'linear.weight': w_prior, 'linear.bias': b_prior}
# lift module parameters to random variables sampled from the priors
lifted_module = pyro.random_module("module", regression_model, priors)
# sample a regressor (which also samples w and b)
lifted_reg_model = lifted_module()
with pyro.iarange("map", N, subsample=data):
x_data = data[:, :-1]
y_data = data[:, -1]
# run the regressor forward conditioned on inputs
prediction_mean = lifted_reg_model(x_data).squeeze()
pyro.observe("obs", Normal(prediction_mean, Variable(torch.ones(data.size(0))).type_as(data)), y_data.squeeze())
def guide(data):
w_mu = Variable(torch.randn(p, 1).type_as(data.data), requires_grad=True)
w_log_sig = Variable((-3.0 * torch.ones(p, 1) + 0.05 * torch.randn(p, 1)).type_as(data.data), requires_grad=True)
b_mu = Variable(torch.randn(1).type_as(data.data), requires_grad=True)
b_log_sig = Variable((-3.0 * torch.ones(1) + 0.05 * torch.randn(1)).type_as(data.data), requires_grad=True)
# register learnable params in the param store
mw_param = pyro.param("guide_mean_weight", w_mu)
sw_param = softplus(pyro.param("guide_log_sigma_weight", w_log_sig))
mb_param = pyro.param("guide_mean_bias", b_mu)
sb_param = softplus(pyro.param("guide_log_sigma_bias", b_log_sig))
# gaussian guide distributions for w and b
w_dist = Normal(mw_param, sw_param)
b_dist = Normal(mb_param, sb_param)
dists = {'linear.weight': w_dist, 'linear.bias': b_dist}
# overloading the parameters in the module with random samples from the guide distributions
lifted_module = pyro.random_module("module", regression_model, dists)
# sample a regressor
return lifted_module()
# instantiate optim and inference objects
def setUp(self):
pyro.clear_param_store()
def model():
mu = pyro.sample("mu", Normal(Variable(torch.zeros(1)),
Variable(torch.ones(1))))
xd = Normal(mu, Variable(torch.ones(1)), batch_size=50)
pyro.observe("xs", xd, self.data)
return mu
def guide():
return pyro.sample("mu", Normal(Variable(torch.zeros(1)),
Variable(torch.ones(1))))
# data
self.data = Variable(torch.zeros(50, 1))
self.mu_mean = Variable(torch.zeros(1))
self.mu_stddev = torch.sqrt(Variable(torch.ones(1)) / 51.0)
# model and guide
self.model = model
self.guide = guide
def setUp(self):
# normal-normal; known covariance
def model_dup():
pyro.param("mu_q", Variable(torch.ones(1), requires_grad=True))
pyro.sample("mu_q", dist.normal, ng_zeros(1), ng_ones(1))
def model_obs_dup():
pyro.sample("mu_q", dist.normal, ng_zeros(1), ng_ones(1))
pyro.observe("mu_q", dist.normal, ng_zeros(1), ng_ones(1), ng_zeros(1))
def model():
pyro.sample("mu_q", dist.normal, ng_zeros(1), ng_ones(1))
def guide():
p = pyro.param("p", Variable(torch.ones(1), requires_grad=True))
pyro.sample("mu_q", dist.normal, ng_zeros(1), p)
pyro.sample("mu_q_2", dist.normal, ng_zeros(1), p)
self.duplicate_model = model_dup
self.duplicate_obs = model_obs_dup
self.model = model
self.guide = guide
def setUp(self):
# Simple model with 1 continuous + 1 discrete + 1 continuous variable.
def model():
p = Variable(torch.Tensor([0.5]))
mu = Variable(torch.zeros(1))
sigma = Variable(torch.ones(1))
x = pyro.sample("x", Normal(mu, sigma)) # Before the discrete variable.
y = pyro.sample("y", Bernoulli(p))
z = pyro.sample("z", Normal(mu, sigma)) # After the discrete variable.
return dict(x=x, y=y, z=z)
self.sites = ["x", "y", "z", "_INPUT", "_RETURN"]
self.model = model
self.queue = Queue()
self.queue.put(poutine.Trace())
def __init__(self, shared_resources: SharedResources):
super(FastQAPyTorchModule, self).__init__()
self._shared_resources = shared_resources
input_size = shared_resources.config["repr_dim_input"]
size = shared_resources.config["repr_dim"]
self._size = size
self._with_char_embeddings = self._shared_resources.config.get("with_char_embeddings", False)
# modules & parameters
if self._with_char_embeddings:
self._conv_char_embedding = embedding.ConvCharEmbeddingModule(
len(shared_resources.char_vocab), size)
self._embedding_projection = nn.Linear(size + input_size, size)
self._embedding_highway = Highway(size, 1)
self._v_wiq_w = nn.Parameter(torch.ones(1, 1, input_size + size))
input_size = size
else:
self._v_wiq_w = nn.Parameter(torch.ones(1, 1, input_size))
self._bilstm = BiLSTM(input_size + 2, size)
self._answer_layer = FastQAAnswerModule(shared_resources)
# [size, 2 * size]
self._question_projection = nn.Parameter(torch.cat([torch.eye(size), torch.eye(size)], dim=1))
self._support_projection = nn.Parameter(torch.cat([torch.eye(size), torch.eye(size)], dim=1))
def compute_cov_a(a, classname, layer_info, fast_cnn):
batch_size = a.size(0)
if classname == 'Conv2d':
if fast_cnn:
a = _extract_patches(a, *layer_info)
a = a.view(a.size(0), -1, a.size(-1))
a = a.mean(1)
else:
a = _extract_patches(a, *layer_info)
a = a.view(-1, a.size(-1)).div_(a.size(1)).div_(a.size(2))
elif classname == 'AddBias':
is_cuda = a.is_cuda
a = torch.ones(a.size(0), 1)
if is_cuda:
a = a.cuda()
return a.t() @ (a / batch_size)
def calc_gradient_penalty(D, real_data, fake_data):
"""Calculatge gradient penalty for WGAN-GP."""
alpha = torch.rand(params.batch_size, 1)
alpha = alpha.expand(real_data.size())
alpha = make_cuda(alpha)
interpolates = make_variable(alpha * real_data + ((1 - alpha) * fake_data))
interpolates.requires_grad = True
disc_interpolates = D(interpolates)
gradients = grad(outputs=disc_interpolates,
inputs=interpolates,
grad_outputs=make_cuda(
torch.ones(disc_interpolates.size())),
create_graph=True,
retain_graph=True,
only_inputs=True)[0]
gradient_penalty = params.penalty_lambda * \
((gradients.norm(2, dim=1) - 1) ** 2).mean()
return gradient_penalty
def __init__(self, num_features, max_length, eps=1e-5, momentum=0.1,
affine=True):
"""
Most parts are copied from
torch.nn.modules.batchnorm._BatchNorm.
"""
super(SeparatedBatchNorm1d, self).__init__()
self.num_features = num_features
self.max_length = max_length
self.affine = affine
self.eps = eps
self.momentum = momentum
if self.affine:
self.weight = nn.Parameter(torch.FloatTensor(num_features))
self.bias = nn.Parameter(torch.FloatTensor(num_features))
else:
self.register_parameter('weight', None)
self.register_parameter('bias', None)
for i in range(max_length):
self.register_buffer(
'running_mean_{}'.format(i), torch.zeros(num_features))
self.register_buffer(
'running_var_{}'.format(i), torch.ones(num_features))
self.reset_parameters()
def baseline_search(self, input, beam_size=None):
# This is the simple greedy search
batch_size = input.size(0)
hidden_feat = self.lstm_im(input.view(1, input.size()[0], input.size()[1]))[1]
x = Variable(torch.ones(1, batch_size,).type(torch.LongTensor) * self.start, requires_grad=False).cuda() # <start>
output = []
flag = torch.ones(batch_size)
for i in range(self.nseq):
input_x = self.encoder(x.view(1, -1))
output_feature, hidden_feat = self.lstm_word(input_x, hidden_feat)
output_t = self.decoder(output_feature.view(-1, output_feature.size(2)))
output_t = F.log_softmax(output_t)
logprob, x = output_t.max(1)
output.append(x)
flag[x.cpu().eq(self.end).data] = 0
if flag.sum() == 0:
break
output = torch.stack(output, 0).squeeze().transpose(0, 1).cpu().data
return output
recurrent_BatchNorm.py 文件源码
项目:Recognizing-Textual-Entailment
作者: codedecde
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def __init__(self, num_features, max_len, eps=1e-5, momentum=0.1, affine=True):
super(recurrent_BatchNorm, self).__init__()
self.num_features = num_features
self.affine = affine
self.max_len = max_len
self.eps = eps
self.momentum = momentum
if self.affine:
self.weight = nn.Parameter(torch.Tensor(num_features))
self.register_parameter('weight', self.weight)
self.bias = nn.Parameter(torch.Tensor(num_features))
self.register_parameter('bias', self.bias)
else:
self.register_parameter('weight', None)
self.register_parameter('bias', None)
for i in xrange(max_len):
self.register_buffer('running_mean_{}'.format(i), torch.zeros(num_features))
self.register_buffer('running_var_{}'.format(i), torch.ones(num_features))
self.reset_parameters()
def __init__(self, X, Y, hidden_layer_sizes):
super(Net, self).__init__()
# Initialize linear layer with least squares solution
X_ = np.hstack([X, np.ones((X.shape[0],1))])
Theta = np.linalg.solve(X_.T.dot(X_), X_.T.dot(Y))
self.lin = nn.Linear(X.shape[1], Y.shape[1])
W,b = self.lin.parameters()
W.data = torch.Tensor(Theta[:-1,:].T)
b.data = torch.Tensor(Theta[-1,:])
# Set up non-linear network of
# Linear -> BatchNorm -> ReLU -> Dropout layers
layer_sizes = [X.shape[1]] + hidden_layer_sizes
layers = reduce(operator.add,
[[nn.Linear(a,b), nn.BatchNorm1d(b), nn.ReLU(), nn.Dropout(p=0.2)]
for a,b in zip(layer_sizes[0:-1], layer_sizes[1:])])
layers += [nn.Linear(layer_sizes[-1], Y.shape[1])]
self.net = nn.Sequential(*layers)
self.sig = Parameter(torch.ones(1, Y.shape[1]).cuda())
def __init__(self, params, eps=1e-2):
super(SolveNewsvendor, self).__init__()
k = len(params['d'])
self.Q = Variable(torch.diag(torch.Tensor(
[params['c_quad']] + [params['b_quad']]*k + [params['h_quad']]*k)) \
.cuda())
self.p = Variable(torch.Tensor(
[params['c_lin']] + [params['b_lin']]*k + [params['h_lin']]*k) \
.cuda())
self.G = Variable(torch.cat([
torch.cat([-torch.ones(k,1), -torch.eye(k), torch.zeros(k,k)], 1),
torch.cat([torch.ones(k,1), torch.zeros(k,k), -torch.eye(k)], 1),
-torch.eye(1 + 2*k)], 0).cuda())
self.h = Variable(torch.Tensor(
np.concatenate([-params['d'], params['d'], np.zeros(1+ 2*k)])).cuda())
self.one = Variable(torch.Tensor([1])).cuda()
self.eps_eye = eps * Variable(torch.eye(1 + 2*k).cuda()).unsqueeze(0)