我一直在研究 keras 中二元分类模型的激活函数。例如,我使用 Dense(1,activation='sigmoid')
实现了一个 logit 模型,并且实现了一个 cloglog 模型,如下所示:
import numpy as np
from keras.layers import Input, Dense
from keras.models import Model
import keras.backend as K
np.random.seed(42)
# Some Data
nrows = 1000
ncols = 20
X = np.random.rand(nrows, ncols) - 0.5
CF = np.random.rand(ncols, 1)
y = np.sign(X.dot(CF))
y[np.where(y == -1)] = 0
# cloglog model
def cloglog(x):
return -(K.exp(-K.exp(x))-1)
input = Input(shape=(ncols,))
output = Dense(1, activation=cloglog)(input)
model = Model(input, output)
model.compile(loss='binary_crossentropy', optimizer='adam')
model.fit(X, y, epochs=10)
我真的很想尝试一个概率模型,但我很难仅使用 keras.backend
中可用的操作来实现链接功能。
这可能吗?或者我真的需要 scipy 中提供的分布函数(pdf/cdf)吗?
最佳答案
如果您使用 TensorFlow 作为 Keras 后端,则可以利用 tf.distributions计算正态分布的 CDF:
from tensorflow.distributions import Normal
def probit(x):
normal = Normal(loc=0.,
scale=1.)
return normal.cdf(x)
关于python - keras中的概率回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52727814/