keras显着性检验中逻辑回归的标准误

时间:2020-03-30 10:15:56

标签: python keras logistic-regression

我有两个自变量(x1,x2),用于预测y(二进制)。通过logistic回归,我可以使用每个估计系数的标准误差来检验其显着性。

但是,我有一个基于inputA(一些文本)和inputB(数字数据)的深度网络。

这意味着,我将不得不从最后一层中提取标准误差,以测试inputB的系数的重要性。否则,将无法检查inputB是否确实显着增加了模型。 如何从深度学习模型(keras)中的逻辑回归中提取标准误差?

#### Network
# define two sets of inputs
inputA = Input(shape=(32,))
inputB = Input(shape=(128,))

# the first branch operates on the first input
x = Dense(8, activation="relu")(inputA)
x = Dense(4, activation="relu")(x)
x = Model(inputs=inputA, outputs=x)

# the second branch opreates on the second input
y = Dense(64, activation="relu")(inputB)
y = Dense(32, activation="relu")(y)
y = Dense(4, activation="relu")(y)
y = Model(inputs=inputB, outputs=y)

# combine the output of the two branches
combined = concatenate([x.output, y.output])

# our model will accept the inputs of the two branches and
# then output a single value
preds = Dense(1, activation='sigmoid',name='output')(combined) 
model = Model(inputs=[x.input, y.input], outputs=[preds])

model.compile(loss='binary_crossentropy',optimizer='adam', metrics=['acc'])

model.fit([x_train,numeric], y_train, epochs=40, batch_size=50)

编辑:

现在,我发现了这个有用的链接:

https://stats.stackexchange.com/questions/89484/how-to-compute-the-standard-errors-of-a-logistic-regressions-coefficients

所以我认为我可以使用y_pred = model.predict(inputs=[x_train,numeric], verbose=1) # gives probabilities

然后,我必须在以下代码中输入combined ...但是我该怎么做..还是我的方法是错误的?

#### Standard Error testing
# Design matrix -- add column of 1's at the beginning of your X_train matrix
X_design = np.hstack([np.ones((combined.shape[0], 1)), combined])

# Initiate matrix of 0's, fill diagonal with each predicted observation's variance
V = np.diagflat(np.product(y_preds, axis=1))


# Covariance matrix
covLogit = np.linalg.inv(np.dot(np.dot(X_design.T, V), X_design))

有人可以添加一些建议/验证吗?

Edit2

让我感到困惑的是,我有两个输入,一个数字输入numeric和一个非数字输入x_train。为了测试系数,我必须创建一个 combined-input 形状的矩阵(实际上是由组合的输入填充的)。

然后,我可以使用模型预测来测试最后一层系数的显着性(如系数测试的参考链接所述)。

但是我怎么输入combined-input ..还是我在某个地方弄错了?

1 个答案:

答案 0 :(得分:4)

不确定此答案对您有好处。...

我该怎么办?

  • 仅使用inputA测试网络。
  • 仅使用inputB测试网络。
  • 测试组合网络。

另一个赢家是我要使用的那个。


获取网络允许通过的每个输入的数量:

如果获得最后一层的权重,则将有两个张量:

  • 一个(1, 8)权重矩阵(或(8, 1),在这种情况下无关紧要)。
  • 一个(1,)偏差值

获取它们:

w, b = model.get_layer("output").get_weights()

展平w(这没关系,因为您只有1个输出单位),并查看网络对每个输入进行加权的程度。遵循将x和y串联的顺序:

w = w.reshape((-1,))
weights_x = w[:4] #the first 4 weigths are multiplying `x.output`   
weights_y = w[4:] #the last 4 weights are multiplying `y.output`
相关问题