Replace keras自定义激活在特定条件下下降

Replace keras自定义激活在特定条件下下降,replace,keras,keras-layer,activation,activation-function,Replace,Keras,Keras Layer,Activation,Activation Function,我试图在我的自定义激活中删除小于1和大于-1的值,如下所示 def ScoreActivationFromSigmoid(x, target_min=1, target_max=9) : condition = K.tf.logical_and(K.tf.less(x, 1), K.tf.greater(x, -1)) case_true = K.tf.reshape(K.tf.zeros([x.shape[1] * x.shape[2]], tf.float32), shape

我试图在我的自定义激活中删除小于1和大于-1的值,如下所示

def ScoreActivationFromSigmoid(x, target_min=1, target_max=9) :
    condition = K.tf.logical_and(K.tf.less(x, 1), K.tf.greater(x, -1))
    case_true = K.tf.reshape(K.tf.zeros([x.shape[1] * x.shape[2]], tf.float32), shape=(K.tf.shape(x)[0], x.shape[1], x.shape[2]))
    case_false = x
    changed_x = K.tf.where(condition, case_true, case_false)

    activated_x = K.sigmoid(changed_x)
    score = activated_x * (target_max - target_min) + target_min
    return  score
数据类型有3个维度:批次大小x序列长度x特征数量

但我犯了这个错误

nvalidArgumentError: Inputs to operation activation_51/Select of type Select must have the same size and shape.  Input 0: [1028,300,64] != input 1: [1,300,64]
     [[{{node activation_51/Select}} = Select[T=DT_FLOAT, _class=["loc:@training_88/Adam/gradients/activation_51/Select_grad/Select_1"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](activation_51/LogicalAnd, activation_51/Reshape, dense_243/add)]]
     [[{{node metrics_92/acc/Mean_1/_9371}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_473_metrics_92/acc/Mean_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
我明白问题是什么;自定义激活函数无法找到输入的正确批量大小。但我不知道如何控制它们


在某些情况下,是否有人可以修复此问题或建议其他方法来替换某些元素值?

我在运行代码时收到的错误消息是:

ValueError:无法重塑包含19200个元素的张量的形状 [1028300,64]19737600“重塑_8”操作的元素:“重塑”带有 输入形状:[19200],[3]和输入张量计算为部分 形状:输入[1]=[1028300,64]

问题应该是你不能将[x.shape[1]*x.shape[2]]的张量重塑为K.tf.shapex[0],x.shape[1],x.shape[2]。这是因为它们的元素计数不同

所以解决方案就是创建一个形状正确的零数组。 这一行:

case_true = K.tf.reshape(K.tf.zeros([x.shape[1] * x.shape[2]], tf.float32), shape=(K.tf.shape(x)[0], x.shape[1], x.shape[2]))
应替换为:

case_true = K.tf.reshape(K.tf.zeros([x.shape[0] * x.shape[1] * x.shape[2]], K.tf.float32), shape=(K.tf.shape(x)[0], x.shape[1], x.shape[2]))
或者使用K.tf.zero_,比如:

可行代码:

import keras.backend as K
import numpy as np

def ScoreActivationFromSigmoid(x, target_min=1, target_max=9) :
    condition = K.tf.logical_and(K.tf.less(x, 1), K.tf.greater(x, -1))
    case_true = K.tf.zeros_like(x)
    case_false = x
    changed_x = K.tf.where(condition, case_true, case_false)

    activated_x = K.tf.sigmoid(changed_x)
    score = activated_x * (target_max - target_min) + target_min
    return  score

with K.tf.Session() as sess:
    x = K.tf.placeholder(K.tf.float32, shape=(1028, 300, 64), name='x')
    score = sess.run(ScoreActivationFromSigmoid(x), feed_dict={'x:0':np.random.randn(1028, 300, 64)})

print(score)

你确定你能为你的激活函数提供可微性吗?非常感谢,我已经用和你非常相似的方法解决了这个问题。不过,我还是要感谢你的详细解释!
import keras.backend as K
import numpy as np

def ScoreActivationFromSigmoid(x, target_min=1, target_max=9) :
    condition = K.tf.logical_and(K.tf.less(x, 1), K.tf.greater(x, -1))
    case_true = K.tf.zeros_like(x)
    case_false = x
    changed_x = K.tf.where(condition, case_true, case_false)

    activated_x = K.tf.sigmoid(changed_x)
    score = activated_x * (target_max - target_min) + target_min
    return  score

with K.tf.Session() as sess:
    x = K.tf.placeholder(K.tf.float32, shape=(1028, 300, 64), name='x')
    score = sess.run(ScoreActivationFromSigmoid(x), feed_dict={'x:0':np.random.randn(1028, 300, 64)})

print(score)