keras自定义激活在某些条件下会下降

时间:2019-02-07 01:10:01

标签: 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=(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个维度:batch_size x sequence_length 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"]()]]

我了解问题出在哪里;自定义激活功能找不到合适的输入批次大小。但是我不知道如何控制它们。

任何人都可以在某些情况下解决此问题或建议其他方法替换某些元素值吗?

1 个答案:

答案 0 :(得分:1)

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

  

ValueError:无法重塑具有19200个元素以进行成形的张量   [1028,300,64](19737600个元素)用于“ Reshape_8”(操作:“ Reshape”),其中   输入形状:[19200],[3],输入张量计算为部分   形状:input [1] = [1028,300,64]。

问题应该是您无法将形状为[x.shape [1] * x.shape [2]]的张量整形为(K.tf.shape(x)[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.zeros_like

case_true = K.tf.zeros_like(x)

可行代码:

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)