In [ ]:
import edward as ed
from edward.models import Poisson,Gamma
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import helper_func
import math
import models
import scipy.special as sp
from scipy.misc import logsumexp
import gc
In [ ]:
sess = tf.InteractiveSession()
init = tf.global_variables_initializer()
init.run()
In [ ]:
dataset = 'bibx'
full_X,x,test_mask1 = helper_func.load_data(dataset)
dataset = 'biby'
full_Y,y,test_mask2 = helper_func.load_data(dataset)
metric = 'mae_nz_all'
x = full_X #*x_train_mask
y = full_Y #*y_train_mask
tot = 100
tot += 1
test_every = 20
non_zero_x = helper_func.non_zero_entries(x)
non_zero_y = helper_func.non_zero_entries(y)
no_sample = 20
score = []
In [ ]:
K = 50
users = full_X.shape[0]
items1 = full_X.shape[1]
items2 = full_Y.shape[1]
param1 = models.hpf(users,items1)
param2 = models.hpf(users,items2)
a = a_c = c = c_c = 0.3
b_c = d_c = 1.0
In [ ]:
kappa_shp = np.random.uniform(low=0.1,size=users)
kappa_rte = np.random.uniform(low=0.1,size=users)
tau_shp = np.random.uniform(low=0.1,size=items1)
tau_rte = np.random.uniform(low=0.1,size=items1)
rho_shp = np.random.uniform(low=0.1,size=items2)
rho_rte = np.random.uniform(low=0.1,size=items2)
phi = np.zeros([users,items1,K])
ohm = np.zeros([users,items2,K])
gam_shp = np.random.uniform(low=0.1,size=[users,K])
gam_rte = np.random.uniform(low=0.1,size=[users,K])
lam_shp = np.random.uniform(low=0.1,size=[items1,K])
lam_rte = np.random.uniform(low=0.1,size=[items1,K])
mu_shp = np.random.uniform(low=0.1,size=[items2,K])
mu_rte = np.random.uniform(low=0.1,size=[items2,K])
In [ ]:
for u in range(0,users):
kappa_shp[u] = a_c + K*a
for i in range(0,items1):
tau_shp[i] = c_c + K*c
for j in range(0,items2):
rho_shp[j] = c_c + K*c
for ite in range(0,tot):
print(ite)
for ui in non_zero_x:
u = ui[0]
i = ui[1]
phi[u,i,:]= sp.digamma(gam_shp[u,:])-np.log(gam_rte[u,:])+sp.digamma(lam_shp[i,:])-np.log(lam_rte[i,:])
norm = logsumexp(phi[u,i,:])
phi[u,i,:] = np.exp(phi[u,i,:]-norm)
for uj in non_zero_y:
u = uj[0]
j = uj[1]
ohm[u,j,:]= sp.digamma(gam_shp[u,:])-np.log(gam_rte[u,:])+sp.digamma(mu_shp[j,:])-np.log(mu_rte[j,:])
norm = logsumexp(ohm[u,j,:])
ohm[u,j,:] = np.exp(ohm[u,j,:]-norm)
for u in range(0,users):
for k in range(0,K):
gam_shp[u,k] = a + np.inner(x[u,:],phi[u,:,k]) + np.inner(y[u,:],ohm[u,:,k])
gam_rte[u,k] = (kappa_shp[u]/kappa_rte[u]) + np.sum(lam_shp[:,k]/lam_rte[:,k]) + np.sum(mu_shp[:,k]/mu_rte[:,k])
kappa_rte[u] = (a_c/b_c) + np.sum(gam_shp[u,:]/gam_rte[u,:])
for i in range(0,items1):
for k in range(0,K):
lam_shp[i,k] = c + np.inner(x[:,i],phi[:,i,k])
lam_rte[i,k] = (tau_shp[i]/tau_rte[i]) + np.sum(gam_shp[:,k]/gam_rte[:,k])
tau_rte[i] = (c_c/d_c) + np.sum(lam_shp[i,:]/lam_rte[i,:])
for j in range(0,items2):
for k in range(0,K):
mu_shp[j,k] = c + np.inner(y[:,j],ohm[:,j,k])
mu_rte[j,k] = (rho_shp[j]/rho_rte[j]) + np.sum(gam_shp[:,k]/gam_rte[:,k])
rho_rte[j] = (c_c/d_c) + np.sum(mu_shp[j,:]/mu_rte[j,:])
if ite%test_every == 0:
q_theta = Gamma(gam_shp,gam_rte)
q_beta1 = Gamma(np.transpose(lam_shp),np.transpose(lam_rte))
q_beta2 = Gamma(np.transpose(mu_shp),np.transpose(mu_rte))
beta1_sample = q_beta1.sample(no_sample).eval()
beta2_sample = q_beta2.sample(no_sample).eval()
theta_sample = q_theta.sample(no_sample).eval()
score.append(helper_func.check(param1,theta_sample,beta1_sample,test_mask1,full_X,metric=metric) \
+helper_func.check(param2,theta_sample,beta2_sample,test_mask2,full_Y,metric=metric))
gc.collect()
In [ ]:
# to_save = [[gam_shp,gam_rte],[lam_shp,lam_rte],[mu_shp,mu_rte]]
# PIK = "../models/bibtex_hpf_"+str(K)+".dat"
# with open(PIK, "wb") as f:
# pickle.dump(to_save, f)
In [ ]:
#check(0)
In [ ]:
plt.plot(score)
plt.show()
# np.savetxt("mae_d_k05.txt",mae_val)