text_html_output_1.png
from neuromatch_ssl_tutorial.modules import data, load, models, plot_util

from neuromatch_ssl_tutorial.modules import data, load, models, plot_util

import matplotlib.pyplot as plt

import numpy as np

import random

import torch

import torch

# @title Plotting functions

# @markdown Function to plot a histogram of RSM values: `plot_rsm_histogram(rsms, colors)`
def plot_rsm_histogram(rsms, colors, labels=None, nbins=100):
  fig, ax = plt.subplots(1)
  ax.set_title("Histogram of RSM values", y=1.05)

  min_val = np.min([np.nanmin(rsm) for rsm in rsms])
  max_val = np.max([np.nanmax(rsm) for rsm in rsms])

  bins = np.linspace(min_val, max_val, nbins+1)

  if labels is None:
    labels = [labels] * len(rsms)
  elif len(labels) != len(rsms):
    raise ValueError("If providing labels, must provide as many as RSMs.")

  if len(rsms) != len(colors):
    raise ValueError("Must provide as may colors as RSMs.")

  for r, rsm in enumerate(rsms):
    ax.hist(
        rsm.reshape(-1), bins, density=True, alpha=0.4,
        color=colors[r], label=labels[r]
        )
  ax.axvline(x=0, ls="dashed", alpha=0.6, color="k")
  ax.set_ylabel("Density")
  ax.set_xlabel("Similarity values")
  ax.legend()
  plt.show()

from IPython.display import display, Image
def test_custom_torch_RSM_fct(custom_torch_RSM_fct):
    rand_feats = torch.rand(100, 1000)
    RSM_custom = custom_torch_RSM_fct(rand_feats)
    RSM_ground_truth = data.calculate_torch_RSM(rand_feats)
def test_custom_contrastive_loss_fct(custom_simclr_contrastive_loss):
    rand_proj_feat1 = torch.rand(100, 1000)
    rand_proj_feat2 = torch.rand(100, 1000)
    loss_custom = custom_simclr_contrastive_loss(rand_proj_feat1, rand_proj_feat2)
    loss_ground_truth = models.contrastive_loss(rand_proj_feat1, rand_proj_feat2)

import random
import torch
def set_seed(seed=None, seed_torch=True):
    if seed is None:
        seed = np.random.choice(2 ** 32)
    random.seed(seed)
    np.random.seed(seed)
    if seed_torch:
        torch.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)
        torch.cuda.manual_seed(seed)
        torch.backends.cudnn.benchmark = False
        torch.backends.cudnn.deterministic = True
def seed_worker(worker_id):
    worker_seed = torch.initial_seed() % 2 ** 32
    np.random.seed(worker_seed)
    random.seed(worker_seed)

def set_device():
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    return device

SEED = 2021
set_seed(seed=SEED)
DEVICE = set_device()