generated from patel-zeel/pip-template
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mean_std Aquisition function #8
Open
VannshJani
wants to merge
8
commits into
sustainability-lab:main
Choose a base branch
from
VannshJani:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7a4e7d5
Create Mean_std.py
VannshJani e41a40a
Update Mean_std.py
VannshJani 29c075b
fixed errors
VannshJani 0ca261d
changed class name
VannshJani 07d0169
inbuilt function for std
VannshJani 7e381f9
added test file
VannshJani 97137f5
Update test_mean_std.py
VannshJani 098ed9d
Updated spelling errors
VannshJani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,18 @@ | ||
import torch | ||
from astra.torch.al.acquisitions.base import EnsembleAcquisition | ||
from astra.torch.al.acquisitions.base import MCAcquisition | ||
# Ensemble and MC strategy for producing different model parameters | ||
|
||
|
||
# maximum mean standard deviation aquisition function | ||
class MeanStd(EnsembleAcquisition,MCAcquisition): | ||
def acquire_scores(self, logits: torch.Tensor) -> torch.Tensor: | ||
# Mean-STD acquisition function | ||
# (n_nets/n_mc_samples, pool_dim, n_classes) logits shape | ||
assert len(logits.shape) == 3, "logits shape must be 3-Dimensional" | ||
pool_num = logits.shape[1] | ||
softmax_activation = torch.nn.Softmax(dim=2) | ||
prob = softmax_activation(logits) | ||
std = torch.std(prob,dim=0,unbiased=False) | ||
scores = torch.mean(std, dim=1) # mean over classes, shape (pool_dim) | ||
return scores |
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,43 @@ | ||
import pytest | ||
|
||
import torch | ||
from torchvision.datasets import CIFAR10 | ||
|
||
from astra.torch.models import CNNClassifier | ||
from astra.torch.al import MeanStd,EnsembleStrategy, MCStrategy | ||
# from astra.torch.al.errors import AcquisitionMismatchError | ||
# from astra.tests.torch.aquisitions.test_common import test_acquisition_mismatch | ||
|
||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
|
||
|
||
def test_random(): | ||
data = CIFAR10(root="data", download=True, train=False) # "test" for less data | ||
inputs = torch.tensor(data.data).float().to(device) | ||
outputs = torch.tensor(data.targets).long().to(device) | ||
|
||
# Meta parameters | ||
n_pool = 1000 | ||
indices = torch.randperm(len(inputs)) | ||
pool_indices = indices[:n_pool] | ||
train_indices = indices[n_pool:] | ||
n_query_samples = 10 | ||
|
||
# Define the acquisition function | ||
acquisition = MeanStd() | ||
strategy1 = EnsembleStrategy(acquisition, inputs, outputs) | ||
strategy2 = MCStrategy(acquisition, inputs, outputs) | ||
|
||
# Put the strategy on the device | ||
strategy1.to(device) | ||
strategy2.to(device) | ||
|
||
# Define the model | ||
net = CNNClassifier(32, 3, 3, [4, 8], [2, 3], 10).to(device) | ||
|
||
# Query the strategy | ||
best_indices1 = strategy1.query(net, pool_indices, n_query_samples=n_query_samples) | ||
best_indices2 = strategy2.query(net, pool_indices, n_query_samples=n_query_samples) | ||
|
||
assert best_indices1["MeanStd"].shape == (n_query_samples,),"EnsembleStrategy failed" | ||
assert best_indices2["MeanStd"].shape == (n_query_samples,),"MCStrategy failed" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This lines goes first