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

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