def initialize(data, lengths, factors): mu = np.average(data[:, 2]) ulen, ilen = lengths[0], lengths[1] p, q = randvec((ulen, factors)) - 0.5, randvec((ilen, factors)) - 0.5 nu, bu = np.zeros(ulen), np.zeros(ulen) ni, bi = np.zeros(ilen), np.zeros(ilen) for u, i, r in data: nu[u] += 1 ni[i] += 1 bu[u] += r - mu bi[i] += r - mu for i in range(ilen): bi[i] = bi[i] / ni[i] if ni[i] != 0 else 0 for u in range(ulen): bu[u] = bu[u] / nu[u] if nu[u] != 0 else 0 return mu, bu, bi, p, q
def sgd(data, users, itesm, lam1, lam2, gamma1, gamma2): print '[INFO] Start learning on λ1 = %.6f, λ2 = %.6f, ɣ1 = %.6f, ɣ2 = %.6f' % (lam1, lam2, gamma1, gamma2) mu, bu, bi = get_normalized_info(data, users, itesm) p, q = randvec((users, 50)) - 1/2., randvec((itesm, 50)) - 1/2. steps = 10 for step in range(steps): print '[INFO] Step %d/%d...' % (step + 1, steps) for u, i, r in data: e = r - (mu + bu[u] + bi[i] + np.dot(p[u], q[i])) bu[u] += gamma1 * (e - lam1 * bu[u]) bi[i] += gamma1 * (e - lam1 * bi[i]) pu1 = p[u] + gamma2 * (e * q[i] - lam2 * p[u]) qi1 = q[i] + gamma2 * (e * p[u] - lam2 * q[i]) p[u], q[i] = pu1, qi1 print '[INFO] Learned.' return lambda u, i: mu + bu[u] + bi[i] + np.dot(p[u], q[i])
def sgd(data, users, itesm, lam1, lam2, gamma1, gamma2): print '[INFO] Start learning on λ1 = %.6f, λ2 = %.6f, ɣ1 = %.6f, ɣ2 = %.6f' % ( lam1, lam2, gamma1, gamma2) mu, bu, bi = get_normalized_info(data, users, itesm) p, q = randvec((users, 50)) - 1 / 2., randvec((itesm, 50)) - 1 / 2. steps = 10 for step in range(steps): print '[INFO] Step %d/%d...' % (step + 1, steps) for u, i, r in data: e = r - (mu + bu[u] + bi[i] + np.dot(p[u], q[i])) bu[u] += gamma1 * (e - lam1 * bu[u]) bi[i] += gamma1 * (e - lam1 * bi[i]) pu1 = p[u] + gamma2 * (e * q[i] - lam2 * p[u]) qi1 = q[i] + gamma2 * (e * p[u] - lam2 * q[i]) p[u], q[i] = pu1, qi1 print '[INFO] Learned.' return lambda u, i: mu + bu[u] + bi[i] + np.dot(p[u], q[i])