本文整理汇总了Python中chainer.functions.max方法的典型用法代码示例。如果您正苦于以下问题:Python functions.max方法的具体用法?Python functions.max怎么用?Python functions.max使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.functions
的用法示例。
在下文中一共展示了functions.max方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: round_channels
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import max [as 别名]
def round_channels(channels,
divisor=8):
"""
Round weighted channel number (make divisible operation).
Parameters:
----------
channels : int or float
Original number of channels.
divisor : int, default 8
Alignment value.
Returns
-------
int
Weighted number of channels.
"""
rounded_channels = max(int(channels + divisor / 2.0) // divisor * divisor, divisor)
if float(rounded_channels) < 0.9 * channels:
rounded_channels += divisor
return rounded_channels
示例2: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import max [as 别名]
def __call__(self, x):
heatmap = x
vector_dim = 2
batch = heatmap.shape[0]
channels = heatmap.shape[1]
in_size = x.shape[2:]
heatmap_vector = F.reshape(heatmap, shape=(batch, channels, -1))
indices = F.cast(F.expand_dims(F.argmax(heatmap_vector, axis=vector_dim), axis=vector_dim), np.float32)
scores = F.max(heatmap_vector, axis=vector_dim, keepdims=True)
scores_mask = (scores.array > 0.0).astype(np.float32)
pts_x = (indices.array % in_size[1]) * scores_mask
pts_y = (indices.array // in_size[1]) * scores_mask
pts = F.concat((pts_x, pts_y, scores), axis=vector_dim).array
for b in range(batch):
for k in range(channels):
hm = heatmap[b, k, :, :].array
px = int(pts_x[b, k])
py = int(pts_y[b, k])
if (0 < px < in_size[1] - 1) and (0 < py < in_size[0] - 1):
pts[b, k, 0] += np.sign(hm[py, px + 1] - hm[py, px - 1]) * 0.25
pts[b, k, 1] += np.sign(hm[py + 1, px] - hm[py - 1, px]) * 0.25
return pts
示例3: update
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import max [as 别名]
def update(Q, target_Q, opt, samples, gamma=0.99, target_type='double_dqn'):
"""Update a Q-function with given samples and a target Q-function."""
dtype = chainer.get_dtype()
xp = Q.xp
obs = xp.asarray([sample[0] for sample in samples], dtype=dtype)
action = xp.asarray([sample[1] for sample in samples], dtype=np.int32)
reward = xp.asarray([sample[2] for sample in samples], dtype=dtype)
done = xp.asarray([sample[3] for sample in samples], dtype=dtype)
obs_next = xp.asarray([sample[4] for sample in samples], dtype=dtype)
# Predicted values: Q(s,a)
y = F.select_item(Q(obs), action)
# Target values: r + gamma * max_b Q(s',b)
with chainer.no_backprop_mode():
if target_type == 'dqn':
next_q = F.max(target_Q(obs_next), axis=1)
elif target_type == 'double_dqn':
next_q = F.select_item(target_Q(obs_next),
F.argmax(Q(obs_next), axis=1))
else:
raise ValueError('Unsupported target_type: {}'.format(target_type))
target = reward + gamma * (1 - done) * next_q
loss = mean_clipped_loss(y, target)
Q.cleargrads()
loss.backward()
opt.update()
示例4: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import max [as 别名]
def __call__(self, x, *args):
"""
Args:
x (ndarray): Shape is (Batch * K, 7, t).
each set has (xi, yi, zi, ri, xi −vx, yi −vy, zi −vz).
vx, vy, vz is local mean at each voxel.
Return:
y (ndarray): Shape is (Batch * K, 128)
"""
n_batch, n_channels, n_points = x.shape
# mask = F.max(x, axis=(1, 2), keepdims=True).data != 0
mask = F.max(x, axis=1, keepdims=True).data != 0
active_length = 0 #mask.sum()
# Convolution1D -> BN -> relu -> pool -> concat
h = F.relu(self.bn1(self.conv1(x), active_length, mask))
global_feat = F.max_pooling_nd(h, n_points)
# Shape is (Batch, channel, points)
global_feat_expand = F.tile(global_feat, (1, 1, n_points))
h = F.concat((h, global_feat_expand))
h *= mask
h = self.conv2(h)
return F.squeeze(F.max_pooling_nd(h, n_points))
示例5: determine_best_prediction_indices
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import max [as 别名]
def determine_best_prediction_indices(self, raw_classification_result):
distribution = F.softmax(raw_classification_result, axis=3)
predicted_classes = F.argmax(distribution, axis=3)
scores = []
for i, image in enumerate(predicted_classes):
means = []
for j, image_variant in enumerate(image):
num_predictions = len([prediction for prediction in image_variant if prediction.array != self.blank_label_class])
probs = F.max(distribution[i, j, :num_predictions], axis=1).array
if len(probs) == 0:
means.append(self.xp.array(0, dtype=probs.dtype))
means.append(self.xp.mean(probs))
means = self.xp.stack(means, axis=0)
scores.append(means)
scores = self.xp.stack(scores, axis=0)
# scores = F.sum(F.max(F.softmax(raw_classification_result, axis=3), axis=3), axis=2)
best_indices = F.argmax(scores, axis=1).array
best_indices = best_indices[:, self.xp.newaxis]
return best_indices, scores
示例6: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import max [as 别名]
def __call__(self, coord_points, feature_points=None):
# coord_points (batch_size, num_point, coord_dim)
# feature_points (batch_size, num_point, ch)
# num_point, ch: coord_dim
# grouped_points (batch_size, k, num_sample, channel)
# center_points (batch_size, k, coord_dim)
grouped_points, center_points = self.sampling_grouping(
coord_points, feature_points=feature_points)
# set alias `h` -> (bs, channel, num_sample, k)
# Note: transpose may be removed by optimizing shape sequence for sampling_groupoing
h = functions.transpose(grouped_points, (0, 3, 2, 1))
# h (bs, ch, num_sample_in_region, k=num_group)
for conv_block in self.feature_extractor_list:
h = conv_block(h)
# TODO: try other option of pooling function
h = functions.max(h, axis=2, keepdims=True)
# h (bs, ch, 1, k=num_group)
for conv_block in self.head_list:
h = conv_block(h)
h = functions.transpose(h[:, :, 0, :], (0, 2, 1))
return center_points, h # (bs, k, coord), h (bs, k, ch')
示例7: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import max [as 别名]
def __call__(self, coord_points, feature_points=None):
# coord_points (batch_size, num_point, coord_dim)
# feature_points (batch_size, num_point, ch)
# num_point, ch: coord_dim
# grouped_points (batch_size, k, num_sample, channel)
# center_points (batch_size, k, coord_dim)
grouped_points, center_points, dist = self.sampling_grouping(
coord_points, feature_points=feature_points)
# set alias `h` -> (bs, channel, num_sample, k)
# Note: transpose may be removed by optimizing shape sequence for sampling_groupoing
h = functions.transpose(grouped_points, (0, 3, 2, 1))
# h (bs, ch, num_sample_in_region, k=num_group)
for conv_block in self.feature_extractor_list:
h = conv_block(h)
# TODO: try other option of pooling function
h = functions.max(h, axis=2, keepdims=True)
# h (bs, ch, 1, k=num_group)
for conv_block in self.head_list:
h = conv_block(h)
h = functions.transpose(h[:, :, 0, :], (0, 2, 1))
# points (bs, k, coord), h (bs, k, ch'), dist (bs, k, num_point)
return center_points, h, dist
示例8: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import max [as 别名]
def __call__(self, h, axis=1, **kwargs):
if self.activation is not None:
h = self.activation(h)
else:
h = h
if self.mode == 'sum':
y = functions.sum(h, axis=axis)
elif self.mode == 'max':
y = functions.max(h, axis=axis)
elif self.mode == 'summax':
h_sum = functions.sum(h, axis=axis)
h_max = functions.max(h, axis=axis)
y = functions.concat((h_sum, h_max), axis=axis)
else:
raise ValueError('mode {} is not supported'.format(self.mode))
return y
示例9: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import max [as 别名]
def __call__(self, x, t, train=True, finetune=False):
h = self.l1(x, train, finetune)
# h = F.dropout(h, self.dr, train)
h = self.l2(h, train, finetune)
h = plane_group_spatial_max_pooling(h, ksize=2, stride=2, pad=0, cover_all=True, use_cudnn=True)
h = self.l3(h, train, finetune)
# h = F.dropout(h, self.dr, train)
h = self.l4(h, train, finetune)
# h = F.dropout(h, self.dr, train)
h = self.l5(h, train, finetune)
# h = F.dropout(h, self.dr, train)
h = self.l6(h, train, finetune)
h = self.top(h)
h = F.max(h, axis=-3, keepdims=False)
h = F.max(h, axis=-1, keepdims=False)
h = F.max(h, axis=-1, keepdims=False)
return F.softmax_cross_entropy(h, t), F.accuracy(h, t)
示例10: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import max [as 别名]
def __call__(self, x, t, train=True, finetune=False):
h = self.l1(x, train, finetune)
h = F.dropout(h, self.dr, train)
h = self.l2(h, train, finetune)
h = F.max_pooling_2d(h, ksize=2, stride=2, pad=0, cover_all=True, use_cudnn=True)
h = self.l3(h, train, finetune)
h = F.dropout(h, self.dr, train)
h = self.l4(h, train, finetune)
h = F.dropout(h, self.dr, train)
h = self.l5(h, train, finetune)
h = F.dropout(h, self.dr, train)
h = self.l6(h, train, finetune)
h = F.dropout(h, self.dr, train)
h = self.top(h)
h = F.max(h, axis=-1, keepdims=False)
h = F.max(h, axis=-1, keepdims=False)
return F.softmax_cross_entropy(h, t), F.accuracy(h, t)
示例11: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import max [as 别名]
def __call__(self, xs):
x_block = chainer.dataset.convert.concat_examples(xs, padding=-1)
ex_block = block_embed(self.embed, x_block, self.dropout)
h_w3 = F.max(self.cnn_w3(ex_block), axis=2)
h_w4 = F.max(self.cnn_w4(ex_block), axis=2)
h_w5 = F.max(self.cnn_w5(ex_block), axis=2)
h = F.concat([h_w3, h_w4, h_w5], axis=1)
h = F.relu(h)
h = F.dropout(h, ratio=self.dropout)
h = self.mlp(h)
return h
示例12: main
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import max [as 别名]
def main():
np.random.seed(314)
a1 = np.random.rand(6, 2, 3).astype(np.float32)
def test(func, name):
testtools.generate_testcase(Simple(func), [a1], subname= name + '_simple')
testtools.generate_testcase(Axis(func), [a1], subname= name + '_axis')
testtools.generate_testcase(KeepDims(func), [a1], subname= name + '_keepdims')
testtools.generate_testcase(AxisKeepDims(func), [a1], subname= name + '_axiskeepdims')
test(F.min, 'min')
test(F.max, 'max')
示例13: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import max [as 别名]
def __call__(self, xs, labels=None):
x_block = chainer.dataset.convert.concat_examples(xs, padding=-1)
ex_block = block_embed(self.embed, x_block, self.dropout)
if self.use_predict_embed and chainer.config.train:
ex_block = self.embed.embed_xs_with_prediction(
xs, labels=labels, batch='concat')
h_w3 = F.max(self.cnn_w3(ex_block), axis=2)
h_w4 = F.max(self.cnn_w4(ex_block), axis=2)
h_w5 = F.max(self.cnn_w5(ex_block), axis=2)
h = F.concat([h_w3, h_w4, h_w5], axis=1)
h = F.relu(h)
h = F.dropout(h, ratio=self.dropout)
h = self.mlp(h)
return h
示例14: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import max [as 别名]
def __call__(self, x, y):
x = self.up(x)
x = self.bn(x)
w_conf = F.softmax(x)
w_max = F.broadcast_to(F.expand_dims(F.max(w_conf, axis=1), axis=1), x.shape)
x = y * (1 - w_max) + x
return x
示例15: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import max [as 别名]
def __call__(self, tokenIdsList_merged, tokenIdsList_merged_b, argsort, argsort_reverse,
pList): # input a list of token ids, output a list of word embeddings
tokenIdsList_merged += 2
input_emb = self.embed(tokenIdsList_merged)
# input = input.reshape(input.shape[0], input.shape[1], input.shape[2])
input_emb = F.transpose(input_emb, (0, 2, 1))
input_emb = F.dropout(input_emb, self.dropout)
# print(input.shape)
if 'small' in self.subword:
h = self.cnn1(input_emb)
h = F.max(h, (2,))
else:
h1 = self.cnn1(input_emb)
h1 = F.max(h1, (2,))
h2 = self.cnn2(input_emb)
h2 = F.max(h2, (2,))
h3 = self.cnn3(input_emb)
h3 = F.max(h3, (2,))
h4 = self.cnn4(input_emb)
h4 = F.max(h4, (2,))
h5 = self.cnn5(input_emb)
h5 = F.max(h5, (2,))
h6 = self.cnn6(input_emb)
h6 = F.max(h6, (2,))
h7 = self.cnn7(input_emb)
h7 = F.max(h7, (2,))
h = F.concat((h1, h2, h3, h4, h5, h6, h7))
h = F.dropout(h, self.dropout)
h = F.tanh(h)
y = self.out(h)
# print(y.shape)
e = y
e = F.reshape(e, (int(e.shape[0] / self.n_ngram),
self.n_ngram, e.shape[1]))
e = F.sum(e, axis=1)
return e