def calc_loss_recurrent(self, frames, actions, rewards, done_list, size_list):
# TODO self.max_step -> max_step
s = Variable(frames.astype(np.float32))
self.model_target.reset_state() # Refresh model_target's state
self.model_target.q_function(s[0]) # Update target model initial state
target_q = self.xp.zeros((self.max_step, self.replay_batch_size), dtype=np.float32)
selected_q_tuple = [None for _ in range(self.max_step)]
for frame in range(0, self.max_step):
q = self.model.q_function(s[frame])
q_dash = self.model_target.q_function(s[frame+1]) # Q(s',*): shape is (batch_size, action_num)
max_q_dash = q_dash.data.max(axis=1) # max_a Q(s',a): shape is (batch_size,)
if self.clipping:
rs = self.xp.sign(rewards[frame])
else:
rs = rewards[frame]
target_q[frame] = rs + self.xp.logical_not(done_list[frame]).astype(np.int)*(self.gamma*max_q_dash)
selected_q_tuple[frame] = F.select_item(q, actions[frame].astype(np.int))
enable = self.xp.broadcast_to(self.xp.arange(self.max_step), (self.replay_batch_size, self.max_step))
size_list = self.xp.expand_dims(cuda.to_gpu(size_list), -1)
enable = (enable < size_list).T
selected_q = F.concat(selected_q_tuple, axis=0)
# element-wise huber loss
huber_loss = F.huber_loss(
F.expand_dims(F.flatten(target_q), axis=1),
F.expand_dims(selected_q, axis=1), delta=1.0)
huber_loss = F.reshape(huber_loss, enable.shape)
zeros = self.xp.zeros(enable.shape, dtype=np.float32)
loss = F.sum(F.where(enable, huber_loss, zeros)) #/ self.replay_batch_size
#print("loss", loss.data)
return loss
python类expand_dims()的实例源码
def __init__(self, *args, mask='B', **kwargs):
super(MaskedConvolution2D, self).__init__(
*args, **kwargs
)
Cout, Cin, kh, kw = self.W.shape
pre_mask = self.xp.ones_like(self.W.data).astype('f')
yc, xc = kh // 2, kw // 2
# context masking - subsequent pixels won't hav access to next pixels (spatial dim)
pre_mask[:, :, yc+1:, :] = 0.0
pre_mask[:, :, yc:, xc+1:] = 0.0
# same pixel masking - pixel won't access next color (conv filter dim)
def bmask(i_out, i_in):
cout_idx = np.expand_dims(np.arange(Cout) % 3 == i_out, 1)
cin_idx = np.expand_dims(np.arange(Cin) % 3 == i_in, 0)
a1, a2 = np.broadcast_arrays(cout_idx, cin_idx)
return a1 * a2
for j in range(3):
pre_mask[bmask(j, j), yc, xc] = 0.0 if mask == 'A' else 1.0
pre_mask[bmask(0, 1), yc, xc] = 0.0
pre_mask[bmask(0, 2), yc, xc] = 0.0
pre_mask[bmask(1, 2), yc, xc] = 0.0
self.mask = pre_mask
def __call__(self, x, train=True):
h_x = self.embed(x)
n_words = h_x.shape[1]
h_x = F.expand_dims(h_x, 1)
h_x = F.relu(self.bnorm1(self.cnn1(h_x)))
h_x = F.max_pooling_2d(h_x, (n_words, self.__vec_size))
h_x = F.relu(self.l1(h_x))
return self.l2(h_x)
def __call__(self, x):
h_x = self.embed(x)
n_words = h_x.shape[1]
h_x = F.expand_dims(h_x, 1)
h_x = F.relu(self.bnorm1(self.cnn1(h_x)))
h_x = F.max_pooling_2d(h_x, (n_words, self.vec_size))
h_x = F.relu(self.l1(h_x))
return self.l2(h_x)
def squared_distance(self, anc, pos, neg):
"""
Compute anchor-positive distance and anchor-negative distance on
batches of anchors, positive, and negative samples.
"""
dist_pos = F.expand_dims(F.batch_l2_norm_squared(anc - pos), 1)
dist_neg = F.expand_dims(F.batch_l2_norm_squared(anc - neg), 1)
return dist_pos, dist_neg
predictive_autoencoder.py 文件源码
项目:Multitask-and-Transfer-Learning
作者: AI-ON
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def normalize_2d(x):
exp = F.exp(x[0])
sums = F.sum(F.sum(exp, axis=-1), axis=-1)
expanded = F.expand_dims(F.expand_dims(sums, axis=-1), axis=-1)
denominator = F.tile(expanded, (1, 160, 210))
return exp / denominator
def process_image(raw_image):
floated = raw_image.astype('float32') / 255.0
transposed = F.transpose(floated)
expanded = F.expand_dims(transposed, 0) # Make a "batch size" of 1
return expanded
def compute_loss(self, t_start, t_stop, rewards, values,
next_values, log_probs):
seq_len = t_stop - t_start
assert len(rewards) == seq_len
assert len(values) == seq_len
assert len(next_values) == seq_len
assert len(log_probs) == seq_len
pi_losses = []
v_losses = []
for t in range(t_start, t_stop):
d = min(t_stop - t, self.rollout_len)
# Discounted sum of immediate rewards
R_seq = sum(self.gamma ** i * rewards[t + i] for i in range(d))
# Discounted sum of log likelihoods
G = chainerrl.functions.weighted_sum_arrays(
xs=[log_probs[t + i] for i in range(d)],
weights=[self.gamma ** i for i in range(d)])
G = F.expand_dims(G, -1)
last_v = next_values[t + d - 1]
if not self.backprop_future_values:
last_v = chainer.Variable(last_v.data)
# C_pi only backprop through pi
C_pi = (- values[t].data +
self.gamma ** d * last_v.data +
R_seq -
self.tau * G)
# C_v only backprop through v
C_v = (- values[t] +
self.gamma ** d * last_v +
R_seq -
self.tau * G.data)
pi_losses.append(C_pi ** 2)
v_losses.append(C_v ** 2)
pi_loss = chainerrl.functions.sum_arrays(pi_losses) / 2
v_loss = chainerrl.functions.sum_arrays(v_losses) / 2
# Re-scale pi loss so that it is independent from tau
pi_loss /= self.tau
pi_loss *= self.pi_loss_coef
v_loss *= self.v_loss_coef
if self.normalize_loss_by_steps:
pi_loss /= seq_len
v_loss /= seq_len
if self.process_idx == 0:
self.logger.debug('pi_loss:%s v_loss:%s',
pi_loss.data, v_loss.data)
return pi_loss + F.reshape(v_loss, pi_loss.data.shape)
def __fit(self, content_image, style_image, epoch_num, callback=None):
xp = self.xp
input_image = None
height, width = content_image.shape[-2:]
base_epoch = 0
for stride in [4, 2, 1][-self.resolution_num:]:
if width // stride < 64:
continue
content_x = xp.asarray(content_image[:,:,::stride,::stride])
if self.keep_color:
style_x = util.luminance_only(xp.asarray(style_image[:,:,::stride,::stride]), content_x)
else:
style_x = xp.asarray(style_image[:,:,::stride,::stride])
content_layer_names = self.content_layer_names
with chainer.using_config('enable_backprop', False):
content_layers = self.model(content_x)
content_layers = [(name, content_layers[name]) for name in content_layer_names]
style_layer_names = self.style_layer_names
with chainer.using_config('enable_backprop', False):
style_layers = self.model(style_x)
style_patches = []
for name in style_layer_names:
patch = util.patch(style_layers[name])
patch_norm = F.expand_dims(F.sum(patch ** 2, axis=1) ** 0.5, 1)
style_patches.append((name, patch, patch_norm))
if input_image is None:
if self.initial_image == 'content':
input_image = xp.asarray(content_image[:,:,::stride,::stride])
else:
input_image = xp.random.uniform(-20, 20, size=content_x.shape).astype(np.float32)
else:
input_image = input_image.repeat(2, 2).repeat(2, 3)
h, w = content_x.shape[-2:]
input_image = input_image[:,:,:h,:w]
link = chainer.Link(x=input_image.shape)
if self.device_id >= 0:
link.to_gpu()
link.x.data[:] = xp.asarray(input_image)
self.optimizer.setup(link)
for epoch in six.moves.range(epoch_num):
loss_info = self.__fit_one(link, content_layers, style_patches)
if callback:
callback(base_epoch + epoch, link.x, loss_info)
base_epoch += epoch_num
input_image = link.x.data
return link.x
def __call__(self, xs):
"""
xs [(w,s,p,y), ..., ]
w: word, c: char, l: length, y: label
"""
batchsize = len(xs)
ws, cs, ls, ts = zip(*xs)
# cs: [(sentence length, max word length)]
ws = map(self.emb_word, ws)
# ls: [(sentence length, char dim)]
# cs = map(lambda (c, l): F.sum(self.emb_char(c), 1) / l, zip(cs, ls))
# cs = [F.reshape(F.average_pooling_2d(
# F.expand_dims(self.emb_char(c), 0), (l, 1)), (-1, self.char_dim))
# for c, l in zip(cs, ls)]
# before conv: (sent len, 1, max word len, char_size)
# after conv: (sent len, char_size, max word len, 1)
# after max_pool: (sent len, char_size, 1, 1)
cs = [F.squeeze(
F.max_pooling_2d(
self.conv_char(
F.expand_dims(
self.emb_char(c), 1)), (l, 1)))
for c, l in zip(cs, ls)]
# [(sentence length, (word_dim + char_dim))]
xs_f = [F.dropout(F.concat([w, c]),
self.dropout_ratio, train=self.train) for w, c in zip(ws, cs)]
xs_b = [x[::-1] for x in xs_f]
cx_f, hx_f, cx_b, hx_b = self._init_state(batchsize)
_, _, hs_f = self.lstm_f(hx_f, cx_f, xs_f, train=self.train)
_, _, hs_b = self.lstm_b(hx_b, cx_b, xs_b, train=self.train)
hs_b = [x[::-1] for x in hs_b]
# ys: [(sentence length, number of category)]
ys = [self.linear2(F.relu(self.linear1(F.concat([h_f, h_b]))))
for h_f, h_b in zip(hs_f, hs_b)]
# ys = [self.linear2(F.relu(
# self.linear1(
# F.squeeze(
# F.transpose(
# F.relu(self.conv1(
# F.reshape(
# F.concat([h_f, h_b]),
# (1, 1, -1, 2 * self.hidden_dim))), (0, 3, 2, 1))
# )))))
# for h_f, h_b in zip(hs_f, hs_b)]
loss = reduce(lambda x, y: x + y,
[F.softmax_cross_entropy(y, t) for y, t in zip(ys, ts)])
acc = reduce(lambda x, y: x + y,
[F.accuracy(y, t, ignore_label=IGNORE) for y, t in zip(ys, ts)])
acc /= batchsize
chainer.report({
"loss": loss,
"accuracy": acc
}, self)
return loss
def pool(self, WX, skip_mask=None):
Z, F, O, I = None, None, None, None
# f-pooling
if len(self._pooling) == 1:
assert len(WX) == 2
Z, F = WX
Z = functions.tanh(Z)
F = self.zoneout(F)
# fo-pooling
if len(self._pooling) == 2:
assert len(WX) == 3
Z, F, O = WX
Z = functions.tanh(Z)
F = self.zoneout(F)
O = functions.sigmoid(O)
# ifo-pooling
if len(self._pooling) == 3:
assert len(WX) == 4
Z, F, O, I = WX
Z = functions.tanh(Z)
F = self.zoneout(F)
O = functions.sigmoid(O)
I = functions.sigmoid(I)
assert Z is not None
assert F is not None
T = Z.shape[2]
for t in xrange(T):
zt = Z[..., t]
ft = F[..., t]
ot = 1 if O is None else O[..., t]
it = 1 - ft if I is None else I[..., t]
xt = 1 if skip_mask is None else skip_mask[:, t, None] # will be used for seq2seq to skip PAD
if self.ct is None:
self.ct = (1 - ft) * zt * xt
else:
self.ct = ft * self.ct + it * zt * xt
self.ht = self.ct if O is None else ot * self.ct
if self.H is None:
self.H = functions.expand_dims(self.ht, 2)
else:
self.H = functions.concat((self.H, functions.expand_dims(self.ht, 2)), axis=2)
return self.H
def __call__(self, X, ht_enc, H_enc, skip_mask=None):
pad = self._kernel_size - 1
WX = self.W(X)
if pad > 0:
WX = WX[:, :, :-pad]
Vh = self.V(ht_enc)
Vh, WX = functions.broadcast(functions.expand_dims(Vh, axis=2), WX)
# f-pooling
Z, F, O = functions.split_axis(WX + Vh, 3, axis=1)
Z = functions.tanh(Z)
F = self.zoneout(F)
O = functions.sigmoid(O)
T = Z.shape[2]
# compute ungated hidden states
self.contexts = []
for t in xrange(T):
z = Z[..., t]
f = F[..., t]
if t == 0:
ct = (1 - f) * z
self.contexts.append(ct)
else:
ct = f * self.contexts[-1] + (1 - f) * z
self.contexts.append(ct)
if skip_mask is not None:
assert skip_mask.shape[1] == H_enc.shape[2]
softmax_bias = (skip_mask == 0) * -1e6
# compute attention weights (eq.8)
H_enc = functions.swapaxes(H_enc, 1, 2)
for t in xrange(T):
ct = self.contexts[t]
bias = 0 if skip_mask is None else softmax_bias[..., None] # to skip PAD
mask = 1 if skip_mask is None else skip_mask[..., None] # to skip PAD
alpha = functions.batch_matmul(H_enc, ct) + bias
alpha = functions.softmax(alpha) * mask
alpha = functions.broadcast_to(alpha, H_enc.shape) # copy
kt = functions.sum(alpha * H_enc, axis=1)
ot = O[..., t]
self.ht = ot * self.o(functions.concat((kt, ct), axis=1))
if t == 0:
self.H = functions.expand_dims(self.ht, 2)
else:
self.H = functions.concat((self.H, functions.expand_dims(self.ht, 2)), axis=2)
return self.H
def forward_one_step(self, X, ht_enc, H_enc, skip_mask):
pad = self._kernel_size - 1
WX = self.W(X)[:, :, -pad-1, None]
Vh = self.V(ht_enc)
Vh, WX = functions.broadcast(functions.expand_dims(Vh, axis=2), WX)
# f-pooling
Z, F, O = functions.split_axis(WX + Vh, 3, axis=1)
Z = functions.tanh(Z)
F = self.zoneout(F)
O = functions.sigmoid(O)
T = Z.shape[2]
# compute ungated hidden states
for t in xrange(T):
z = Z[..., t]
f = F[..., t]
if self.contexts is None:
ct = (1 - f) * z
self.contexts = [ct]
else:
ct = f * self.contexts[-1] + (1 - f) * z
self.contexts.append(ct)
if skip_mask is not None:
assert skip_mask.shape[1] == H_enc.shape[2]
softmax_bias = (skip_mask == 0) * -1e6
# compute attention weights (eq.8)
H_enc = functions.swapaxes(H_enc, 1, 2)
for t in xrange(T):
ct = self.contexts[t - T]
bias = 0 if skip_mask is None else softmax_bias[..., None] # to skip PAD
mask = 1 if skip_mask is None else skip_mask[..., None] # to skip PAD
alpha = functions.batch_matmul(H_enc, ct) + bias
alpha = functions.softmax(alpha) * mask
alpha = functions.broadcast_to(alpha, H_enc.shape) # copy
kt = functions.sum(alpha * H_enc, axis=1)
ot = O[..., t]
self.ht = ot * self.o(functions.concat((kt, ct), axis=1))
if self.H is None:
self.H = functions.expand_dims(self.ht, 2)
else:
self.H = functions.concat((self.H, functions.expand_dims(self.ht, 2)), axis=2)
return self.H
def calcAttention(self, h1, hList, aList, encLen, cMBSize, args):
# attention????????????????h1???
if self.attn_mode == 0:
return h1
# 1, attention????????
target1 = self.model.attnIn_L1(h1) # ??????
# (cMBSize, self.hDim) => (cMBSize, 1, self.hDim)
target2 = chaFunc.expand_dims(target1, axis=1)
# (cMBSize, 1, self.hDim) => (cMBSize, encLen, self.hDim)
target3 = chaFunc.broadcast_to(target2, (cMBSize, encLen, self.hDim))
# target3 = chaFunc.broadcast_to(chaFunc.reshape(
# target1, (cMBSize, 1, self.hDim)), (cMBSize, encLen, self.hDim))
# 2, attention?????????
if self.attn_mode == 1: # bilinear
# bilinear??attention?????hList1 == hList2 ???
# shape: (cMBSize, encLen)
aval = chaFunc.sum(target3 * aList, axis=2)
elif self.attn_mode == 2: # MLP
# attnSum ????????
t1 = chaFunc.reshape(target3, (cMBSize * encLen, self.hDim))
# (cMBSize*encLen, self.hDim) => (cMBSize*encLen, 1)
t2 = self.model.attnSum(chaFunc.tanh(t1 + aList))
# shape: (cMBSize, encLen)
aval = chaFunc.reshape(t2, (cMBSize, encLen))
# aval = chaFunc.reshape(self.model.attnSum(
# chaFunc.tanh(t1 + aList)), (cMBSize, encLen))
else:
assert 0, "ERROR"
# 3, softmax????
cAttn1 = chaFunc.softmax(aval) # (cMBSize, encLen)
# 4, attention???????context vector????????
# (cMBSize, encLen) => (cMBSize, 1, encLen)
cAttn2 = chaFunc.expand_dims(cAttn1, axis=1)
# (1, encLen) x (encLen, hDim) ?????(matmul)?cMBSize?????
# => (cMBSize, 1, hDim)
cAttn3 = chaFunc.batch_matmul(cAttn2, hList)
# cAttn3 = chaFunc.batch_matmul(chaFunc.reshape(
# cAttn1, (cMBSize, 1, encLen)), hList)
# axis=1???1????????????
context = chaFunc.reshape(cAttn3, (cMBSize, self.hDim))
# 4, attention???????context vector????????
# ??????????
# (cMBSize, scrLen) => (cMBSize, scrLen, hDim)
# cAttn2 = chaFunc.reshape(cAttn1, (cMBSize, encLen, 1))
# (cMBSize, scrLen) => (cMBSize, scrLen, hDim)
# cAttn3 = chaFunc.broadcast_to(cAttn2, (cMBSize, encLen, self.hDim))
# ???????? (cMBSize, encLen, hDim)
# => (cMBSize, hDim) # axis=1 ?????
# context = chaFunc.sum(aList * cAttn3, axis=1)
# 6, attention??????????
c1 = chaFunc.concat((h1, context))
c2 = self.model.attnOut_L2(c1)
finalH = chaFunc.tanh(c2)
# finalH = chaFunc.tanh(self.model.attnOut_L2(
# chaFunc.concat((h1, context))))
return finalH # context
# ??????