Created
May 31, 2017 12:36
-
-
Save ajbrock/5b92a484ad3bf25654f12cf81baa88b9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Inception Score Calculator | |
# | |
# A Brock, 2017 | |
# | |
# This snippet assumes you have two functions defined: | |
# 1. sample_net, which takes in a batch x num_latents random vector and returns batch samples, | |
# 2. eval_net, which takes in batch samples and returns a batch x #classes prediction vector. | |
num_latents = 100 | |
# Get predicted class given z, p(c|z), for 50,000 samples | |
p_C_Z = [] | |
for i in range(1000): | |
p_C_Z.append(eval_net(sample_net(np.float32(np.random.randn(50,num_latents))))) # Append score to p_C_Z | |
p_C_Z = np.asarray(p_C_Z) | |
# Now take ten chunks of p(c|z) and get their scores | |
score = [] | |
for i in range(10): | |
p = p_C_Z[(i * p_C_Z.shape[0] // 10):((i + 1) * p_C_Z.shape[0] // 10), :] | |
kl = p * (np.log(p) - np.log(np.expand_dims(np.mean(p, 0), 0))) | |
kl = np.mean(np.mean(kl, 1)) | |
score.append(np.exp(kl)) | |
# Calculate mean and std across ten chunks, and report | |
incept = np.mean(np.asarray(score)) | |
incept_std = np.std(np.asarray(score)) | |
print('Inception score is ' +str(incept)+'+/-'+str(incept_std)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment