我想在 pymc2 中为 iris 数据集构建一个 BNN 模型。
我定义了我的模型并尝试进行训练,但准确性仅0.33
取决于训练和测试数据。
这是我当前的代码
iterations = 2000
iris = load_iris()
X = iris.data[:, :]
Y = iris.target
X = scale(X)
X = X.astype(float)
Y = Y.astype(float)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.5)
w11 = pm.Normal('w11', mu=0., tau=1.)
w12 = pm.Normal('w12', mu=0., tau=1.)
w21 = pm.Normal('w21', mu=0., tau=1.)
w22 = pm.Normal('w22', mu=0., tau=1.)
w31 = pm.Normal('w31', mu=0., tau=1.)
w32 = pm.Normal('w32', mu=0., tau=1.)
x1 = X_train[:, 0]
x2 = X_train[:, 1]
# x3 = X_train[:, 2]
# x4 = X_train[:, 3]
x3 = pm.Lambda('x3', lambda w1=w11, w2=w12: np.tanh(w1 * x1 + w2 * x2))
x4 = pm.Lambda('x4', lambda w1=w21, w2=w22: np.tanh(w1 * x1 + w2 * x2))
@pm.deterministic
def activation(x=w31 * x3 + w32 * x4):
return 1. / (1. + np.exp(-x)) #sigmoid
y = pm.Categorical('y', activation, observed=True, value=Y_train)
model = pm.Model([w11, w12, w21, w22, w31, w32, y])
inference = pm.MCMC(model)
inference.sample(iterations)
y_pred_train = pm.Categorical('y_pred_train', activation)
print("Accuracy on train data = {}".format((y_pred_train.value == Y_test).mean()))
x1 = X_test[:, 0]
x2 = X_test[:, 1]
inference.sample(iterations)
y_pred_test = pm.Categorical('y_pred_test', activation)
print("Accuracy on test data = {}".format((y_pred_test.value == Y_test).mean()))
我的网络架构是
我不确定问题是否出在 和 的分类分布y
中。而且我无法跟踪这些变量来查看里面到底是什么。y_pred_train
y_pred_test
inference.trace("y")[:]
我目前的结果
Accuracy on train data = 0.3333333333333333
Accuracy on test data = 0.3333333333333333
您对提高这个分数有什么建议吗?