【问题标题】:Tensorflow - alternate training: minimize function per columnTensorflow - 交替训练:最小化每列的功能
【发布时间】:2019-08-28 20:12:49
【问题描述】:

我正在 python 3.6.8 中使用 tensorflow 1.14.0 训练去噪自动编码器。每个训练步骤都包括模型的拟合,还应根据先前的预测优化其他变量 theta(损失中使用的二项式分布),这会对下一个预测产生影响。

这是我的问题: 如何在每个列的每个训练步骤之后最小化这个 theta?那么基于当前预测的交替训练作为每列最小化问题的输入?

我已经尝试过的:

### 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

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    对于那些仍然感兴趣的人,我通过在每一步之间切换到 scipy 来解决它。当然不是理想的方式,但对我来说更快并且效果很好

    ### 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      相关资源
      最近更新 更多