Skip to content

Commit e26048c

Browse files
committed
Diffferential Evolution
1 parent 2a39b5b commit e26048c

File tree

3 files changed

+356
-46
lines changed

3 files changed

+356
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {
7+
"collapsed": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"import numpy as np\n",
12+
"import random\n",
13+
"from matplotlib import pyplot as plt\n",
14+
"%matplotlib inline"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 2,
20+
"metadata": {
21+
"collapsed": false
22+
},
23+
"outputs": [
24+
{
25+
"name": "stdout",
26+
"output_type": "stream",
27+
"text": [
28+
"[ 1.53605874 0.97791895]\n"
29+
]
30+
}
31+
],
32+
"source": [
33+
"N_POP = 100\n",
34+
"N_GEN = 20\n",
35+
"\n",
36+
"N_DIM = 2\n",
37+
"\n",
38+
"F = 0.5\n",
39+
"CR = 0.5\n",
40+
"\n",
41+
"random_shift = 2*np.random.random((N_DIM))\n",
42+
"print random_shift"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 20,
48+
"metadata": {
49+
"collapsed": true
50+
},
51+
"outputs": [],
52+
"source": [
53+
"def loss(x):\n",
54+
" n_dim = x.shape[0]\n",
55+
" \n",
56+
" # Sphere\n",
57+
" y = np.sum((random_shift + x)**2)\n",
58+
" return y\n",
59+
"\n",
60+
"\n",
61+
"def generate_random_point(n_dim=N_DIM, lim=10):\n",
62+
" pt = (2*np.random.random((n_dim,))-1)*lim\n",
63+
" return pt\n",
64+
"\n",
65+
"pop = [generate_random_point() for ix in range(N_POP)]\n",
66+
"temp = []"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": 21,
72+
"metadata": {
73+
"collapsed": false,
74+
"scrolled": false
75+
},
76+
"outputs": [
77+
{
78+
"name": "stdout",
79+
"output_type": "stream",
80+
"text": [
81+
"Best loss: 4.1177938188 [-0.89108622 0.9460884 ]\n",
82+
"Best loss: 4.1177938188 [-0.89108622 0.9460884 ]\n",
83+
"Best loss: 1.50118526642 [-2.08984937 0.11501344]\n",
84+
"Best loss: 0.579470348565 [-0.89108622 -1.38224653]\n",
85+
"Best loss: 0.374478246274 [-2.08984937 -1.23829212]\n",
86+
"Best loss: 0.248505387938 [-1.82764852 -1.38224653]\n",
87+
"Best loss: 0.223798210632 [-1.83912484 -1.34116701]\n",
88+
"Best loss: 0.0684560556361 [-1.5617855 -1.23829212]\n",
89+
"Best loss: 0.0439413288574 [-1.34875816 -1.07204558]\n",
90+
"Best loss: 0.0439413288574 [-1.34875816 -1.07204558]\n"
91+
]
92+
}
93+
],
94+
"source": [
95+
"for gen in range(N_GEN):\n",
96+
" ord_pop = sorted(pop, key=lambda x: loss(x))\n",
97+
" best = ord_pop[0]\n",
98+
" print \"Best loss:\", loss(best), best\n",
99+
" \n",
100+
" for ix in range(N_POP):\n",
101+
" x = pop[ix]\n",
102+
" a, b, c = random.sample(pop, 3)\n",
103+
" while (list(a) == list(x)) or (list(b) == list(x)) or (list(c) == list(x)):\n",
104+
" a, b, c = random.sample(pop, 3)\n",
105+
" \n",
106+
" t = x + F*(b-c)\n",
107+
" # print t\n",
108+
" \n",
109+
" R = int(np.random.random() * N_DIM)\n",
110+
" y = np.copy(x)\n",
111+
" \n",
112+
" for j in range(N_DIM):\n",
113+
" if (np.random.random() < CR) or (j==R):\n",
114+
" y[j] = t[j]\n",
115+
" else:\n",
116+
" y[j] = x[j]\n",
117+
" \n",
118+
" if loss(y) < loss(x):\n",
119+
" pop[ix] = y\n",
120+
" else:\n",
121+
" pass"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"metadata": {
128+
"collapsed": true
129+
},
130+
"outputs": [],
131+
"source": []
132+
}
133+
],
134+
"metadata": {
135+
"kernelspec": {
136+
"display_name": "Python 2",
137+
"language": "python",
138+
"name": "python2"
139+
},
140+
"language_info": {
141+
"codemirror_mode": {
142+
"name": "ipython",
143+
"version": 2
144+
},
145+
"file_extension": ".py",
146+
"mimetype": "text/x-python",
147+
"name": "python",
148+
"nbconvert_exporter": "python",
149+
"pygments_lexer": "ipython2",
150+
"version": "2.7.12"
151+
}
152+
},
153+
"nbformat": 4,
154+
"nbformat_minor": 2
155+
}

class_20/.ipynb_checkpoints/NeuralArt-checkpoint.ipynb

+46-46
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
{
3030
"cell_type": "code",
31-
"execution_count": 48,
31+
"execution_count": 63,
3232
"metadata": {
3333
"collapsed": true
3434
},
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"cell_type": "code",
53-
"execution_count": 49,
53+
"execution_count": 64,
5454
"metadata": {
5555
"collapsed": true
5656
},
@@ -81,7 +81,7 @@
8181
},
8282
{
8383
"cell_type": "code",
84-
"execution_count": 50,
84+
"execution_count": 65,
8585
"metadata": {
8686
"collapsed": true
8787
},
@@ -93,7 +93,7 @@
9393
},
9494
{
9595
"cell_type": "code",
96-
"execution_count": 51,
96+
"execution_count": 66,
9797
"metadata": {
9898
"collapsed": true
9999
},
@@ -108,7 +108,7 @@
108108
},
109109
{
110110
"cell_type": "code",
111-
"execution_count": 52,
111+
"execution_count": 67,
112112
"metadata": {
113113
"collapsed": false
114114
},
@@ -120,7 +120,7 @@
120120
"_________________________________________________________________\n",
121121
"Layer (type) Output Shape Param # \n",
122122
"=================================================================\n",
123-
"input_4 (InputLayer) (3, 400, 533, 3) 0 \n",
123+
"input_5 (InputLayer) (3, 400, 533, 3) 0 \n",
124124
"_________________________________________________________________\n",
125125
"block1_conv1 (Conv2D) (3, 400, 533, 64) 1792 \n",
126126
"_________________________________________________________________\n",
@@ -173,7 +173,7 @@
173173
},
174174
{
175175
"cell_type": "code",
176-
"execution_count": 53,
176+
"execution_count": 68,
177177
"metadata": {
178178
"collapsed": false
179179
},
@@ -182,25 +182,25 @@
182182
"name": "stdout",
183183
"output_type": "stream",
184184
"text": [
185-
"block4_pool Tensor(\"block4_pool_4/MaxPool:0\", shape=(3, 25, 33, 512), dtype=float32)\n",
186-
"block1_pool Tensor(\"block1_pool_4/MaxPool:0\", shape=(3, 200, 266, 64), dtype=float32)\n",
187-
"block4_conv1 Tensor(\"block4_conv1_4/Relu:0\", shape=(3, 50, 66, 512), dtype=float32)\n",
188-
"block2_conv1 Tensor(\"block2_conv1_4/Relu:0\", shape=(3, 200, 266, 128), dtype=float32)\n",
189-
"block2_conv2 Tensor(\"block2_conv2_4/Relu:0\", shape=(3, 200, 266, 128), dtype=float32)\n",
190-
"block4_conv2 Tensor(\"block4_conv2_4/Relu:0\", shape=(3, 50, 66, 512), dtype=float32)\n",
191-
"block4_conv3 Tensor(\"block4_conv3_4/Relu:0\", shape=(3, 50, 66, 512), dtype=float32)\n",
192-
"block5_conv2 Tensor(\"block5_conv2_4/Relu:0\", shape=(3, 25, 33, 512), dtype=float32)\n",
193-
"block2_pool Tensor(\"block2_pool_4/MaxPool:0\", shape=(3, 100, 133, 128), dtype=float32)\n",
194-
"block5_conv3 Tensor(\"block5_conv3_4/Relu:0\", shape=(3, 25, 33, 512), dtype=float32)\n",
195-
"block1_conv1 Tensor(\"block1_conv1_4/Relu:0\", shape=(3, 400, 533, 64), dtype=float32)\n",
196-
"block5_conv1 Tensor(\"block5_conv1_4/Relu:0\", shape=(3, 25, 33, 512), dtype=float32)\n",
197-
"block3_pool Tensor(\"block3_pool_4/MaxPool:0\", shape=(3, 50, 66, 256), dtype=float32)\n",
198-
"block1_conv2 Tensor(\"block1_conv2_4/Relu:0\", shape=(3, 400, 533, 64), dtype=float32)\n",
199-
"input_4 Tensor(\"concat_3:0\", shape=(3, 400, 533, 3), dtype=float32)\n",
200-
"block3_conv1 Tensor(\"block3_conv1_4/Relu:0\", shape=(3, 100, 133, 256), dtype=float32)\n",
201-
"block3_conv3 Tensor(\"block3_conv3_4/Relu:0\", shape=(3, 100, 133, 256), dtype=float32)\n",
202-
"block3_conv2 Tensor(\"block3_conv2_4/Relu:0\", shape=(3, 100, 133, 256), dtype=float32)\n",
203-
"block5_pool Tensor(\"block5_pool_4/MaxPool:0\", shape=(3, 12, 16, 512), dtype=float32)\n"
185+
"block4_pool Tensor(\"block4_pool_5/MaxPool:0\", shape=(3, 25, 33, 512), dtype=float32)\n",
186+
"block1_pool Tensor(\"block1_pool_5/MaxPool:0\", shape=(3, 200, 266, 64), dtype=float32)\n",
187+
"block4_conv1 Tensor(\"block4_conv1_5/Relu:0\", shape=(3, 50, 66, 512), dtype=float32)\n",
188+
"block2_conv1 Tensor(\"block2_conv1_5/Relu:0\", shape=(3, 200, 266, 128), dtype=float32)\n",
189+
"block2_conv2 Tensor(\"block2_conv2_5/Relu:0\", shape=(3, 200, 266, 128), dtype=float32)\n",
190+
"block4_conv2 Tensor(\"block4_conv2_5/Relu:0\", shape=(3, 50, 66, 512), dtype=float32)\n",
191+
"block4_conv3 Tensor(\"block4_conv3_5/Relu:0\", shape=(3, 50, 66, 512), dtype=float32)\n",
192+
"block2_pool Tensor(\"block2_pool_5/MaxPool:0\", shape=(3, 100, 133, 128), dtype=float32)\n",
193+
"block5_conv3 Tensor(\"block5_conv3_5/Relu:0\", shape=(3, 25, 33, 512), dtype=float32)\n",
194+
"block5_conv2 Tensor(\"block5_conv2_5/Relu:0\", shape=(3, 25, 33, 512), dtype=float32)\n",
195+
"block5_conv1 Tensor(\"block5_conv1_5/Relu:0\", shape=(3, 25, 33, 512), dtype=float32)\n",
196+
"block3_pool Tensor(\"block3_pool_5/MaxPool:0\", shape=(3, 50, 66, 256), dtype=float32)\n",
197+
"block1_conv2 Tensor(\"block1_conv2_5/Relu:0\", shape=(3, 400, 533, 64), dtype=float32)\n",
198+
"block1_conv1 Tensor(\"block1_conv1_5/Relu:0\", shape=(3, 400, 533, 64), dtype=float32)\n",
199+
"input_5 Tensor(\"concat_4:0\", shape=(3, 400, 533, 3), dtype=float32)\n",
200+
"block3_conv1 Tensor(\"block3_conv1_5/Relu:0\", shape=(3, 100, 133, 256), dtype=float32)\n",
201+
"block3_conv3 Tensor(\"block3_conv3_5/Relu:0\", shape=(3, 100, 133, 256), dtype=float32)\n",
202+
"block3_conv2 Tensor(\"block3_conv2_5/Relu:0\", shape=(3, 100, 133, 256), dtype=float32)\n",
203+
"block5_pool Tensor(\"block5_pool_5/MaxPool:0\", shape=(3, 12, 16, 512), dtype=float32)\n"
204204
]
205205
}
206206
],
@@ -212,7 +212,7 @@
212212
},
213213
{
214214
"cell_type": "code",
215-
"execution_count": 54,
215+
"execution_count": 69,
216216
"metadata": {
217217
"collapsed": true
218218
},
@@ -247,7 +247,7 @@
247247
},
248248
{
249249
"cell_type": "code",
250-
"execution_count": 55,
250+
"execution_count": 70,
251251
"metadata": {
252252
"collapsed": true
253253
},
@@ -263,7 +263,7 @@
263263
},
264264
{
265265
"cell_type": "code",
266-
"execution_count": 56,
266+
"execution_count": 71,
267267
"metadata": {
268268
"collapsed": true
269269
},
@@ -276,7 +276,7 @@
276276
},
277277
{
278278
"cell_type": "code",
279-
"execution_count": 57,
279+
"execution_count": 72,
280280
"metadata": {
281281
"collapsed": true
282282
},
@@ -294,7 +294,7 @@
294294
},
295295
{
296296
"cell_type": "code",
297-
"execution_count": 58,
297+
"execution_count": 73,
298298
"metadata": {
299299
"collapsed": true
300300
},
@@ -313,7 +313,7 @@
313313
},
314314
{
315315
"cell_type": "code",
316-
"execution_count": 59,
316+
"execution_count": 74,
317317
"metadata": {
318318
"collapsed": true
319319
},
@@ -335,7 +335,7 @@
335335
},
336336
{
337337
"cell_type": "code",
338-
"execution_count": 60,
338+
"execution_count": 75,
339339
"metadata": {
340340
"collapsed": true
341341
},
@@ -366,7 +366,7 @@
366366
},
367367
{
368368
"cell_type": "code",
369-
"execution_count": 61,
369+
"execution_count": 76,
370370
"metadata": {
371371
"collapsed": false
372372
},
@@ -376,45 +376,45 @@
376376
"output_type": "stream",
377377
"text": [
378378
"('Start of iteration', 0)\n",
379-
"('Current loss value:', 6.0200903e+10)\n",
379+
"('Current loss value:', 7.2344904e+10)\n",
380380
"('Image saved as', 'results/im_at_iteration_0.png')\n",
381381
"Iteration 0 completed in 23s\n",
382382
"('Start of iteration', 1)\n",
383-
"('Current loss value:', 2.8211651e+10)\n",
383+
"('Current loss value:', 3.2852105e+10)\n",
384384
"('Image saved as', 'results/im_at_iteration_1.png')\n",
385385
"Iteration 1 completed in 23s\n",
386386
"('Start of iteration', 2)\n",
387-
"('Current loss value:', 2.2322983e+10)\n",
387+
"('Current loss value:', 2.5034301e+10)\n",
388388
"('Image saved as', 'results/im_at_iteration_2.png')\n",
389389
"Iteration 2 completed in 23s\n",
390390
"('Start of iteration', 3)\n",
391-
"('Current loss value:', 2.0141793e+10)\n",
391+
"('Current loss value:', 2.2248387e+10)\n",
392392
"('Image saved as', 'results/im_at_iteration_3.png')\n",
393393
"Iteration 3 completed in 23s\n",
394394
"('Start of iteration', 4)\n",
395-
"('Current loss value:', 1.9059913e+10)\n",
395+
"('Current loss value:', 2.0828391e+10)\n",
396396
"('Image saved as', 'results/im_at_iteration_4.png')\n",
397397
"Iteration 4 completed in 23s\n",
398398
"('Start of iteration', 5)\n",
399-
"('Current loss value:', 1.8409126e+10)\n",
399+
"('Current loss value:', 1.9998147e+10)\n",
400400
"('Image saved as', 'results/im_at_iteration_5.png')\n",
401401
"Iteration 5 completed in 23s\n",
402402
"('Start of iteration', 6)\n",
403-
"('Current loss value:', 1.7965795e+10)\n",
403+
"('Current loss value:', 1.9452068e+10)\n",
404404
"('Image saved as', 'results/im_at_iteration_6.png')\n",
405405
"Iteration 6 completed in 23s\n",
406406
"('Start of iteration', 7)\n",
407-
"('Current loss value:', 1.7639055e+10)\n",
407+
"('Current loss value:', 1.9060795e+10)\n",
408408
"('Image saved as', 'results/im_at_iteration_7.png')\n",
409409
"Iteration 7 completed in 23s\n",
410410
"('Start of iteration', 8)\n",
411-
"('Current loss value:', 1.738588e+10)\n",
411+
"('Current loss value:', 1.8770221e+10)\n",
412412
"('Image saved as', 'results/im_at_iteration_8.png')\n",
413-
"Iteration 8 completed in 26s\n",
413+
"Iteration 8 completed in 23s\n",
414414
"('Start of iteration', 9)\n",
415-
"('Current loss value:', 1.7179054e+10)\n",
415+
"('Current loss value:', 1.8537345e+10)\n",
416416
"('Image saved as', 'results/im_at_iteration_9.png')\n",
417-
"Iteration 9 completed in 24s\n"
417+
"Iteration 9 completed in 23s\n"
418418
]
419419
}
420420
],

0 commit comments

Comments
 (0)