I want to create a matrix D
that is defined by D[i,j]=d(i-j)
where d
is some arbitrary function that I can choose.
It can easily be done with loops, but it is very slow. Is there an efficiant way of creating this matrix with torch or numpy?
I want to create a matrix D
that is defined by D[i,j]=d(i-j)
where d
is some arbitrary function that I can choose.
It can easily be done with loops, but it is very slow. Is there an efficiant way of creating this matrix with torch or numpy?
You could apply the function (if vectorised) to numpy.indices
:
import numpy as np
i, j = np.indices((n, m))
D = d(i - j)
The following code shows you how you can configure a tensor-based calculation process for your problem:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
import time
#let's define heigth and width of D:
height=45
width=77
#Let's configure inputs for neural network having input shape similar with D but also extra dimension of size 2
syote=keras.Input(shape=(height,width,2))
#Let's make next layer for the network...
valikerros=layers.Dense(1)
#And attach input to this layer...
x=valikerros(syote)
x=layers.Dense(1)(x)
x=layers.Dense(1)(x)
#...and select so many layers you need...according to complexity of the function d, more layers can easily be added...
#Let's make the neural network...
matriisimalli=keras.Model(inputs=syote,outputs=x,name="Special neural network model presenting D including function d")
#And show its strutuce
matriisimalli.summary()
#next let's create ONCE the i,j -matrix index basis for the input, where there is in each i,j coordinate the index values of those coordinates...this need to be done once only, and can also be saved as a variable and be lodaded, if it is essential to avoid usage of for-loops
pohjasyote=np.ones((1,height,width,2))
for korkeus in range(height):
for leveys in range(width):
pohjasyote[0,korkeus,leveys,0]=korkeus
pohjasyote[0,korkeus,leveys,1]=leveys
#Now let's see how long time it takes to calculate the result for D:
alkuaika=time.time()
result_including_information_of_D=matriisimalli.predict(pohjasyote)
loppuaika=time.time()
print("It took ",loppuaika-alkuaika, " seconds to calculate D")
#...and to use the created (rapid tensor-based) structure for calculation let's next train the network...
#using the standard protocol ... where you train the network first to predict d accurately... then verify it works OK ...
#after that simply use it...
#alternative for the training is you arithmetically deduce the correct values for the weight tensors of the model (accurate results..)
...of course please note this is a some kind of "trick" using the advantages of tensors in keras, but by following the idea in the code I think you can find a straightforward way to find a solution for your problem.
If you find hard to follow the idea in calculation (sorry about poor commenting), then first test the code by using your size of D in the calculation, and compare is this speed better than in your current for-loop based solution. If the "matriisimalli" is much better, then it is worth to go through carefully the code and utilize its idea to reach the better performance.