开发者

Gaussian connection pattern between 2 different layers of neurons of different size

开发者 https://www.devze.com 2023-04-08 14:34 出处:网络
I have code to solve the following problem, but it seems too slow to use (I think O(n^4)). Help a python beginner turn correct code into usable code!

I have code to solve the following problem, but it seems too slow to use (I think O(n^4)). Help a python beginner turn correct code into usable code!

A neuron can be specified by its layer, and a 2 dimensional index. For example, N{0}(1,1) is the neuron in the first layer, 2nd row, 2nd column. Each neuron is located in 2 dimensional space, and both layers uniformly fill the same space. We can therefore assign an (x,y) coordinate to each neuron based on its 2-dimensional index, and the number of rows/columns its layer has. For example: Let's say the first layer has s0 rows and columns. Then, N{0}(1,1) would be located at (x,y) = (1.5/s0,1.5/s0)

I have to specify a connection pattern between 2 layers of neurons. Let's say the first layer is s0 by s0, and the next is s1 by s1. For each layer, I can give a neuron a unique index by going column first. The output of my function will then be a matrix with s0*s0 rows, and s1*s1 columns: each entry specifies the strength of a connection from layer 0 to layer 1. I want the strength of this connection to be the value of a gaussian function evaluated at the difference between the spatial location (x,y) of the two neurons.

My first approach was to use nested for loops, but this is horribly slow:

def multiscaleNormalConnection(s0,s1,standardDeviationOfGaussian):  
  C = zeros((scale1**2),(scale2**2))  #connection matrix
  sigma = matrix([[standardDeviationOfGaussian**2, 0],[0, standardDeviationOfGaussian**2]])
  coeff = power(2*pi*linalg.det开发者_StackOverflow(sigma),-.5)
  for i in range(C.shape[0]):
    for j in range(C.shape[1]):
      inColumn = i/scale1
      inX = float(inColumn)/s0
      inRow = mod(i,scale1)
      inY = float(inRow)/s0
      outColumn = j/s1
      outX = float(outColumn)/s1
      outRow = mod(j,scale1)
      outY = float(outRow)/s1
      dev = array([outX-inX,outY-inY])
      C[i,j] = coeff*exp(-0.5*dot(dot(dev.T,sigma.I),dev))              
  return C

Perhaps there is some way to calculate all the values you will need beforehand? Are there any python tricks to help me speed this up? If it were matlab I would try to vectorize the code, but I need to write this in python.

Any ideas welcome! Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消