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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. new_task_embed = value
  47. return C, new_task_embed
  48. class Trainer(torch.nn.Module):
  49. def __init__(self, config_param, head=None):
  50. super(Trainer, self).__init__()
  51. fc1_in_dim = config_param['embedding_dim'] * 8
  52. fc2_in_dim = config_param['first_fc_hidden_dim']
  53. fc2_out_dim = config_param['second_fc_hidden_dim']
  54. self.fc1 = torch.nn.Linear(fc1_in_dim, fc2_in_dim)
  55. self.fc2 = torch.nn.Linear(fc2_in_dim, fc2_out_dim)
  56. self.linear_out = torch.nn.Linear(fc2_out_dim, 1)
  57. # cluster module
  58. self.cluster_module = ClustringModule(config_param)
  59. # self.task_dim = fc1_in_dim
  60. self.task_dim = config_param['cluster_final_dim']
  61. # transform task to weights
  62. self.film_layer_1_beta = nn.Linear(self.task_dim, fc2_in_dim, bias=False)
  63. self.film_layer_1_gamma = nn.Linear(self.task_dim, fc2_in_dim, bias=False)
  64. self.film_layer_2_beta = nn.Linear(self.task_dim, fc2_out_dim, bias=False)
  65. self.film_layer_2_gamma = nn.Linear(self.task_dim, fc2_out_dim, bias=False)
  66. # self.film_layer_3_beta = nn.Linear(self.task_dim, self.h3_dim, bias=False)
  67. # self.film_layer_3_gamma = nn.Linear(self.task_dim, self.h3_dim, bias=False)
  68. # self.dropout_rate = 0
  69. self.dropout_rate = config_param['trainer_dropout_rate']
  70. self.dropout = nn.Dropout(self.dropout_rate)
  71. def aggregate(self, z_i):
  72. return torch.mean(z_i, dim=0)
  73. def forward(self, task_embed, y, training, adaptation_data=None, adaptation_labels=None):
  74. if training:
  75. C, clustered_task_embed = self.cluster_module(task_embed, y)
  76. # hidden layers
  77. # todo : adding activation function or remove it
  78. hidden_1 = self.fc1(task_embed)
  79. beta_1 = torch.tanh(self.film_layer_1_beta(clustered_task_embed))
  80. gamma_1 = torch.tanh(self.film_layer_1_gamma(clustered_task_embed))
  81. hidden_1 = torch.mul(hidden_1, gamma_1) + beta_1
  82. hidden_1 = self.dropout(hidden_1)
  83. hidden_2 = F.relu(hidden_1)
  84. hidden_2 = self.fc2(hidden_2)
  85. beta_2 = torch.tanh(self.film_layer_2_beta(clustered_task_embed))
  86. gamma_2 = torch.tanh(self.film_layer_2_gamma(clustered_task_embed))
  87. hidden_2 = torch.mul(hidden_2, gamma_2) + beta_2
  88. hidden_2 = self.dropout(hidden_2)
  89. hidden_3 = F.relu(hidden_2)
  90. y_pred = self.linear_out(hidden_3)
  91. else:
  92. C, clustered_task_embed = self.cluster_module(adaptation_data, adaptation_labels)
  93. beta_1 = torch.tanh(self.film_layer_1_beta(clustered_task_embed))
  94. gamma_1 = torch.tanh(self.film_layer_1_gamma(clustered_task_embed))
  95. beta_2 = torch.tanh(self.film_layer_2_beta(clustered_task_embed))
  96. gamma_2 = torch.tanh(self.film_layer_2_gamma(clustered_task_embed))
  97. hidden_1 = self.fc1(task_embed)
  98. hidden_1 = torch.mul(hidden_1, gamma_1) + beta_1
  99. hidden_1 = self.dropout(hidden_1)
  100. hidden_2 = F.relu(hidden_1)
  101. hidden_2 = self.fc2(hidden_2)
  102. hidden_2 = torch.mul(hidden_2, gamma_2) + beta_2
  103. hidden_2 = self.dropout(hidden_2)
  104. hidden_3 = F.relu(hidden_2)
  105. y_pred = self.linear_out(hidden_3)
  106. return y_pred, C