Saved image — no attribution recorded
— saved image
907 908 909 def permute_matrix(W, rng=None): 910 """permute a matrix in a reversible way""" 911 912 num_params = np.prod(W.shape) 913 vec = W.reshape(num_params) 914 if rng is None: 915 p_ids = np.random.permutation(np.arange(num_params)) 916 else: 917 p_ids = rng.permutation(np.arange(num_params)) 918 p_vec = vec[p_ids] 919 p_W = p_vec.reshape(W.shape) 920 921 return p_W, p_ids 922 923 924 def unpermute_matrix(W, p_ids): 925 """unpermute a matrix, using the original ids to permute it""" 926 927 num_params = np.prod(W.shape) 928 vec = W.reshape(num_params) 929 unp_ids = np.argsort(p_ids) 930 unp_vec = vec[unp_ids] 931 unp_W = unp_vec.reshape(W.shape) 932 933 return unp_W 934
Note from Claude Sonnet 5
Screenshot of a Python code editor (line numbers 907-934) showing two functions, permute_matrix and unpermute_matrix, which reversibly shuffle the elements of a weight matrix using numpy.