python - Tensorflow: tf.case with parametrized callable, case list defined in for-loop -


i'm trying implement case-branch within training loop ensemble of autoencoders: depending on condition 1 specific autoencoder should updated. have been trying implementent using tf.case(), it's not working expected...

def f(k_win):      update_bw = tf.train.adamoptimizer(learning_rate=learningrate).minimize(cost_list[k_win])      return update_mse_winner(k_win) + [update_bw, update_n_list(k_win), update_n_alpha_list(k_win)]   winner_index = tf.argmin(cost_alpha_list, 0)    case_list = []  k in range(n_class):       case = (tf.equal(winner_index,k), lambda: f(k))         case_list.append(case)   execution_list = tf.case(case_list, lambda: f(0)) 

winner_index: index of autoencoder update

f(k_win): returns update callables specific ae-index

case_list: contains pairs of booleans , parametrized functions

execution_list: callable sess.run() in execution-loop.

the parameter k in for-loop should define case_list, 'lambda: f(k)', seems, after building list, 'lambda: f(k)' set last k=n_classes-1: effect is, last autoencoder updated, , not 1 'winner_index'. have idea, what's going on here...?

thanks.

the problem lambdas defining using global variable k which, time function called, has last value took in loop (n_class - 1).

a more simple example:

lst = [] k in range(10):     lst.append(lambda: k * k) print([lst_i() lst_i in lst]) 

gives:

[81, 81, 81, 81, 81, 81, 81, 81, 81, 81] 

instead of:

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 

this answer explains issue better , points out couple of methods overcome this. in case, can this:

def f(k_win):      update_bw = tf.train.adamoptimizer(learning_rate=learningrate).minimize(cost_list[k_win])      return update_mse_winner(k_win) + [update_bw, update_n_list(k_win), update_n_alpha_list(k_win)]   winner_index = tf.argmin(cost_alpha_list, 0)    case_list = []  k in range(n_class):       case = (tf.equal(winner_index,k), (lambda kk: lambda: f(kk))(k))         case_list.append(case)   execution_list = tf.case(case_list, lambda: f(0)) 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -