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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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):
  11. super(ClustringModule, self).__init__()
  12. self.h1_dim = 64
  13. self.h2_dim = 32
  14. # self.final_dim = fc1_in_dim
  15. self.final_dim = 32
  16. self.dropout_rate = 0
  17. layers = [nn.Linear(config['embedding_dim'] * 8 + 1, self.h1_dim),
  18. torch.nn.Dropout(self.dropout_rate),
  19. nn.ReLU(inplace=True),
  20. nn.Linear(self.h1_dim, self.h2_dim),
  21. torch.nn.Dropout(self.dropout_rate),
  22. nn.ReLU(inplace=True),
  23. nn.Linear(self.h2_dim, self.final_dim)]
  24. self.input_to_hidden = nn.Sequential(*layers)
  25. self.clusters_k = 7
  26. self.embed_size = self.final_dim
  27. self.array = nn.Parameter(init.xavier_uniform_(torch.FloatTensor(self.clusters_k, self.embed_size)))
  28. self.temperature = 10.0
  29. def aggregate(self, z_i):
  30. return torch.mean(z_i, dim=0)
  31. def forward(self, task_embed, y, training=True):
  32. y = y.view(-1, 1)
  33. input_pairs = torch.cat((task_embed, y), dim=1)
  34. task_embed = self.input_to_hidden(input_pairs)
  35. # todo : may be useless
  36. mean_task = self.aggregate(task_embed)
  37. # C_distribution, new_task_embed = self.memoryunit(mean_task)
  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. # calculate target distribution
  47. return C, new_task_embed
  48. class Trainer(torch.nn.Module):
  49. def __init__(self, config, head=None):
  50. super(Trainer, self).__init__()
  51. fc1_in_dim = config['embedding_dim'] * 8
  52. fc2_in_dim = config['first_fc_hidden_dim']
  53. fc2_out_dim = config['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)
  59. # self.task_dim = fc1_in_dim
  60. self.task_dim = 32
  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.1
  69. self.dropout = nn.Dropout(self.dropout_rate)
  70. self.gamma_1, self.beta_1, self.gamma_2, self.beta_2 = None, None, None, None
  71. def aggregate(self, z_i):
  72. return torch.mean(z_i, dim=0)
  73. def forward(self, task_embed, y, training):
  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. self.gamma_1, self.beta_1, self.gamma_2, self.beta_2 = gamma_1, beta_1, gamma_2, beta_2
  92. else:
  93. hidden_1 = self.fc1(task_embed)
  94. hidden_1 = torch.mul(hidden_1, self.gamma_1) + self.beta_1
  95. hidden_1 = self.dropout(hidden_1)
  96. hidden_2 = F.relu(hidden_1)
  97. hidden_2 = self.fc2(hidden_2)
  98. hidden_2 = torch.mul(hidden_2, self.gamma_2) + self.beta_2
  99. hidden_2 = self.dropout(hidden_2)
  100. hidden_3 = F.relu(hidden_2)
  101. y_pred = self.linear_out(hidden_3)
  102. return y_pred