def mseGrad(y, trueY, G):
try:
k,n = G.shape
except:
import IPython; IPython.embed(); sys.exit(-1)
assert(len(y) == n)
# y_ = np.copy(y)
# eps = 1e-8
# y_ = np.clip(y_, eps, 1.-eps)
I = np.where((y > 1e-8) & (1.-y > 1e-8))
# print(len(I[0]))
z = np.ones_like(y)
z[I] = (1./y[I] + 1./(1.-y[I]))
z = 1./y + 1./(1.-y)
zinv = 1./z
G_zinv = G*zinv
G_zinv_GT = np.dot(G_zinv, G.T)
H = np.bmat([[G_zinv_GT, np.ones((k,1))], [np.ones((1,k)), np.zeros((1,1))]])
# Different scaling than the MSE plots.
dl = -(y-trueY)
b = np.concatenate([np.dot(G_zinv, dl), np.zeros(1)])
clamt = np.linalg.solve(H, b)
clam, ct = np.split(clamt, [k])
cy = zinv*dl - np.dot((G*zinv).T, clam)
cy[(y == 0) | (y == 1)] = 0
return cy, clam, ct
评论列表
文章目录