本文整理汇总了Python中chainer.functions.flatten方法的典型用法代码示例。如果您正苦于以下问题:Python functions.flatten方法的具体用法?Python functions.flatten怎么用?Python functions.flatten使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.functions
的用法示例。
在下文中一共展示了functions.flatten方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import flatten [as 别名]
def __call__(self, x, alpha=1.0):
if self.depth > 0 and alpha < 1:
h1 = self['b%d'%(7-self.depth)](x, True)
x2 = F.average_pooling_2d(x, 2, 2)
h2 = F.leaky_relu(self['b%d'%(7-self.depth+1)].fromRGB(x2))
h = h2 * (1 - alpha) + h1 * alpha
else:
h = self['b%d'%(7-self.depth)](x, True)
for i in range(self.depth):
h = self['b%d'%(7-self.depth+1+i)](h)
h = self.l(h)
h = F.flatten(h)
return h
示例2: update_q_func
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import flatten [as 别名]
def update_q_func(self, batch):
"""Compute loss for a given Q-function."""
batch_next_state = batch['next_state']
batch_rewards = batch['reward']
batch_terminal = batch['is_state_terminal']
batch_state = batch['state']
batch_actions = batch['action']
batch_discount = batch['discount']
with chainer.no_backprop_mode(), chainer.using_config('train', False):
next_action_distrib = self.policy(batch_next_state)
next_actions, next_log_prob =\
next_action_distrib.sample_with_log_prob()
next_q1 = self.target_q_func1(batch_next_state, next_actions)
next_q2 = self.target_q_func2(batch_next_state, next_actions)
next_q = F.minimum(next_q1, next_q2)
entropy_term = self.temperature * next_log_prob[..., None]
assert next_q.shape == entropy_term.shape
target_q = batch_rewards + batch_discount * \
(1.0 - batch_terminal) * F.flatten(next_q - entropy_term)
predict_q1 = F.flatten(self.q_func1(batch_state, batch_actions))
predict_q2 = F.flatten(self.q_func2(batch_state, batch_actions))
loss1 = 0.5 * F.mean_squared_error(target_q, predict_q1)
loss2 = 0.5 * F.mean_squared_error(target_q, predict_q2)
# Update stats
self.q1_record.extend(cuda.to_cpu(predict_q1.array))
self.q2_record.extend(cuda.to_cpu(predict_q2.array))
self.q_func1_loss_record.append(float(loss1.array))
self.q_func2_loss_record.append(float(loss2.array))
self.q_func1_optimizer.update(lambda: loss1)
self.q_func2_optimizer.update(lambda: loss2)
示例3: update_q_func
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import flatten [as 别名]
def update_q_func(self, batch):
"""Compute loss for a given Q-function."""
batch_next_state = batch['next_state']
batch_rewards = batch['reward']
batch_terminal = batch['is_state_terminal']
batch_state = batch['state']
batch_actions = batch['action']
batch_discount = batch['discount']
with chainer.no_backprop_mode(), chainer.using_config('train', False):
next_actions = self.target_policy_smoothing_func(
self.target_policy(batch_next_state).sample().array)
next_q1 = self.target_q_func1(batch_next_state, next_actions)
next_q2 = self.target_q_func2(batch_next_state, next_actions)
next_q = F.minimum(next_q1, next_q2)
target_q = batch_rewards + batch_discount * \
(1.0 - batch_terminal) * F.flatten(next_q)
predict_q1 = F.flatten(self.q_func1(batch_state, batch_actions))
predict_q2 = F.flatten(self.q_func2(batch_state, batch_actions))
loss1 = F.mean_squared_error(target_q, predict_q1)
loss2 = F.mean_squared_error(target_q, predict_q2)
# Update stats
self.q1_record.extend(cuda.to_cpu(predict_q1.array))
self.q2_record.extend(cuda.to_cpu(predict_q2.array))
self.q_func1_loss_record.append(float(loss1.array))
self.q_func2_loss_record.append(float(loss2.array))
self.q_func1_optimizer.update(lambda: loss1)
self.q_func2_optimizer.update(lambda: loss2)
示例4: _flatten_and_concat_variables
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import flatten [as 别名]
def _flatten_and_concat_variables(vs):
"""Flatten and concat variables to make a single flat vector variable."""
return F.concat([F.flatten(v) for v in vs], axis=0)
示例5: _compute_ppo_loss
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import flatten [as 别名]
def _compute_ppo_loss(self, obs, acts, at, vt, old_params):
params = self._pi_f(obs)
cv = F.flatten(self._vf_f(obs))
ratio = F.exp(self._logp(params, acts) - self._logp(old_params, acts))
surr1 = ratio * at
surr2 = F.clip(ratio, 1 - self._ppo_clipparam, 1 + self._ppo_clipparam) * at
ppo_surr_loss = (
-sym_mean(F.minimum(surr1, surr2))
+ self._ppo_klcoeff * sym_mean(self.kl(old_params, params))
+ sym_mean(F.square(cv - vt))
)
return ppo_surr_loss
示例6: forward_expected
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import flatten [as 别名]
def forward_expected(self, inputs):
x, = inputs
return x.flatten(),
示例7: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import flatten [as 别名]
def forward(self, inputs, device):
x, = inputs
y = functions.flatten(x)
return y,
示例8: _elementwise_softmax_cross_entropy
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import flatten [as 别名]
def _elementwise_softmax_cross_entropy(x, t):
assert x.shape[:-1] == t.shape
shape = t.shape
x = F.reshape(x, (-1, x.shape[-1]))
t = F.flatten(t)
return F.reshape(
F.softmax_cross_entropy(x, t, reduce='no'), shape)