本文整理匯總了Python中cntk.clip方法的典型用法代碼示例。如果您正苦於以下問題:Python cntk.clip方法的具體用法?Python cntk.clip怎麽用?Python cntk.clip使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cntk
的用法示例。
在下文中一共展示了cntk.clip方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: relu
# 需要導入模塊: import cntk [as 別名]
# 或者: from cntk import clip [as 別名]
def relu(x, alpha=0., max_value=None, threshold=0.):
if alpha != 0.:
if threshold != 0.:
negative_part = C.relu(-x + threshold)
else:
negative_part = C.relu(-x)
if threshold != 0.:
x = x * C.greater(x, threshold)
else:
x = C.relu(x)
if max_value is not None:
x = C.clip(x, 0.0, max_value)
if alpha != 0.:
x -= alpha * negative_part
return x
示例2: clip
# 需要導入模塊: import cntk [as 別名]
# 或者: from cntk import clip [as 別名]
def clip(x, min_value, max_value):
if max_value is not None and max_value < min_value:
max_value = min_value
if max_value is None:
max_value = np.inf
if min_value is None:
min_value = -np.inf
return C.clip(x, min_value, max_value)
示例3: binary_crossentropy
# 需要導入模塊: import cntk [as 別名]
# 或者: from cntk import clip [as 別名]
def binary_crossentropy(target, output, from_logits=False):
if from_logits:
output = C.sigmoid(output)
output = C.clip(output, epsilon(), 1.0 - epsilon())
output = -target * C.log(output) - (1.0 - target) * C.log(1.0 - output)
return output
示例4: hard_sigmoid
# 需要導入模塊: import cntk [as 別名]
# 或者: from cntk import clip [as 別名]
def hard_sigmoid(x):
x = (0.2 * x) + 0.5
x = C.clip(x, 0.0, 1.0)
return x
示例5: categorical_crossentropy
# 需要導入模塊: import cntk [as 別名]
# 或者: from cntk import clip [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)
示例6: emit_Relu6
# 需要導入模塊: import cntk [as 別名]
# 或者: from cntk import clip [as 別名]
def emit_Relu6(self, IR_node):
codes = list()
codes.append(self.emit_Relu(IR_node))
codes.append("{:<15} = cntk.clip({}, 0, 6, name='{}_clip')".format(
IR_node.variable_name + "_clip",
IR_node.variable_name,
IR_node.name
))
IR_node.real_name = IR_node.name + '_clip'
return codes
示例7: relu
# 需要導入模塊: import cntk [as 別名]
# 或者: from cntk import clip [as 別名]
def relu(x, alpha=0., max_value=None):
if alpha != 0.:
negative_part = C.relu(-x)
x = C.relu(x)
if max_value is not None:
x = C.clip(x, 0.0, max_value)
if alpha != 0.:
x -= alpha * negative_part
return x
示例8: categorical_crossentropy
# 需要導入模塊: import cntk [as 別名]
# 或者: from cntk import clip [as 別名]
def categorical_crossentropy(target, output, from_logits=False):
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)