使用Keras后端实现裁剪功能

3

我想使用Keras后端实现裁剪功能:

f(x) = 

x, if 0<x<1 
1, if x>1 
0, otherwise 

我可以使用numpy进行如下操作:
def myclip(x): 
    import numpy as np 

    return np.int64((x>=1)==True)+np.multiply(np.int64( np.logical_and(x>0, x<1)==True),x) 

It could be

def myclipK(x): 

    from tensorflow.keras import backend as K 


    return K.int64((x>=1)==True)+K.multiply(np.int64( K.logical_and(x>0, x<1)==True),x) 

但是,我们没有像K.int64或K.multiply这样的东西,也没有K.logical。

我该怎么办?


3
为什么不使用K.clip()函数? - zihaozhihao
2个回答

1
你可以利用keras.backend的功能。它具有clip()、sum()、greater_than等功能。您需要使用keras.backend安排方程式,然后它应该可以工作。

1

查看Keras文档,目前无法实现。以下是所有当前可用的函数:

_broadcast_normalize_batch_in_training
_fused_normalize_batch_in_training
_get_available_gpus
_get_current_tf_device
_GRAPH_LEARNING_PHASES
_GRAPH_UID_DICTS
_has_nchw_support
_is_current_explicit_device
_LOCAL_DEVICES
_MANUAL_VAR_INIT
_preprocess_conv1d_input
_preprocess_conv2d_input
_preprocess_conv3d_input
_preprocess_padding
_regular_normalize_batch_in_training
_SESSION
_TfDeviceCaptureOp
_to_tensor
abs
all
any
arange
argmax
argmin
batch_dot
batch_flatten
batch_get_value
batch_normalization
batch_set_value
bias_add
binary_crossentropy
cast
categorical_crossentropy
clear_session
clip
concatenate
constant
conv1d
conv2d
conv2d_transpose
conv3d
conv3d_transpose
cos
count_params
ctc_batch_cost
ctc_decode
ctc_label_dense_to_sparse
cumprod
cumsum
depthwise_conv2d
dot
dropout
dtype
elu
equal
eval
exp
expand_dims
eye
flatten
foldl
foldr
function
Function
gather
get_session
get_uid
get_value
get_variable_shape
gradients
greater
greater_equal
hard_sigmoid
identity
in_test_phase
in_top_k
in_train_phase
int_shape
is_keras_tensor
is_placeholder
is_sparse
is_tensor
l2_normalize
learning_phase
less
less_equal
local_conv1d
local_conv2d
log
logsumexp
manual_variable_initialization
map_fn
max
maximum
mean
min
minimum
moving_average_update
name_scope
ndim
normalize_batch_in_training
not_equal
one_hot
ones
ones_like
permute_dimensions
placeholder
pool2d
pool3d
pow
print_tensor
prod
py_all
py_any
py_slice
py_sum
random_binomial
random_normal
random_normal_variable
random_uniform
random_uniform_variable
relu
repeat
repeat_elements
reset_uids
reshape
resize_images
resize_volumes
reverse
rnn
round
separable_conv1d
separable_conv2d
set_learning_phase
set_session
set_value
shape
sigmoid
sign
sin
slice
softmax
softplus
softsign
sparse_categorical_crossentropy
spatial_2d_padding
spatial_3d_padding
sqrt
square
squeeze
stack
std
stop_gradient
sum
switch
tanh
temporal_padding
tile
to_dense
transpose
truncated_normal
update
update_add
update_sub
var
variable
zeros
zeros_like

因此,您可以将自己的实现推送到github并为Keras做出贡献。悲伤但真实的事实。

1
如何使用 k.clip? - Sus20200
好的,我假设您想知道如何像在numpy中一样“手动”创建剪辑函数。因此,我认为我的问题回答了您最初提出的问题。 - eugen
谢谢。我可以不用等待Keras,直接使用TensorFlow编写并在Keras中使用吗? - Sus20200
是的,您可以。但问题在于您要求Keras后端,人们可以理解为您希望创建与后端无关的梯度裁剪。也就是说,我理解您想要创建一种适用于Theano、PyTorch、TensorFlow或任何其他后端的功能。这就是Keras后端背后的思想。如果您的意图只是使用TensorFlow,那么您想要使用Keras后端就相当奇怪了。希望您能明白我的意思。 - eugen
是的,我同意。现在,考虑到我只需要tensorflow,我们能否使用它来编写代码? - Sus20200
是的,你可以。但是对于这篇文章,你必须接受或拒绝我的帖子,因为我认为我回答了你最初的问题。 - eugen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接