1

Is it possible, and if so, how, to formulate this computation as an einsum formulation as described here (tensorflow) or here (numpy).

def dist_np(X,Y,a,b):
    Z = np.ndarray(shape=(a,a), dtype=float)
    Z.fill(0)
    for i in range(a):
        for k in range(a):
            for j in range(b):
                Z[i,k] += (X[i,j] - Y[k,j])**2
            Z[i,k] = np.math.sqrt(Z[i,k])
    return Z

Maybe there is some way of performing a subtraction that isn't obvious from the description or the data can be reorganized in such a way that the result is similar or can be transformed using a simple, element-wise tensorflow operation?

I'm specifically asking for einsum notation as it is supported as tensorflow operation while performing the operation defined using numpy above can't be trivially translated into tensorflow without the use of tf.map_fn, which I wan't to avoid due too bad performance for large amounts of data.

hechth
  • 85
  • 10

0 Answers0