【发布时间】:2020-02-03 06:24:27
【问题描述】:
我正在尝试编写一个 Keras 后端函数,如果输入 x 在 a 和 b 之间,它给出 1;否则,它给出零。我无法使用 Keras 后端中提供的功能来做到这一点。如果是 numpy,我会写:
def my_function(x):
import numpy as np
y=np.int64(np.logical_and(x>=a, x<=b))
return y
问题 1:如何使用 Keras 后端做到这一点?我知道我可以使用这样的东西,但效率不高
def my_function(x):
from keras import backend as K
y=x
for i in y:
if i<=b and i>=a:
i=1
else:
i=0
return y
问题 2: 我已经安装了 TensorFlow 1.14.0 和 Keras 2.2.6,所以我认为后端是 Tensorflow。如果我不能在 Keras 后端做到这一点。如何在 TensorFlow 后端编写函数?
【问题讨论】:
-
输入的形状是什么?
-
@zihaozhihao 我不知道。它是一个层的输出。我们可以为一般输入形状编写它吗?
-
哦,我明白了。也许它应用于 logits 或 probs。
-
将用于神经网络
标签: python python-3.x tensorflow keras