@@ -56,36 +56,29 @@ import torch, torchhd
5656d = 10000 # number of dimensions
5757
5858# create the hypervectors for each symbol
59- country = torchhd.random_hv(1 , d)
60- capital = torchhd.random_hv(1 , d)
61- currency = torchhd.random_hv(1 , d)
59+ keys = torchhd.random_hv(3 , d)
60+ country, capital, currency = keys
6261
63- usa = torchhd.random_hv(1 , d) # United States
64- mex = torchhd.random_hv(1 , d) # Mexico
65-
66- wdc = torchhd.random_hv(1 , d) # Washington D.C.
67- mxc = torchhd.random_hv(1 , d) # Mexico City
68-
69- usd = torchhd.random_hv(1 , d) # US Dollar
70- mxn = torchhd.random_hv(1 , d) # Mexican Peso
62+ usa, mex = torchhd.random_hv(2 , d) # United States and Mexico
63+ wdc, mxc = torchhd.random_hv(2 , d) # Washington D.C. and Mexico City
64+ usd, mxn = torchhd.random_hv(2 , d) # US Dollar and Mexican Peso
7165
7266# create country representations
73- keys = torch.cat([country, capital, currency], dim = 0 )
74-
75- us_values = torch.cat([usa, wdc, usd])
76- US = torchhd.functional.hash_table(keys, us_values)
67+ us_values = torch.stack([usa, wdc, usd])
68+ us = torchhd.functional.hash_table(keys, us_values)
7769
78- mx_values = torch.cat ([mex, mxc, mxn])
79- MX = torchhd.functional.hash_table(keys, mx_values)
70+ mx_values = torch.stack ([mex, mxc, mxn])
71+ mx = torchhd.functional.hash_table(keys, mx_values)
8072
81- MX_US = torchhd.bind(US , MX )
73+ # combine all the associated information
74+ mx_us = torchhd.bind(us, mx)
8275
8376# query for the dollar of mexico
84- usd_of_mex = torchhd.bind(MX_US , usd)
77+ usd_of_mex = torchhd.bind(mx_us , usd)
8578
8679memory = torch.cat([keys, us_values, mx_values], dim = 0 )
8780torchhd.functional.cosine_similarity(usd_of_mex, memory)
88- # tensor([ 0.0133 , 0.0062 , -0.0115, 0.0066 , -0.0007, 0.0149, -0.0034 , 0.0084 , 0.3334 ])
81+ # tensor([-0.0062 , 0.0123 , -0.0057, -0.0019 , -0.0084, -0.0078, 0.0102 , 0.0057 , 0.3292 ])
8982# The hypervector for the Mexican Peso is the most similar.
9083```
9184
0 commit comments