(fn, epochs, batch_shape)
| 45 | |
| 46 | # let's generalize this and put it into a function |
| 47 | def minimize(fn, epochs, batch_shape): |
| 48 | t0 = datetime.now() |
| 49 | losses = [] |
| 50 | x = np.random.randn(np.prod(batch_shape)) |
| 51 | for i in range(epochs): |
| 52 | x, l, _ = fmin_l_bfgs_b( |
| 53 | func=fn, |
| 54 | x0=x, |
| 55 | maxfun=20 |
| 56 | ) |
| 57 | x = np.clip(x, -127, 127) |
| 58 | print("iter=%s, loss=%s" % (i, l)) |
| 59 | losses.append(l) |
| 60 | |
| 61 | print("duration:", datetime.now() - t0) |
| 62 | plt.plot(losses) |
| 63 | plt.show() |
| 64 | |
| 65 | newimg = x.reshape(*batch_shape) |
| 66 | final_img = unpreprocess(newimg) |
| 67 | return final_img[0] |
| 68 | |
| 69 | |
| 70 | if __name__ == '__main__': |
no test coverage detected