Skip to content

Instantly share code, notes, and snippets.

@tail-call
Created June 26, 2025 08:20
Show Gist options
  • Select an option

  • Save tail-call/4a6a5c0eb13204260b090de48895da44 to your computer and use it in GitHub Desktop.

Select an option

Save tail-call/4a6a5c0eb13204260b090de48895da44 to your computer and use it in GitHub Desktop.
A bunch of files I deleted from CGT4NN repository, archiving here just in case
from cgtnnlib.LearningTask import LearningTask
from cgtnnlib.nn.AugmentedReLUNetwork import AugmentedReLUNetwork
from cgtnnlib.training import train_model
from cgtnnlib.datasets import datasets
import torch.optim as optim
p = 0.5
dataset = datasets[0]
model = AugmentedReLUNetwork(
inputs_count=dataset.features_count, outputs_count=dataset.classes_count, p=p
)
train_model(
model=model,
dataset=dataset,
epochs=1,
iteration=1,
p=p,
criterion=dataset.learning_task.criterion,
optimizer=optim.Adam(
model.parameters(),
lr=0.05,
),
)
from typing import Callable
import torch
import torch.nn as nn
from cgtnnlib.NoiseGenerator import stable_noise
from cgtnnlib.training import add_noise_to_labels_regression
import cgtnnlib.datasets as ds
def do(labels: torch.Tensor, generate_sample: Callable[[], float]):
"""
Applies noise to labels and returns a Bernoulli sample.
This method adds regression noise to the input labels using
`add_noise_to_labels_regression`, normalizes the noisy labels to be between 0 and 1,
and then samples from a Bernoulli distribution with probabilities derived from these normalized values. Prints intermediate tensors for debugging.
Args:
labels: The original label tensor.
generate_sample: A callable that generates a sample value (used by add_noise_to_labels_regression).
Returns:
torch.Tensor: A Bernoulli sample generated from the noisy labels.
"""
t = add_noise_to_labels_regression(
labels,
generate_sample,
)
print("t =", t)
n = t.sub(t.min()).div(t.max() - t.min())
# Alternatively: Use argmax if you're going to use more classes later
# noisy_targets = torch.argmax(torch.stack([1 - noisy_probabilities, noisy_probabilities], dim=1), dim=1)
# noisy_targets = noisy_targets.long()
print(f"Noisy probabilities: {n}")
print(f"Original targets: {labels}")
return torch.distributions.Bernoulli(probs=n).sample()
if __name__ == "__main__":
ng = stable_noise(
dataset=ds.datasets[0],
factor=0.03,
alpha=1,
beta=1,
)
for i in range(0, 100):
baps = do(
torch.tensor([0.0, 0.0, 0.0, 1.0, 1.0, 1, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0]),
generate_sample=ng.next_sample,
)
print(f"baps: {baps}")
import torch
from typing import Callable
from cgtnnlib.NoiseGenerator import stable_noise
import cgtnnlib.datasets as ds
def add_noise_to_labels_classification(
labels: torch.Tensor, num_classes: int, generate_sample: Callable[[], float]
) -> torch.Tensor:
"""
Добавляет шум к меткам классов для многоклассовой классификации.
Args:
labels: Tensor с метками классов (например, [1, 2, 0, 2, 3, 0]).
num_classes: Количество классов в задаче классификации.
generate_sample: Функция, которая генерирует случайное число, определяющее
величину шума для каждой метки. Эта функция должна возвращать float.
Returns:
Tensor с измененными метками классов.
"""
noisy_labels = labels.clone()
for i in range(labels.numel()):
noise = generate_sample()
probs = torch.ones(num_classes) * noise / (num_classes - 1)
probs[labels[i]] = 1 - noise
probs = probs / probs.sum()
noisy_labels[i] = torch.multinomial(probs, 1)[0]
return noisy_labels
ng = stable_noise(
dataset=ds.datasets[0],
factor=0.03,
alpha=1,
beta=1,
)
for i in range(0, 100):
print(
add_noise_to_labels_classification(
torch.tensor([1, 2, 0, 2, 3, 0]), 4, ng.next_sample
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment