extend Melu code to perform different meta algorithms and hyperparameters
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

clustering.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import torch.nn.init as init
  2. import os
  3. import torch
  4. import pickle
  5. from options import config
  6. import gc
  7. import torch.nn as nn
  8. from torch.nn import functional as F
  9. class ClustringModule(torch.nn.Module):
  10. def __init__(self, config_param):
  11. super(ClustringModule, self).__init__()
  12. self.h1_dim = config_param['cluster_h1_dim']
  13. self.h2_dim = config_param['cluster_h2_dim']
  14. self.final_dim = config_param['cluster_final_dim']
  15. self.dropout_rate = config_param['cluster_dropout_rate']
  16. layers = [nn.Linear(config_param['embedding_dim'] * 8 + 1, self.h1_dim),
  17. torch.nn.Dropout(self.dropout_rate),
  18. nn.ReLU(inplace=True),
  19. # nn.BatchNorm1d(self.h1_dim),
  20. nn.Linear(self.h1_dim, self.h2_dim),
  21. torch.nn.Dropout(self.dropout_rate),
  22. nn.ReLU(inplace=True),
  23. # nn.BatchNorm1d(self.h2_dim),
  24. nn.Linear(self.h2_dim, self.final_dim)]
  25. self.input_to_hidden = nn.Sequential(*layers)
  26. self.clusters_k = config_param['cluster_k']
  27. self.embed_size = self.final_dim
  28. self.array = nn.Parameter(init.xavier_uniform_(torch.FloatTensor(self.clusters_k, self.embed_size)))
  29. self.temperature = config_param['temperature']
  30. def aggregate(self, z_i):
  31. return torch.mean(z_i, dim=0)
  32. def forward(self, task_embed, y, training=True):
  33. y = y.view(-1, 1)
  34. input_pairs = torch.cat((task_embed, y), dim=1)
  35. task_embed = self.input_to_hidden(input_pairs)
  36. # todo : may be useless
  37. mean_task = self.aggregate(task_embed)
  38. res = torch.norm(mean_task - self.array, p=2, dim=1, keepdim=True)
  39. res = torch.pow((res / self.temperature) + 1, (self.temperature + 1) / -2)
  40. # 1*k
  41. C = torch.transpose(res / res.sum(), 0, 1)
  42. # 1*k, k*d, 1*d
  43. value = torch.mm(C, self.array)
  44. # simple add operation
  45. new_task_embed = value + mean_task
  46. return C, new_task_embed
  47. class Trainer(torch.nn.Module):
  48. def __init__(self, config_param, head=None):
  49. super(Trainer, self).__init__()
  50. fc1_in_dim = config_param['embedding_dim'] * 8
  51. fc2_in_dim = config_param['first_fc_hidden_dim']
  52. fc2_out_dim = config_param['second_fc_hidden_dim']
  53. self.fc1 = torch.nn.Linear(fc1_in_dim, fc2_in_dim)
  54. self.fc2 = torch.nn.Linear(fc2_in_dim, fc2_out_dim)
  55. self.linear_out = torch.nn.Linear(fc2_out_dim, 1)
  56. # cluster module
  57. self.cluster_module = ClustringModule(config_param)
  58. # self.task_dim = fc1_in_dim
  59. self.task_dim = config_param['cluster_final_dim']
  60. # transform task to weights
  61. self.film_layer_1_beta = nn.Linear(self.task_dim, fc2_in_dim, bias=False)
  62. self.film_layer_1_gamma = nn.Linear(self.task_dim, fc2_in_dim, bias=False)
  63. self.film_layer_2_beta = nn.Linear(self.task_dim, fc2_out_dim, bias=False)
  64. self.film_layer_2_gamma = nn.Linear(self.task_dim, fc2_out_dim, bias=False)
  65. # self.film_layer_3_beta = nn.Linear(self.task_dim, self.h3_dim, bias=False)
  66. # self.film_layer_3_gamma = nn.Linear(self.task_dim, self.h3_dim, bias=False)
  67. # self.dropout_rate = 0
  68. self.dropout_rate = config_param['trainer_dropout_rate']
  69. self.dropout = nn.Dropout(self.dropout_rate)
  70. def aggregate(self, z_i):
  71. return torch.mean(z_i, dim=0)
  72. def forward(self, task_embed, y, training, adaptation_data=None, adaptation_labels=None):
  73. if training:
  74. C, clustered_task_embed = self.cluster_module(task_embed, y)
  75. # hidden layers
  76. # todo : adding activation function or remove it
  77. hidden_1 = self.fc1(task_embed)
  78. beta_1 = torch.tanh(self.film_layer_1_beta(clustered_task_embed))
  79. gamma_1 = torch.tanh(self.film_layer_1_gamma(clustered_task_embed))
  80. hidden_1 = torch.mul(hidden_1, gamma_1) + beta_1
  81. hidden_1 = self.dropout(hidden_1)
  82. hidden_2 = F.relu(hidden_1)
  83. hidden_2 = self.fc2(hidden_2)
  84. beta_2 = torch.tanh(self.film_layer_2_beta(clustered_task_embed))
  85. gamma_2 = torch.tanh(self.film_layer_2_gamma(clustered_task_embed))
  86. hidden_2 = torch.mul(hidden_2, gamma_2) + beta_2
  87. hidden_2 = self.dropout(hidden_2)
  88. hidden_3 = F.relu(hidden_2)
  89. y_pred = self.linear_out(hidden_3)
  90. else:
  91. C, clustered_task_embed = self.cluster_module(adaptation_data, adaptation_labels)
  92. beta_1 = torch.tanh(self.film_layer_1_beta(clustered_task_embed))
  93. gamma_1 = torch.tanh(self.film_layer_1_gamma(clustered_task_embed))
  94. beta_2 = torch.tanh(self.film_layer_2_beta(clustered_task_embed))
  95. gamma_2 = torch.tanh(self.film_layer_2_gamma(clustered_task_embed))
  96. hidden_1 = self.fc1(task_embed)
  97. hidden_1 = torch.mul(hidden_1, gamma_1) + beta_1
  98. hidden_1 = self.dropout(hidden_1)
  99. hidden_2 = F.relu(hidden_1)
  100. hidden_2 = self.fc2(hidden_2)
  101. hidden_2 = torch.mul(hidden_2, gamma_2) + beta_2
  102. hidden_2 = self.dropout(hidden_2)
  103. hidden_3 = F.relu(hidden_2)
  104. y_pred = self.linear_out(hidden_3)
  105. return y_pred, C