def propdown(self, hid):
""" This function propagates the hidden units activation downwords to the visible units
:param hid: Variable Matrix(batch_size, out_channels, image_height_out, image_width_out) - given h_sample
:return: Variable Matrix(batch_size, in_channels, image_height, image_width) - probability for each visible units to be v_j = 1
"""
batch_size = hid.data.shape[0]
if self.real == 0:
W_flipped = F.swapaxes(CF.flip(self.conv.W, axes=(2, 3)), axis1=0, axis2=1)
pre_sigmoid_activation = F.convolution_2d(hid, W_flipped, self.conv.a, pad=self.ksize-1)
# F.matmul(hid, self.l.W) + F.broadcast_to(self.l.a, (batch_size, self.n_visible))
v_mean = F.sigmoid(pre_sigmoid_activation)
#print('W info ', self.conv.W.data.shape, 'W_flipped info ', W_flipped.data.shape)
#print('W info ', self.conv.W.data[3, 0, 2, 3], 'W_flipped info ', W_flipped.data[0, 3, 8, 7])
#print('W info ', self.conv.W.data[3, 0, 8, 7], 'W_flipped info ', W_flipped.data[0, 3, 2, 3])
#print('W info ', self.conv.W.data[19, 0, 4, 0], 'W_flipped info ', W_flipped.data[0, 19, 6, 10])
#print('pre_sigmoidactivation', F.sum(pre_sigmoid_activation).data)
#print('v_mean', v_mean.data.shape)
#print('v_mean sum', F.sum(v_mean).data)
#print('hid', hid.data.shape)
else:
# TODO: check
W_flipped = F.swapaxes(CF.flip(self.conv.W, axes=(2, 3)), axis1=0, axis2=1)
v_mean = F.convolution_2d(hid, W_flipped, self.conv.a, pad=self.ksize-1)
return v_mean
python类sum()的实例源码
def __call__(self, x):
xp = chainer.cuda.get_array_module(x.data)
batchsize = x.shape[0]
if self.train_weights == False and self.initial_T is not None:
self.T.W.data = self.initial_T
M = F.reshape(self.T(x), (-1, self.num_kernels, self.ndim_kernel))
M = F.expand_dims(M, 3)
M_T = F.transpose(M, (3, 1, 2, 0))
M, M_T = F.broadcast(M, M_T)
norm = F.sum(abs(M - M_T), axis=2)
eraser = F.broadcast_to(xp.eye(batchsize, dtype=x.dtype).reshape((batchsize, 1, batchsize)), norm.shape)
c_b = F.exp(-(norm + 1e6 * eraser))
o_b = F.sum(c_b, axis=2)
if self.train_weights == False:
self.initial_T = self.T.W.data
return F.concat((x, o_b), axis=1)
def __call__(self, *args):
x = args[:-1]
t = args[-1]
self.y = None
self.loss = None
self.accuracy = None
self.y = self.predictor(*x)
self.loss = F.softmax_cross_entropy(self.y, t)
if self.stored_variable_list is not None and \
self.fisher_list is not None: # i.e. Stored
for i in range(len(self.variable_list)):
self.loss += self.lam/2. * F.sum(
self.fisher_list[i] *
F.square(self.variable_list[i][1] -
self.stored_variable_list[i]))
reporter.report({'loss': self.loss}, self)
if self.compute_accuracy:
self.accuracy = F.accuracy(self.y, t)
reporter.report({'accuracy': self.accuracy}, self)
return self.loss
def loss_l1(h, t):
return F.sum(F.absolute(h-t)) / np.prod(h.data.shape)
def loss_l2(h, t):
return F.sum((h-t)**2) / np.prod(h.data.shape)
def loss_l2_norm(h, t, axis=(1)):
return F.sum(F.sqrt(F.sum((h-t)**2, axis=axis))) / h.data.shape[0]
def loss_func_dcgan_dis_real(y_real):
return F.sum(F.softplus(-y_real)) / np.prod(y_real.data.shape)
def loss_func_dcgan_dis_fake(y_fake):
return F.sum(F.softplus(y_fake)) / np.prod(y_fake.data.shape)
def loss_func_tv_l2(x_out):
xp = cuda.get_array_module(x_out.data)
b, ch, h, w = x_out.data.shape
Wx = xp.zeros((ch, ch, 2, 2), dtype="f")
Wy = xp.zeros((ch, ch, 2, 2), dtype="f")
for i in range(ch):
Wx[i,i,0,0] = -1
Wx[i,i,0,1] = 1
Wy[i,i,0,0] = -1
Wy[i,i,1,0] = 1
return F.sum(F.convolution_2d(x_out, W=Wx) ** 2) + F.sum(F.convolution_2d(x_out, W=Wy) ** 2)
def train(model, batch, num_samples, word_keep_rate, UNK, alpha):
xp = model.xp
use_gpu = (xp == cuda.cupy)
if use_gpu:
batch = cuda.to_gpu(batch)
KL, xents = forward(model, batch, num_samples=num_samples, word_keep_rate=word_keep_rate, UNK=UNK, train=True)
loss = alpha * KL + sum(xents) / num_samples
loss.backward()
optimizer.update()
loss.unchain_backward()
if alpha == 0: KL.unchain_backward()
def solve(self, x_seq, pos, neg, train=True, variablize=False, onebyone=True):
if variablize:# If arguments are just arrays (not variables), make them variables
x_seq = [chainer.Variable(x, volatile=not train) for x in x_seq]
x_seq = [F.dropout(x, ratio=self.dropout_ratio, train=train) for x in x_seq]
pos = self.act1(self.W_candidate(
F.dropout(chainer.Variable(pos, volatile=not train),
ratio=self.dropout_ratio, train=train)))
neg = self.act1(self.W_candidate(
F.dropout(chainer.Variable(neg, volatile=not train),
ratio=self.dropout_ratio, train=train)))
if onebyone and train:
target_x_seq = [self.act1(self.W_candidate(x)) for x in x_seq[:4]]# 1,2,3,4,5-th targets
onebyone_loss = 0.
self.LSTM.reset_state()
for i, x in enumerate(x_seq):
h = self.LSTM( F.dropout(x, ratio=self.dropout_ratio, train=train) )
if onebyone and train and target_x_seq[i+1:]:
pos_score, neg_score = self.calculate_score(h, target_x_seq[i+1:], neg,
multipos=True)
onebyone_loss += F.relu( self.margin - pos_score + neg_score )
pos_score, neg_score = self.calculate_score(h, pos, neg)
accum_loss = F.relu( self.margin - pos_score + neg_score )
TorFs = sum(accum_loss.data < self.margin)
if onebyone and train:
return F.sum(accum_loss) + F.sum(onebyone_loss), TorFs
else:
return F.sum(accum_loss), TorFs
def _pl_sample(t, ?):
"""
Sample from the plackett luce distribution directly
:param t: The target labels
:return: A random permutation from the plackett-luce distribution
parameterized by the target labels
"""
xp = cuda.get_array_module(t)
t = t[:, 0]
probs = xp.exp(t * ?)
probs /= xp.sum(probs)
return xp.random.choice(probs.shape[0], probs.shape[0], replace=False,
p=probs)
def compute_expectation(self, beta):
return F.sum(F.softmax(beta * self.q_values) * self.q_values, axis=1)
def compute_actor_loss(self, batch):
"""Compute loss for actor.
Preconditions:
q_function must have seen up to s_{t-1} and s_{t-1}.
policy must have seen up to s_{t-1}.
Preconditions:
q_function must have seen up to s_t and s_t.
policy must have seen up to s_t.
"""
batch_state = batch['state']
batch_action = batch['action']
batch_size = len(batch_action)
# Estimated policy observes s_t
onpolicy_actions = self.policy(batch_state).sample()
# Q(s_t, mu(s_t)) is evaluated.
# This should not affect the internal state of Q.
with state_kept(self.q_function):
q = self.q_function(batch_state, onpolicy_actions)
# Estimated Q-function observes s_t and a_t
if isinstance(self.q_function, Recurrent):
self.q_function.update_state(batch_state, batch_action)
# Avoid the numpy #9165 bug (see also: chainer #2744)
q = q[:, :]
# Since we want to maximize Q, loss is negation of Q
loss = - F.sum(q) / batch_size
# Update stats
self.average_actor_loss *= self.average_loss_decay
self.average_actor_loss += ((1 - self.average_loss_decay) *
float(loss.data))
return loss
def compute_value_loss(y, t, clip_delta=True, batch_accumulator='mean'):
"""Compute a loss for value prediction problem.
Args:
y (Variable or ndarray): Predicted values.
t (Variable or ndarray): Target values.
clip_delta (bool): Use the Huber loss function if set True.
batch_accumulator (str): 'mean' or 'sum'. 'mean' will use the mean of
the loss values in a batch. 'sum' will use the sum.
Returns:
(Variable) scalar loss
"""
assert batch_accumulator in ('mean', 'sum')
y = F.reshape(y, (-1, 1))
t = F.reshape(t, (-1, 1))
if clip_delta:
loss_sum = F.sum(F.huber_loss(y, t, delta=1.0))
if batch_accumulator == 'mean':
loss = loss_sum / y.shape[0]
elif batch_accumulator == 'sum':
loss = loss_sum
else:
loss_mean = F.mean_squared_error(y, t) / 2
if batch_accumulator == 'mean':
loss = loss_mean
elif batch_accumulator == 'sum':
loss = loss_mean * y.shape[0]
return loss
def _compute_loss(self, exp_batch, gamma, errors_out=None):
"""Compute the Q-learning loss for a batch of experiences
Args:
experiences (list): see update()'s docstring
gamma (float): discount factor
Returns:
loss
"""
y, t = self._compute_y_and_t(exp_batch, gamma)
if errors_out is not None:
del errors_out[:]
delta = F.sum(abs(y - t), axis=1)
delta = cuda.to_cpu(delta.data)
for e in delta:
errors_out.append(e)
if 'weights' in exp_batch:
return compute_weighted_value_loss(
y, t, exp_batch['weights'],
clip_delta=self.clip_delta,
batch_accumulator=self.batch_accumulator)
else:
return compute_value_loss(y, t, clip_delta=self.clip_delta,
batch_accumulator=self.batch_accumulator)
def __call__(self, obs):
action_distrib = self.pi(obs)
action_value = self.q(obs)
v = F.sum(action_distrib.all_prob *
action_value.q_values, axis=1)
return action_distrib, action_value, v
def __call__(self, obs):
action_distrib = self.pi(obs)
v = self.v(obs)
def evaluator(action):
adv_mean = sum(self.adv(obs, action_distrib.sample().data)
for _ in range(self.n)) / self.n
return v + self.adv(obs, action) - adv_mean
action_value = SingleActionValue(evaluator)
return action_distrib, action_value, v
def update(self, t_start, t_stop, R, states, actions, rewards, values,
action_values, action_distribs, action_distribs_mu,
avg_action_distribs):
assert np.isscalar(R)
total_loss = self.compute_loss(
t_start=t_start,
t_stop=t_stop,
R=R,
states=states,
actions=actions,
rewards=rewards,
values=values,
action_values=action_values,
action_distribs=action_distribs,
action_distribs_mu=action_distribs_mu,
avg_action_distribs=avg_action_distribs)
# Compute gradients using thread-specific model
self.model.zerograds()
total_loss.backward()
# Copy the gradients to the globally shared model
self.shared_model.zerograds()
copy_param.copy_grad(
target_link=self.shared_model, source_link=self.model)
# Update the globally shared model
if self.process_idx == 0:
norm = sum(np.sum(np.square(param.grad))
for param in self.optimizer.target.params())
self.logger.debug('grad norm:%s', norm)
self.optimizer.update()
self.sync_parameters()
if isinstance(self.model, Recurrent):
self.model.unchain_backward()
def entropy(self):
with chainer.force_backprop_mode():
return - F.sum(self.all_prob * self.all_log_prob, axis=1)