<jupyter_start><jupyter_text>TitleFréchet distance (numpy) Description$$FD=\left|\mu-\mu_w\right|^2+tr\left(\Sigma+\Sigma_w-2\left(\Sigma\Sigma_w\right)^{\frac{1}{2}}\right)$$ Hintsnumpy covnumpy trace<jupyter_code># calculate frechet inception distance with numpy
import numpy as np
from scipy.linalg import sqrtm
# calculate frechet inception distance
def calculate_fd(act1, act2):
	# calculate mean and covariance statistics
	# The “mu_1” and “mu_2” refer to the feature-wise mean of the real 
	# and generated images, e.g. 2,048 element vectors where each 
	# element is the mean feature observed across the images.
	mu1, mu2 = ___
	
	# The sigma1 and sigma2 are the covariance matrix for the real 
	# and generated feature vectors. 
	# Remember each row is a sample/image.
	sigma1, sigma2 = ___
	
	# calculate sum squared difference between means
	ssdiff = np.sum((mu1 - mu2)**2.0)
	# calculate sqrt of product between cov
	covmean = sqrtm(sigma1.dot(sigma2))
	# check and correct imaginary numbers from sqrt
	if np.iscomplexobj(covmean):
		covmean = covmean.real

	# calculate score
	fd = ___

	return fd
### edTest(test_check) ###
# define two collections of activations
np.random.seed(109)
# We assume 10 images
# The output layer of the Inception model is removed and the output 
# is taken as the activations from the last pooling layer, hence 2048.
act1 = np.random.random(10*2048)
act1 = act1.reshape((10,2048))
np.random.seed(295)
act2 = np.random.random(10*2048)
act2 = act2.reshape((10,2048))
# fd between act1 and act1
fd = calculate_fd(act1, act1)
print('FD (same): %.3f' % fd)
# fd between act1 and act2
fd = calculate_fd(act1, act2)
print('FD (different): %.3f' % fd)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10722610722610723, 'fraction_numerical': 0.04428904428904429, 'mean_word_length': 4.723333333333334, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '405212', 'license': 'MIT', 'n_tokens_mistral': 619, 'n_tokens_neox': 566, 'n_words': 189, 'path': 'content/lectures/lecture30/notebook/fid.ipynb', 'repo_name': 'DavidAssaraf106/2021-CS109B', 'size': '1716'}