-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96dd4e9
commit 5577049
Showing
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import torch | ||
from semilearn.algorithms.SemiReward import Rewarder,Generator,cosine_similarity_n | ||
import numpy as np | ||
|
||
class SemiReward_infer: | ||
def __init__(self, rewarder_model, starttiming): | ||
self.rewarder = rewarder_model | ||
self.starttiming = starttiming | ||
|
||
def __call__(self, feats_x_ulb_w, pseudo_label, it): | ||
pseudo_label_list = [] | ||
if it >= self.starttiming: | ||
reward_list = [] | ||
|
||
|
||
for _ in range(256): | ||
reward = self.rewarder(feats_x_ulb_w, pseudo_label) | ||
pseudo_label_list.append(pseudo_label) | ||
reward_list.append(reward.item()) | ||
|
||
# Calculate the average reward | ||
average_reward = sum(reward_list) / len(reward_list) | ||
|
||
# Filter out pseudo_labels with rewards below the average | ||
filtered_pseudo_labels = [] | ||
for i, reward in enumerate(reward_list): | ||
if reward >= average_reward: | ||
filtered_pseudo_labels.append(pseudo_label_list[i]) | ||
|
||
return filtered_pseudo_labels | ||
else: | ||
for _ in range(256): | ||
pseudo_label_list.append(pseudo_label) | ||
return pseudo_label_list | ||
|
||
class SemiReward_train: | ||
def __init__(self, rewarder_model, generator_model, criterion, starttiming,gpu): | ||
self.rewarder = rewarder_model | ||
self.generator = generator_model | ||
self.criterion = criterion | ||
self.starttiming = starttiming | ||
self.gpu=gpu | ||
def __call__(self, feats_x_ulb_w, pseudo_label, y_lb, it): | ||
generated_label = self.generator(feats_x_ulb_w).detach() | ||
|
||
real_labels_tensor = y_lb.cuda(self.gpu).view(-1) | ||
real_labels_tensor=real_labels_tensor.unsqueeze(0) | ||
if it >= self.starttiming: | ||
accumulated_pseudo_labels = [] | ||
reward_list = [] | ||
|
||
for _ in range(80): | ||
reward = self.rewarder(feats_x_ulb_w, pseudo_label) | ||
accumulated_pseudo_labels.append(pseudo_label.squeeze().cpu().numpy()) | ||
reward_list.append(reward.item()) | ||
|
||
sorted_indices = np.argsort(reward_list)[-8:] | ||
filtered_pseudo_labels = [accumulated_pseudo_labels[i] for i in sorted_indices] | ||
reward = [reward_list[i] for i in sorted_indices] | ||
|
||
filtered_pseudo_labels_tensor = torch.tensor(filtered_pseudo_labels, dtype=torch.float32).cuda(self.gpu) | ||
reward = torch.tensor(reward, dtype=torch.float32, requires_grad=True).cuda(self.gpu) | ||
|
||
|
||
cosine_similarity_score = cosine_similarity_n(generated_label, filtered_pseudo_labels_tensor) | ||
|
||
else: | ||
cosine_similarity_score = cosine_similarity_n(generated_label, real_labels_tensor) | ||
|
||
# Convert generated pseudo labels and true labels to tensors | ||
generated_label = generated_label.view(-1) | ||
reward = self.rewarder(feats_x_ulb_w,generated_label) | ||
reward=reward.view(1) | ||
|
||
generator_loss = self.criterion(reward, torch.ones_like(reward).cuda(self.gpu)) | ||
rewarder_loss = self.criterion(reward, cosine_similarity_score) | ||
|
||
return generator_loss, rewarder_loss |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
from .main import Rewarder,Generator,cosine_similarity_n | ||
from .SemiRewardH import SemiReward_infer,SemiReward_train |
Binary file added
BIN
+332 Bytes
semilearn/algorithms/SemiReward/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
|
||
class Generator(nn.Module): | ||
def __init__(self, feature_dim): | ||
super(Generator, self).__init__() | ||
self.fc_layers = nn.Sequential( | ||
nn.Linear(feature_dim, 256), | ||
nn.ReLU(), | ||
nn.Linear(256, 128), | ||
nn.ReLU(), | ||
nn.Linear(128, 64), | ||
nn.ReLU(), | ||
nn.Linear(64, 1) | ||
) | ||
|
||
def forward(self, x): | ||
x = self.fc_layers(x) | ||
return x | ||
|
||
class Rewarder(nn.Module): | ||
def __init__(self, label_embedding_dim, feature_dim): | ||
super(Rewarder, self).__init__() | ||
|
||
# Feature processing section | ||
self.feature_fc = nn.Linear(feature_dim, 128) | ||
self.feature_norm = nn.LayerNorm(128) | ||
|
||
# Label embedding section | ||
self.label_embedding = nn.Embedding(100, label_embedding_dim) | ||
self.label_norm = nn.LayerNorm(label_embedding_dim) | ||
|
||
# Cross-Attention mechanism | ||
self.cross_attention_fc = nn.Linear(128, 1) | ||
|
||
# MLP (Multi-Layer Perceptron) | ||
self.mlp_fc1 = nn.Linear(128, 256) | ||
self.mlp_fc2 = nn.Linear(256, 128) | ||
|
||
# Feed-Forward Network (FFN) | ||
self.ffn_fc1 = nn.Linear(128, 64) | ||
self.ffn_fc2 = nn.Linear(64, 1) | ||
|
||
def forward(self, features, label_indices): | ||
# Process features | ||
features = self.feature_fc(features) | ||
features = self.feature_norm(features) | ||
|
||
# Process labels | ||
label_embed = self.label_embedding(label_indices.to(torch.int64)) | ||
label_embed = self.label_norm(label_embed) | ||
|
||
# Cross-Attention mechanism | ||
cross_attention_input = torch.cat((features.unsqueeze(0), label_embed.unsqueeze(0)), dim=0) | ||
cross_attention_weights = torch.softmax(self.cross_attention_fc(cross_attention_input), dim=0) | ||
cross_attention_output = (cross_attention_weights * cross_attention_input).sum(dim=0) | ||
|
||
# MLP section | ||
mlp_input = torch.cat((cross_attention_output, label_embed), dim=0) | ||
mlp_output = F.relu(self.mlp_fc1(mlp_input)) | ||
mlp_output = self.mlp_fc2(mlp_output) | ||
|
||
# FFN section | ||
ffn_output = F.relu(self.ffn_fc1(mlp_output)) | ||
reward = torch.sigmoid(self.ffn_fc2(ffn_output)) | ||
reward = torch.mean(reward) | ||
|
||
return reward | ||
|
||
def cosine_similarity_n(x, y): | ||
# Calculate cosine similarity | ||
cosine_similarity = torch.cosine_similarity(x, y) | ||
|
||
# Normalize cosine similarity to the range of 0 to 1 | ||
normalized_similarity = 0.5 * (cosine_similarity + 1) | ||
|
||
return normalized_similarity |