0

I am trying to make a easy code to generate random patterns with fix overlap. You can find below my small code.

I think it is efficient to store my random patterns in a dictionary called patterns={}. To create the first pattern, I simply choose n=10 values out of my values interval ([0,4095]) and I store them in the first element of the dictionary, which is patterns['0']. Then I create the other N-1 patterns with a loop, where, of course, I should consider which are the values that has been already used, so I store those values in the array used_neurons, that was initialized as used_neurons=patterns['0']. After that, I only update the value of used_neurons, but I don't modify any more patterns['0']. However when I print patterns['0'] I can see it gets updated together with used_neurons. Can anyone explain why this happens? Thanks a lot!

import random as rnd
from numpy import *

pat_number=3
neu_per_pat=10
neu_number=4096
overlap=0.1

#------------ check if the setting is correct ------------------------
b=(pat_number-1)*(neu_per_pat-overlap*neu_per_pat)
a=neu_number-neu_per_pat -b

max_num_pat=(neu_number-neu_per_pat)/(neu_per_pat-overlap*neu_per_pat)+1
print "--------------------------------"
print neu_number, " total neurons, ", neu_per_pat, " active neurons per pattern. "
print "Overlap between patterns ",overlap, ", the max number of pattern is",max_num_pat
print "--------------------------------"

if a <0:
    print "can't generate that many pattern"
    print "- decrease the number of pattern or increase the overlap"

#---------------------------------------------------------------------

pool= arange(0,neu_number-1)


#creating the first pattern
patterns = {}  #patterns are stored into a dictionary
patterns['0']=rnd.sample(pool , neu_per_pat)  # Choose the elements of the first pattern
print 'patterns[0]',patterns['0']

#fix the overlapping neurons -- here we assume they are the same for all the patterns 
c=int(overlap*neu_per_pat)
print "overlap*neu_per_pat",c
overlapped_neus=rnd.sample(patterns['0'] , c)

#producing the other N-1 patterns
used_neurons=patterns['0']
print 'used_neurons',used_neurons

for pat in range(1,pat_number):

    remaining_neu1 = setdiff1d(pool,used_neurons)
    patterns['%s' % pat]=rnd.sample(remaining_neu1 , neu_per_pat-c)
    patterns['%s' % pat]+=overlapped_neus
    used_neurons+=patterns['%s' % pat]
    print 'used_neurons',used_neurons
    print 'patterns[0]',patterns['0']

print 'patterns[0]',patterns['0']
print 'patterns[1]',patterns['1']
print 'patterns[2]',patterns['2']
cgastald
  • 13
  • 4

0 Answers0