def update_factors(target_name, target_id, sf1_name, sf2_name): z_alpha_prod = [[] for i in range(latent_dim)] z_beta_prod = [[] for i in range(latent_dim)] soft_plus = torch.nn.Softplus() X = [None] * latent_dim X_train_target = X_train_groups[target_name].get_group(target_id) for rind, data_row in X_train_target.iterrows(): sf1_id = data_row[sf1_name] sf2_id = data_row[sf2_name] for i in range(latent_dim): X[i] = torch.stack([ torch.tensor([y_train1.iloc[rind, 0]]), factors[sf1_name][sf1_id][i], factors[sf2_name][sf2_id][i] ], 1) z_alpha_prod[i].append( soft_plus( torch.matmul(X[i], weights[target_name][i]) + weights[target_name][2 * latent_dim + i])) z_beta_prod[i].append( soft_plus( torch.matmul(X[i], weights[target_name][latent_dim + i]) + weights[target_name][3 * latent_dim + i])) target = [None] * latent_dim for i in range(latent_dim): gamma_dist1 = gamma.Gamma(torch.sum(torch.cat(z_alpha_prod[i])), torch.sum(torch.cat(z_beta_prod[i]))) target[i] = gamma_dist1.rsample(torch.Size([1])) #target[i] = torch.tensor([torch.sum(torch.cat(z_alpha_prod[i])).data.numpy() / torch.sum(torch.cat(z_beta_prod[i])).data.numpy()]) return (target_id, torch.stack(target).data)
def sim_linear_data(d, prior, device): # sample random normal noise n_n = torch.empty((len(d), len(prior)), device=device, dtype=torch.float).normal_(mean=0, std=1) # sample random gamma noise n_g = gamma.Gamma(torch.tensor([2.0], device=device), torch.tensor([1 / 2.0], device=device)).sample( sample_shape=(len(d), len(prior))).reshape( len(d), len(prior)) # prepare mask vz = torch.zeros_like(n_n, device=device) # predictions of model 1 + masking y_1 = prior[:, 1] + torch.mul(prior[:, 2], d) + torch.where( prior[:, 0] == 1, n_n + n_g, vz) # predictions of model 2 delta = torch.tensor(1e-4, dtype=torch.float, device=device) d_mask_abs = torch.where(torch.abs(d) > delta, torch.abs(d), delta) y_2 = prior[:, 3] + torch.mul(prior[:, 4], torch.log(d_mask_abs)) + torch.where( prior[:, 0] == 2, n_n + n_g, vz) # predictions of model 3 y_3 = prior[:, 5] + torch.mul(prior[:, 6], torch.sqrt( torch.abs(d))) + torch.where(prior[:, 0] == 3, n_n + n_g, vz) data = y_1.T + y_2.T + y_3.T return data
def sim_linear_torch(d, prior, device): # sample random normal noise n_n = torch.empty((len(d), len(prior)), device=device, dtype=torch.float).normal_(mean=0, std=1) # sample random gamma noise n_g = gamma.Gamma(torch.tensor([2.0], device=device), torch.tensor([1 / 2.0], device=device)).sample( sample_shape=(len(d), len(prior))).reshape( len(d), len(prior)) # perform forward pass y = (prior[:, 0] + torch.mul(prior[:, 1], d) + n_n + n_g).T ygrads = prior[:, 1].reshape(-1, 1) return y, ygrads
def vae_target_multi(target_name, sf1_name, sf2_name, target_id): kl_div = torch.zeros(1) target_pred_train = [] target_data_train = [] z_alpha_prod = [[] for i in range(latent_dim)] z_beta_prod = [[] for i in range(latent_dim)] X = [None] * latent_dim X_train_target = X_train_groups[target_name].get_group(target_id) for rind, data_row in X_train_target.iterrows(): sf1_id = data_row[sf1_name] sf2_id = data_row[sf2_name] target_data_train.append(y_train.loc[rind, 0]) for i in range(latent_dim): X[i] = torch.stack([ torch.tensor([y_train1.iloc[rind, 0]]), factors[sf1_name][sf1_id][i], factors[sf2_name][sf2_id][i] ], 1) z_alpha_prod[i].append(torch.nn.Softplus()( torch.matmul(X[i], weights[target_name][i]) + weights[target_name][2 * latent_dim + i])) z_beta_prod[i].append(torch.nn.Softplus()( torch.matmul(X[i], weights[target_name][latent_dim + i]) + weights[target_name][3 * latent_dim + i])) target = [None] * latent_dim for i in range(latent_dim): gamma_dist1 = gamma.Gamma(torch.sum(torch.cat(z_alpha_prod[i])), torch.sum(torch.cat(z_beta_prod[i]))) target[i] = gamma_dist1.rsample(torch.Size([1])) kl_div = torch.add(kl_div, kl.kl_divergence(gamma_dist1, gamma_dist)) #factors[target_name][target_id] = Variable(torch.stack(target), requires_grad=False) j = 0 for rind, data_row in X_train_target.iterrows(): sf1_id = data_row[sf1_name] sf2_id = data_row[sf2_name] target_pred_train.append( torch.sum( torch.cat([ target[i] * factors[sf1_name][sf1_id][i] * factors[sf2_name][sf2_id][i] for i in range(latent_dim) ]))) j += 1 target_pred_train_final = torch.stack(target_pred_train) return target_pred_train_final, kl_div, target_data_train
def forward(ctx, input, params, device): # sample random normal noise n_n = torch.empty((len(input), len(params)), device=device, dtype=torch.float).normal_(mean=0, std=1) # sample random gamma noise n_g = gamma.Gamma(torch.tensor([2.0], device=device), torch.tensor([1 / 2.0], device=device)).sample( sample_shape=(len(input), len(params))).reshape( len(input), len(params)) # perform forward pass y = (params[:, 0] + torch.mul(params[:, 1], input) + n_n + n_g).T ctx.save_for_backward(input, params) ctx.device = device return y
y_train1 = y.iloc[0:len(y_train)] y_test1 = y.iloc[len(y_train):len(y)].reset_index(drop=True) user_ids = X_train['user'].unique() print(max(user_ids), len(user_ids)) times_ids = X_train['time'].unique() print(max(times_ids), len(times_ids)) asin_ids = X_train['asin'].unique() print(max(asin_ids), len(asin_ids)) factors = dict() factors['user'] = dict() factors['time'] = dict() factors['asin'] = dict() gamma_dist = gamma.Gamma(torch.tensor([alpha]), torch.tensor([beta])) for user_id in user_ids: factors['user'][user_id] = gamma_dist.sample(torch.Size([latent_dim])) for time_id in times_ids: factors['time'][time_id] = gamma_dist.sample(torch.Size([latent_dim])) for asin_id in asin_ids: factors['asin'][asin_id] = gamma_dist.sample(torch.Size([latent_dim])) weights = {} weights['user'] = [] weights['time'] = [] weights['asin'] = [] normal_dist = Normal(torch.Tensor([30]), torch.Tensor([10])) normal_dist1 = Normal(torch.Tensor([50]), torch.Tensor([10]))