0

I am training a denoising autoencoder with tensorflow 1.14.0 in python 3.6.8. each training step includes a fit of the model and should also optimize an other variable theta (of a binomial distribution used in loss) based on the previous prediction, which has an impact on the next prediction.

here is my question: how do I minimize this theta after each training step for each column individually? so alternate training based on the current prediction as input for minimization problem per column?

what I already tried:

### no functioning python code, just to give you an idea
### input in tensorflow model: ae_input, theta


### training steps
for epoch in range(200):
    model.fit( x = ae_input+theta,
        y = ae_input,
        epochs = epoch+1)
    current_pred = model.predict(ae_input+theta)

    theta = update_theta(ae_input, current_pred)   # PROBLEMATIC STEP


### current way because map_fn is not working and
### would also be too slow due to multiple session creation
def update_theta(y_true, y_pred):
    theta_list=[]
    for col in range(y_true.get_shape()[1]):
        t = theta_optimize(y_true[:,x], y_pred[:,x])
        theta_list.append(t)
    return theta_list

### minimize theta for single column
def theta_optimize(y_t, y_p):
    var_theta = tf.Variable(5.)
    func_to_minimize = loss_per_col(y_t, y_p, var_theta) 
    optimial_theta = tf_minimize(func_to_optimize, output=var_theta) 
    ### tf_minimize from link mentioned above
    return optimal_theta

1 Answers1

0

for these, who are still interested, I solved it by switching to scipy between each step. Surely not the ideal way, but was faster and worked fine for me

### CODE FOR TENSORFLOW 2.0.0 beta

### function for scipy to minimize theta between 0-1000
def theta_loss_per_col(y_t, y_p, theta):
    # ... 
    return loss


### function for scipy to optimize
def func_minimize_theta(y_t, y_p):
    return lambda x: theta_loss_per_col(y_t, y_p, x).numpy()


### return updated list of thetas
def get_updated_theta(y_true, y_pred):
    y_true_cols = tf.range(y_true.shape[1])
    theta_list =  tf.map_fn(lambda i:
scipy.optimize.fminbound(func_minimize_theta(y_true[:,i], y_pred[:,i]),
1e-5, 1000) , y_true_cols, dtype=tf.float32) # maybe add:parallel_iterations=10
    return theta_list