1
+ from matplotlib import cm
2
+ from mpl_toolkits .mplot3d import Axes3D
3
+
1
4
import matplotlib .pyplot as plt
2
5
import numpy as np
3
6
import tensorflow as tf
@@ -22,10 +25,10 @@ def cost_func(x=None, y=None):
22
25
y = tf .placeholder (tf .float32 , shape = [None , 1 ])
23
26
24
27
# two local minima near (0, 0)
25
- z = __f1 (x , y )
28
+ # z = __f1(x, y)
26
29
27
30
# 3rd local minimum at (-0.5, -0.8)
28
- z -= __f2 (x , y , x_mean = - 0.5 , y_mean = - 0.8 , x_sig = 0.35 , y_sig = 0.35 )
31
+ z = - 1 * __f2 (x , y , x_mean = - 0.5 , y_mean = - 0.8 , x_sig = 0.35 , y_sig = 0.35 )
29
32
30
33
# one steep gaussian trench at (0, 0)
31
34
# z -= __f2(x, y, x_mean=0, y_mean=0, x_sig=0.2, y_sig=0.2)
@@ -53,7 +56,8 @@ def __f2(x, y, x_mean, y_mean, x_sig, y_sig):
53
56
54
57
# pyplot settings
55
58
plt .ion ()
56
- plt .figure (figsize = (3 , 2 ), dpi = 300 )
59
+ fig = plt .figure (figsize = (3 , 2 ), dpi = 300 )
60
+ ax = fig .add_subplot (111 , projection = '3d' )
57
61
plt .subplots_adjust (left = 0 , bottom = 0 , right = 1 , top = 1 , wspace = 0 , hspace = 0 )
58
62
params = {'legend.fontsize' : 3 ,
59
63
'legend.handlelength' : 3 }
@@ -72,7 +76,9 @@ def __f2(x, y, x_mean, y_mean, x_sig, y_sig):
72
76
z_val_mesh_flat = sess .run (z , feed_dict = {x : x_val_mesh_flat , y : y_val_mesh_flat })
73
77
z_val_mesh = z_val_mesh_flat .reshape (x_val_mesh .shape )
74
78
levels = np .arange (- 10 , 1 , 0.05 )
75
- plt .contour (x_val_mesh , y_val_mesh , z_val_mesh , levels , alpha = .7 , linewidths = 0.4 )
79
+ # ax.contour(x_val_mesh, y_val_mesh, z_val_mesh, levels, alpha=.7, linewidths=0.4)
80
+ # ax.plot_wireframe(x_val_mesh, y_val_mesh, z_val_mesh, alpha=.5, linewidths=0.4, antialiased=True)
81
+ ax .plot_surface (x_val_mesh , y_val_mesh , z_val_mesh , alpha = .4 , cmap = cm .coolwarm )
76
82
plt .draw ()
77
83
78
84
# starting location for variables
@@ -91,58 +97,71 @@ def __f2(x, y, x_mean, y_mean, x_sig, y_sig):
91
97
cost .append (cost_func (x_var [i ], y_var [i ])[2 ])
92
98
93
99
# define method of gradient descent for each graph
94
- ops_param = [['Adadelta' , 50 ],
95
- ['Adagrad' , 0.10 ],
96
- ['Adam' , 0.05 ],
97
- ['Ftrl' , 0.5 ],
98
- ['GD' , 0.05 ],
99
- ['Momentum' , 0.01 ],
100
- ['RMSProp' , 0.02 ]]
100
+ # optimizer label name, learning rate, color
101
+ ops_param = np .array ([['Adadelta' , 50.0 , 'b' ],
102
+ ['Adagrad' , 0.10 , 'g' ],
103
+ ['Adam' , 0.05 , 'r' ],
104
+ ['Ftrl' , 0.5 , 'c' ],
105
+ ['GD' , 0.05 , 'm' ],
106
+ ['Momentum' , 0.01 , 'y' ],
107
+ ['RMSProp' , 0.02 , 'k' ]])
101
108
102
109
ops = []
103
- ops .append (tf .train .AdadeltaOptimizer (ops_param [0 ][1 ]).minimize (cost [0 ]))
104
- ops .append (tf .train .AdagradOptimizer (ops_param [1 ][1 ]).minimize (cost [1 ]))
105
- ops .append (tf .train .AdamOptimizer (ops_param [2 ][1 ]).minimize (cost [2 ]))
106
- ops .append (tf .train .FtrlOptimizer (ops_param [3 ][1 ]).minimize (cost [3 ]))
107
- ops .append (tf .train .GradientDescentOptimizer (ops_param [4 ][1 ]).minimize (cost [4 ]))
108
- ops .append (tf .train .MomentumOptimizer (ops_param [5 ][1 ], momentum = 0.95 ).minimize (cost [5 ]))
109
- ops .append (tf .train .RMSPropOptimizer (ops_param [6 ][1 ]).minimize (cost [6 ]))
110
+ ops .append (tf .train .AdadeltaOptimizer (float (ops_param [0 , 1 ])).minimize (cost [0 ]))
111
+ ops .append (tf .train .AdagradOptimizer (float (ops_param [1 , 1 ])).minimize (cost [1 ]))
112
+ ops .append (tf .train .AdamOptimizer (float (ops_param [2 , 1 ])).minimize (cost [2 ]))
113
+ ops .append (tf .train .FtrlOptimizer (float (ops_param [3 , 1 ])).minimize (cost [3 ]))
114
+ ops .append (tf .train .GradientDescentOptimizer (float (ops_param [4 , 1 ])).minimize (cost [4 ]))
115
+ ops .append (tf .train .MomentumOptimizer (float (ops_param [5 , 1 ]), momentum = 0.95 ).minimize (cost [5 ]))
116
+ ops .append (tf .train .RMSPropOptimizer (float (ops_param [6 , 1 ])).minimize (cost [6 ]))
117
+
118
+ # 3d plot camera zoom, angle
119
+ xlm = ax .get_xlim3d ()
120
+ ylm = ax .get_ylim3d ()
121
+ zlm = ax .get_zlim3d ()
122
+ ax .set_xlim3d (xlm [0 ] * 0.5 , xlm [1 ] * 0.5 )
123
+ ax .set_ylim3d (ylm [0 ] * 0.5 , ylm [1 ] * 0.5 )
124
+ ax .set_zlim3d (zlm [0 ] * 0.5 , zlm [1 ] * 0.5 )
125
+ azm = ax .azim
126
+ ele = ax .elev + 40
127
+ ax .view_init (elev = ele , azim = azm )
110
128
111
129
with tf .Session () as sess :
112
130
sess .run (tf .global_variables_initializer ())
113
131
114
132
# use last location to draw a line to the current location
115
- last_x , last_y = [], []
116
- plot_cache = []
117
- for i in range (7 ):
118
- last_x .append (x_i )
119
- last_y .append (y_i )
120
- plot_cache .append (None )
121
-
122
- # available colors for each label
123
- colors = ('b' , 'g' , 'r' , 'c' , 'm' , 'y' , 'k' )
133
+ last_x , last_y , last_z = [], [], []
134
+ plot_cache = [None for _ in range (len (ops ))]
124
135
125
136
# loop each step of the optimization algorithm
126
137
steps = 1000
127
138
for iter in range (steps ):
128
139
for i , op in enumerate (ops ):
129
140
# run a step of optimization and collect new x and y variable values
130
- _ , x_val , y_val = sess .run ([op , x_var [i ], y_var [i ]])
141
+ _ , x_val , y_val , z_val = sess .run ([op , x_var [i ], y_var [ i ], cost [i ]])
131
142
132
143
# move dot to the current value
133
144
if plot_cache [i ]:
134
145
plot_cache [i ].remove ()
135
- plot_cache [i ] = plt .scatter (x_val , y_val , color = colors [ i ] , s = 3 , label = ops_param [i ][ 0 ])
146
+ plot_cache [i ] = ax .scatter (x_val , y_val , z_val , s = 3 , depthshade = True , label = ops_param [i , 0 ], color = ops_param [ i , 2 ])
136
147
137
148
# draw a line from the previous value
138
- if last_x [i ] and last_y [i ]:
139
- plt .plot ([last_x [i ], x_val ], [last_y [i ], y_val ], color = colors [i ], linewidth = 0.5 )
149
+ if iter == 0 :
150
+ last_z .append (z_val )
151
+ last_x .append (x_i )
152
+ last_y .append (y_i )
153
+ ax .plot ([last_x [i ], x_val ], [last_y [i ], y_val ], [last_z [i ], z_val ], linewidth = 0.5 , color = ops_param [i , 2 ])
140
154
last_x [i ] = x_val
141
155
last_y [i ] = y_val
156
+ last_z [i ] = z_val
157
+
158
+ if iter == 0 :
159
+ legend = np .vstack ((ops_param [:, 0 ], ops_param [:, 1 ])).transpose ()
160
+ plt .legend (plot_cache , legend )
142
161
143
- plt .legend (plot_cache , ops_param )
144
162
plt .savefig ('figures/' + str (iter ) + '.png' )
145
163
print ('iteration: {}' .format (iter ))
146
- plt .pause (0.001 )
164
+
165
+ plt .pause (0.0001 )
147
166
148
167
print ("done" )
0 commit comments