def run_xor():
from operator import xor
from scipy import special
# create dataset
print "generating dataset..."
n = 25
Y = np.zeros((0, 3))
for i in [0, 1]:
for j in [0, 1]:
a = i * np.ones((n, 1))
b = j * np.ones((n, 1))
c = xor(bool(i), bool(j)) * np.ones((n, 1))
Y_ij = np.hstack((a, b, c))
Y = np.vstack((Y, Y_ij))
Y = 2 * Y - 1
# inference
print "inference ..."
M = 10
D = 2
lvm = aep.SGPLVM(Y, D, M, lik='Probit')
lvm.optimise(method='L-BFGS-B', alpha=0.1, maxiter=200)
# predict given inputs
mx, vx = lvm.get_posterior_x()
lims = [-1.5, 1.5]
x = np.linspace(*lims, num=101)
y = np.linspace(*lims, num=101)
X, Y = np.meshgrid(x, y)
X_ravel = X.ravel()
Y_ravel = Y.ravel()
inputs = np.vstack((X_ravel, Y_ravel)).T
my, vy = lvm.predict_f(inputs)
t = my / np.sqrt(1 + vy)
Z = 0.5 * (1 + special.erf(t / np.sqrt(2)))
for d in range(3):
plt.figure()
plt.scatter(mx[:, 0], mx[:, 1])
zu = lvm.sgp_layer.zu
plt.plot(zu[:, 0], zu[:, 1], 'ko')
plt.contour(X, Y, np.log(Z[:, d] + 1e-16).reshape(X.shape))
plt.xlim(*lims)
plt.ylim(*lims)
# Y_test = np.array([[1, -1, 1], [-1, 1, 1], [-1, -1, -1], [1, 1, -1]])
# # impute missing data
# for k in range(3):
# Y_test_k = Y_test
# missing_mask = np.ones_like(Y_test_k)
# missing_mask[:, k] = 0
# my_pred, vy_pred = lvm.impute_missing(
# Y_test_k, missing_mask,
# alpha=0.1, no_iters=100, add_noise=False)
# print k, my_pred, vy_pred, Y_test_k
plt.show()
评论列表
文章目录