本文整理汇总了Python中cntk.reshape方法的典型用法代码示例。如果您正苦于以下问题:Python cntk.reshape方法的具体用法?Python cntk.reshape怎么用?Python cntk.reshape使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cntk
的用法示例。
在下文中一共展示了cntk.reshape方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _remove_dims
# 需要导入模块: import cntk [as 别名]
# 或者: from cntk import reshape [as 别名]
def _remove_dims(x, axis, keepdims=False):
if keepdims is False and isinstance(axis, list):
# sequence axis is removed by default, so don't need reshape on it
reduce_axes = []
for a in axis:
if isinstance(a, C.Axis) is False:
reduce_axes.append(a)
return _reshape_dummy_dim(x, reduce_axes)
else:
if isinstance(axis, list):
has_seq = False
for a in axis:
if isinstance(a, C.Axis):
has_seq = True
break
if has_seq:
nones = _get_dynamic_axis_num(x)
x = expand_dims(x, nones)
return x
示例2: squeeze
# 需要导入模块: import cntk [as 别名]
# 或者: from cntk import reshape [as 别名]
def squeeze(x, axis):
if isinstance(axis, tuple):
axis = list(axis)
if not isinstance(axis, list):
axis = [axis]
shape = list(int_shape(x))
_axis = []
for _ in axis:
if isinstance(_, int):
_axis.append(_ if _ >= 0 else _ + len(shape))
if len(_axis) == 0:
return x
nones = _get_dynamic_axis_num(x)
for _ in sorted(_axis, reverse=True):
del shape[_]
new_shape = shape[nones:]
new_shape = tuple([C.InferredDimension if _ == C.FreeDimension else _ for _ in new_shape])
return C.reshape(x, new_shape)
示例3: _reshape_dummy_dim
# 需要导入模块: import cntk [as 别名]
# 或者: from cntk import reshape [as 别名]
def _reshape_dummy_dim(x, axis):
shape = list(x.shape)
_axis = [_ + len(shape) if _ < 0 else _ for _ in axis]
if shape.count(C.InferredDimension) > 1 or shape.count(C.FreeDimension) > 1:
result = x
for index in sorted(_axis, reverse=True):
result = C.reshape(result,
shape=(),
begin_axis=index,
end_axis=index + 1)
return result
else:
for index in sorted(_axis, reverse=True):
del shape[index]
shape = [C.InferredDimension if _ == C.FreeDimension else _ for _ in shape]
return C.reshape(x, shape)
示例4: repeat
# 需要导入模块: import cntk [as 别名]
# 或者: from cntk import reshape [as 别名]
def repeat(x, n):
# this is a workaround for recurrent layer
# if n is inferred dimension,
# we can't figure out how to repeat it in cntk now
# return the same x to take cntk broadcast feature
# to make the recurrent layer work.
# need to be fixed in GA.
if n is C.InferredDimension or n is C.FreeDimension:
return x
index = 1 - _get_dynamic_axis_num(x)
if index < 0 or index > 1:
raise NotImplementedError
new_shape = list(x.shape)
new_shape.insert(index, 1)
new_shape = tuple(new_shape)
x = C.reshape(x, new_shape)
temp = [x] * n
return C.splice(*temp, axis=index)
示例5: _layer_Conv
# 需要导入模块: import cntk [as 别名]
# 或者: from cntk import reshape [as 别名]
def _layer_Conv(self):
self.add_body(0, """
def convolution(input, is_transpose, name, **kwargs):
dim = __weights_dict[name]['weights'].ndim
if is_transpose:
weight = np.transpose(__weights_dict[name]['weights'], [dim - 2, dim - 1] + list(range(0, dim - 2)))
kwargs.pop('groups', None)
else:
weight = np.transpose(__weights_dict[name]['weights'], [dim - 1, dim - 2] + list(range(0, dim - 2)))
w = cntk.Parameter(init=weight, name=name + '_weight')
input = cntk.transpose(input, [dim - 2] + list(range(0, dim - 2)))
if is_transpose:
layer = ops.convolution_transpose(w, input, **kwargs)
else:
layer = ops.convolution(w, input, **kwargs)
if 'bias' in __weights_dict[name]:
bias = np.reshape(__weights_dict[name]['bias'], [-1] + [1] * (dim - 2))
b = cntk.Parameter(init=bias, name=name + '_bias')
layer = layer + b
layer = cntk.transpose(layer, list(range(1, dim - 1)) + [0])
return layer
""")
示例6: random_binomial
# 需要导入模块: import cntk [as 别名]
# 或者: from cntk import reshape [as 别名]
def random_binomial(shape, p=0.0, dtype=None, seed=None):
# use numpy workaround now
if seed is None:
# ensure that randomness is conditioned by the Numpy RNG
seed = np.random.randint(10e7)
np.random.seed(seed)
if dtype is None:
dtype = np.float32
else:
dtype = _convert_string_dtype(dtype)
size = 1
for _ in shape:
if _ is None:
raise ValueError('CNTK Backend: randomness op with '
'dynamic shape is not supported now. '
'Please provide fixed dimension '
'instead of `None`.')
size *= _
binomial = np.random.binomial(1, p, size).astype(dtype).reshape(shape)
return variable(value=binomial, dtype=dtype)
示例7: local_conv1d
# 需要导入模块: import cntk [as 别名]
# 或者: from cntk import reshape [as 别名]
def local_conv1d(inputs, kernel, kernel_size, strides, data_format=None):
if data_format is None:
data_format = image_data_format()
if data_format not in {'channels_first', 'channels_last'}:
raise ValueError('Unknown data_format ' + str(data_format))
stride = strides[0]
kernel_shape = int_shape(kernel)
output_length, feature_dim, filters = kernel_shape
xs = []
for i in range(output_length):
slice_length = slice(i * stride,
i * stride + kernel_size[0])
xs.append(reshape(inputs[:, slice_length, :],
(-1, 1, feature_dim)))
x_aggregate = concatenate(xs, axis=1)
# transpose kernel to output_filters first, to apply broadcast
weight = permute_dimensions(kernel, (2, 0, 1))
# Shape: (batch, filters, output_length, input_length * kernel_size)
output = x_aggregate * weight
# Shape: (batch, filters, output_length)
output = sum(output, axis=3)
# Shape: (batch, output_length, filters)
return permute_dimensions(output, (0, 2, 1))
示例8: expand_dims
# 需要导入模块: import cntk [as 别名]
# 或者: from cntk import reshape [as 别名]
def expand_dims(x, axis=-1):
shape = list(int_shape(x))
nones = _get_dynamic_axis_num(x)
index = axis if axis >= 0 else len(shape) + 1
shape.insert(index, 1)
new_shape = shape[nones:]
new_shape = tuple(
[C.InferredDimension if _ is None else _ for _ in new_shape])
result = C.reshape(x, new_shape)
if index < nones:
result._keras_shape = shape
return result
示例9: flatten
# 需要导入模块: import cntk [as 别名]
# 或者: from cntk import reshape [as 别名]
def flatten(x):
return reshape(x, (-1,))
示例10: reshape
# 需要导入模块: import cntk [as 别名]
# 或者: from cntk import reshape [as 别名]
def reshape(x, shape):
shape = tuple([C.InferredDimension if _ == C.FreeDimension else _ for _ in shape])
if isinstance(x, C.variables.Parameter):
return C.reshape(x, shape)
else:
num_dynamic_axis = _get_dynamic_axis_num(x)
if num_dynamic_axis == 1 and len(shape) > 0 and shape[0] == -1:
# collapse axis with batch axis
if b_any(_ == C.InferredDimension for _ in x.shape) or b_any(
_ == C.FreeDimension for _ in x.shape):
warnings.warn(
'Warning: CNTK backend does not support '
'collapse of batch axis with inferred dimension. '
'The reshape did not take place.')
return x
return _reshape_batch(x, shape)
else:
# no collapse, then first need to padding the shape
if num_dynamic_axis >= len(shape):
i = 0
while i < len(shape):
if shape[i] is None or shape[i] == -1:
i += 1
else:
break
shape = tuple([-1 for _ in range(num_dynamic_axis - i)]) + shape
new_shape = list(shape)
new_shape = new_shape[num_dynamic_axis:]
new_shape = [C.InferredDimension if _ is None else _ for _ in new_shape]
return C.reshape(x, new_shape)
示例11: separable_conv2d
# 需要导入模块: import cntk [as 别名]
# 或者: from cntk import reshape [as 别名]
def separable_conv2d(x, depthwise_kernel, pointwise_kernel, strides=(1, 1),
padding='valid', data_format=None, dilation_rate=(1, 1)):
data_format = normalize_data_format(data_format)
x = _preprocess_conv2d_input(x, data_format)
depthwise_kernel = _preprocess_conv2d_kernel(depthwise_kernel, data_format)
depthwise_kernel = C.reshape(C.transpose(depthwise_kernel, (1, 0, 2, 3)),
(-1, 1) + depthwise_kernel.shape[2:])
pointwise_kernel = _preprocess_conv2d_kernel(pointwise_kernel, data_format)
padding = _preprocess_border_mode(padding)
if dilation_rate == (1, 1):
strides = (1,) + strides
x = C.convolution(depthwise_kernel, x,
strides=strides,
auto_padding=[False, padding, padding],
groups=x.shape[0])
x = C.convolution(pointwise_kernel, x,
strides=(1, 1, 1),
auto_padding=[False])
else:
if dilation_rate[0] != dilation_rate[1]:
raise ValueError('CNTK Backend: non-square dilation_rate is '
'not supported.')
if strides != (1, 1):
raise ValueError('Invalid strides for dilated convolution')
x = C.convolution(depthwise_kernel, x,
strides=dilation_rate[0],
auto_padding=[False, padding, padding])
x = C.convolution(pointwise_kernel, x,
strides=(1, 1, 1),
auto_padding=[False])
return _postprocess_conv2d_output(x, data_format)
示例12: batch_flatten
# 需要导入模块: import cntk [as 别名]
# 或者: from cntk import reshape [as 别名]
def batch_flatten(x):
# cntk's batch axis is not in shape,
# so just flatten all the dim in x.shape
dim = np.prod(x.shape)
x = C.reshape(x, (-1,))
x._keras_shape = (None, dim)
return x
示例13: categorical_crossentropy
# 需要导入模块: import cntk [as 别名]
# 或者: from cntk import reshape [as 别名]
def categorical_crossentropy(target, output, from_logits=False, axis=-1):
# Here, unlike other backends, the tensors lack a batch dimension:
axis_without_batch = -1 if axis == -1 else axis - 1
output_dimensions = list(range(len(output.shape)))
if axis_without_batch != -1 and axis_without_batch not in output_dimensions:
raise ValueError(
'{}{}{}'.format(
'Unexpected channels axis {}. '.format(axis_without_batch),
'Expected to be -1 or one of the axes of `output`, ',
'which has {} dimensions.'.format(len(output.shape))))
# If the channels are not in the last axis, move them to be there:
if axis_without_batch != -1 and axis_without_batch != output_dimensions[-1]:
permutation = output_dimensions[:axis_without_batch]
permutation += output_dimensions[axis_without_batch + 1:]
permutation += [axis_without_batch]
output = C.transpose(output, permutation)
target = C.transpose(target, permutation)
if from_logits:
result = C.cross_entropy_with_softmax(output, target)
# cntk's result shape is (batch, 1), while keras expect (batch, )
return C.reshape(result, ())
else:
# scale preds so that the class probas of each sample sum to 1
output /= C.reduce_sum(output, axis=-1)
# avoid numerical instability with epsilon clipping
output = C.clip(output, epsilon(), 1.0 - epsilon())
return -sum(target * C.log(output), axis=-1)
示例14: sparse_categorical_crossentropy
# 需要导入模块: import cntk [as 别名]
# 或者: from cntk import reshape [as 别名]
def sparse_categorical_crossentropy(target, output, from_logits=False, axis=-1):
# Here, unlike other backends, the tensors lack a batch dimension:
axis_without_batch = -1 if axis == -1 else axis - 1
output_dimensions = list(range(len(output.shape)))
if axis_without_batch != -1 and axis_without_batch not in output_dimensions:
raise ValueError(
'{}{}{}'.format(
'Unexpected channels axis {}. '.format(axis_without_batch),
'Expected to be -1 or one of the axes of `output`, ',
'which has {} dimensions.'.format(len(output.shape))))
target = C.one_hot(target, output.shape[axis_without_batch],
axis=axis_without_batch)
target = C.reshape(target, output.shape)
return categorical_crossentropy(target, output, from_logits, axis=axis)
示例15: in_top_k
# 需要导入模块: import cntk [as 别名]
# 或者: from cntk import reshape [as 别名]
def in_top_k(predictions, targets, k):
_targets = C.one_hot(targets, predictions.shape[-1])
result = C.classification_error(predictions, _targets, topN=k)
return 1 - C.reshape(result, shape=())