1

I would like to minimize the following cost function

enter image description here

where h_u and h_v are the vectors of real numbers. I am doing this in Python, but I am not sure that the function minimize from scipy.optimize package is what I need.

My approach is the following: given h_u and h_v as two matrices with dimensionality

print(h_u.shape, h_v.shape)
>>>(12458, 7) (12458, 7)

I define objective function

fun = lambda h: (h[0]*h[1]).mean()

Then I stack the vectors

h_in = np.stack((h_u, h_v), axis = 0)

and try to minimize

minimized = minimize(fun, x0 = h_in)

I am not sure that providing h_in as an initial guess x0 is correct, because it produces an error

MemoryError: Unable to allocate 227. GiB for an array with shape (174412, 174412) and data type float64

Could somebody tell me if I do it correctly and how could I circumvent the error? Thank you!

Python
  • 359
  • 2
  • 16

1 Answers1

0

Using scipy.optimize.minimize is not suitable for your problem, as it's meant for continuous problems (most of its methods use a gradient).

I did not compute the memory usage, but in general I'd propose something like this; if the memory is not large enough to fit the whole list comprehension, you can break it down to one loop inside the comprehension and loop over the other vectors, cache the results and compare them to the newly found minimum for that vector.

import numpy as np

a = np.ones((5,2)).T
a[0,:] *= 2

b = np.arange(10).reshape((5,2)).T

min_index_flat = np.argmin([sum(i*j) for i in a for j in b])

min_index_a = min_index_flat // len(a)
min_index_b = min_index_flat % len(b)

EDIT: That is under the assumption that you want to find the minimum of all possible combinations; I just realized that that depends on your set epsilon, but if you just want to compare pairwise, the code would just get easier: min_index_flat = np.argmin([sum(i*j) for i,j in zip(a,b)])

Marius Wallraff
  • 391
  • 1
  • 5
  • 11