Created
February 21, 2021 03:52
-
-
Save KeremTurgutlu/4d8e1e2812a7c16fa329bbd732e764d3 to your computer and use it in GitHub Desktop.
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
import os | |
import torch | |
import torch.distributed as dist | |
from torch.multiprocessing import Process | |
from torchvision import datasets, transforms | |
import numpy as np | |
import torch.nn as nn | |
import torch.nn.functional as F | |
import random | |
def set_seed(s, reproducible=False): | |
"Set random seed for `random`, `torch`, and `numpy` (where available)" | |
try: torch.manual_seed(s) | |
except NameError: pass | |
try: torch.cuda.manual_seed_all(s) | |
except NameError: pass | |
try: np.random.seed(s%(2**32-1)) | |
except NameError: pass | |
random.seed(s) | |
if reproducible: | |
torch.backends.cudnn.deterministic = True | |
torch.backends.cudnn.benchmark = False | |
set_seed(42) | |
image_inputs = torch.randn(8, 4) | |
set_seed(42) | |
text_inputs = torch.randn(8, 4) | |
set_seed(42) | |
image_encoder = nn.Linear(4, 2, bias=False) | |
set_seed(42) | |
text_encoder = nn.Linear(4, 2, bias=False) | |
# print(image_inputs) | |
# print(text_inputs) | |
# print(image_encoder.weight) | |
# print(text_encoder.weight) | |
image_embeddings = image_encoder(image_inputs) | |
text_embeddings = text_encoder(text_inputs) | |
image_embeddings = F.normalize(image_embeddings) | |
text_embeddings = F.normalize(text_embeddings) | |
image_embeddings.shape, text_embeddings.shape | |
cosine_sim = image_embeddings @ text_embeddings.T | |
loss = F.cross_entropy(cosine_sim, torch.arange(len(cosine_sim)), reduction="none") | |
print(loss) | |
loss = loss.mean() | |
print(loss) | |
loss.backward() | |
print(image_encoder.weight.grad, text_encoder.weight.grad) | |
# print(image_encoder.weight) | |
# print(text_encoder.weight) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Following code results in: