Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

analyse_chains excl warm-up.py 3.0 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  1. import sys
  2. sys.path.append('src')
  3. from pickle_wrapper import unpickle, pickle_it
  4. from mcmc_norm_learning.mcmc_convergence import prepare_sequences
  5. from mcmc_norm_learning.algorithm_1_v4 import to_tuple
  6. from collections import defaultdict
  7. import itertools
  8. import operator
  9. import yaml
  10. import math
  11. with open("params.yaml", 'r') as fd:
  12. params = yaml.safe_load(fd)
  13. m = params['m']
  14. num_chains = math.ceil(m/2)
  15. chains_and_log_posteriors = unpickle('data/chains_and_log_posteriors.pickle')[:num_chains]
  16. with open('metrics/chain_info_no_warmup.txt', 'w') as chain_info:
  17. chain_info.write(f'Number of chains: {len(chains_and_log_posteriors)}\n')
  18. chain_length = len(chains_and_log_posteriors[0]["chain"])
  19. print(f'Chain length: {chain_length}')
  20. chain_info.write(f'Length of each chain: {chain_length}\n')
  21. exps_in_chains = [None]*len(chains_and_log_posteriors)
  22. for i,chain_data in enumerate(chains_and_log_posteriors): # Consider skipping first few entries
  23. chain = chain_data['chain'][:int(chain_length/2)]
  24. log_posteriors = chain_data['log_posteriors'][:int(chain_length/2)]
  25. exp_lp_pairs = list(zip(chain,log_posteriors))
  26. exps_in_chains[i] = set(map(to_tuple, chain))
  27. print(sorted(log_posteriors, reverse=True))
  28. lps_to_exps = defaultdict(set)
  29. for exp,lp in exp_lp_pairs:
  30. lps_to_exps[lp].add(to_tuple(exp))
  31. num_exps_in_chain = len(exps_in_chains[i])
  32. print(lps_to_exps.keys())
  33. print('\n')
  34. chain_info.write(f'Num. expressions in chain {i}: {num_exps_in_chain}\n')
  35. decreasing_lps = sorted(lps_to_exps.keys(), reverse=True)
  36. chain_info.write("Expressions by decreasing log posterior\n")
  37. for lp in decreasing_lps:
  38. chain_info.write(f'lp = {lp} [{len(lps_to_exps[lp])} exps]:\n')
  39. for exp in lps_to_exps[lp]:
  40. chain_info.write(f' {exp}\n')
  41. chain_info.write('\n')
  42. chain_info.write('\n')
  43. all_exps = set(itertools.chain(*exps_in_chains))
  44. chain_info.write(f'Total num. distinct exps across all chains (including warm-up): {len(all_exps)}\n')
  45. with open("params.yaml", 'r') as fd:
  46. params = yaml.safe_load(fd)
  47. true_norm_exp = params['true_norm']['exp']
  48. true_norm_tuple = to_tuple(true_norm_exp)
  49. chain_info.write(f'True norm in some chain(s): {true_norm_tuple in all_exps}\n')
  50. num_chains_in_to_exps = defaultdict(set)
  51. for exp in all_exps:
  52. num_chains_in = operator.countOf(map(operator.contains,
  53. exps_in_chains,
  54. (exp for _ in range(len(exps_in_chains)))
  55. ),
  56. True)
  57. num_chains_in_to_exps[num_chains_in].add(exp)
  58. for num in sorted(num_chains_in_to_exps.keys(), reverse=True):
  59. chain_info.write(f'Out of {len(exps_in_chains)} chains ...\n')
  60. chain_info.write(f'{len(num_chains_in_to_exps[num])} exps are in {num} chains.\n')
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...