Reputation: 5923
I get the error 'Too many values to unpack' when optimizing using fmin_l_bfgs_b. From stackoverflow I've found that something is wrong with the way I've defined the bounds. However, I cannot seem to find the right way to do it.
I've tried to create a minimal working example below to illustrate the issue. The input is a 28x28x1 greyscale image, which is bounded from 0 to 1. As I see it I therefore want a list of 784 pairs that each have the value (0,1). I've tried to implement that using the following code:
img = random.uniform(size=(28, 28))
constraintPairs = [(0, 1)]*(28*28)
def func(img):
return img.mean()
imgOpt, cost = fmin_l_bfgs_b(func, img, approx_grad=1,bounds=constraintPairs)
What am I doing wrong? Thanks!
Upvotes: 1
Views: 188
Reputation: 12701
The problem is simply the return value of fmin_l_bfgs_b
(Documentation). It returns 3 objects and you only define two in your code. This should work:
img = random.uniform(size=(28, 28))
constraintPairs = [(0, 1)]*(28*28)
def func(img):
return img.mean()
img = reshape(img, (1, 28*28))
imgOpt, cost, info = fmin_l_bfgs_b(func, img, approx_grad=1,bounds=constraintPairs)
imgOpt = reshape(imgOpt, (28,28))
Whether or not 768 dimensions are too many, is hard to tell. If so, you could consider downsampling the input image.
Upvotes: 1