简体   繁体   English

在 Python 中自定义梯度下降算法的每一步中存储参数值

[英]Storing parameter values in every step of the custom gradient descent algorithm in Python

I'm trying to make custom gradient descent estimator, however, I am encountering the issue with storing the parameter values at every step of the gradient descent algorithm.我正在尝试制作自定义梯度下降估计器,但是,我遇到了在梯度下降算法的每一步存储参数值的问题。 Here is the code skeleton:这是代码框架:

from numpy import *
import pandas as pd
from joblib import Parallel, delayed
from multiprocessing import cpu_count

ftemp = zeros((2, )) 
stemp = empty([1, ], dtype='<U10') 
la = 10

vals = pd.DataFrame(index=range(la), columns=['a', 'b', 'string']

def sfun(k1, k2, k3, string):
    a = k1*k2
    b = k2*k3
    s = string

    nums = [a, b]
    strs = [s]

    return(nums, strs)

def store(inp):
    r = rsfun(inp[0], inp[1], inp[2], inp[3])

    ftemp = append(ftemp, asarray(r[0]), axis = 0)
    stemp = append(stemp, asarray(r[1]), axis = 0)
    
    return(ftemp, stemp)

for l in range(la):
    inputs = [(2, 3, 4, 'he'),
              (4, 6, 2, 'je'), 
              (2, 7, 5, 'ke')]

    Parallel(n_jobs = cpu_count)(delayed(store)(i) for i in inputs)

    vals.iloc[l, 0:2] = ftemp[0, 0], ftemp[0, 1]
    vals.iloc[l, 2] = stemp[0]

    d = ftemp[2, 0]-ftemp[0, 0]
    

Note: most of the gradient descent stuff is removed because I do not have any issues with that.注意:大部分梯度下降的东西都被删除了,因为我没有任何问题。 the main issues that I have are storing the values at each step.我遇到的主要问题是在每一步存储值。

sfun() is the loss function (I know that it doesn't look like that here) and store() is just an attempt to store the parameter values with each step. sfun() 是损失函数(我知道它在这里看起来不像),而 store() 只是尝试存储每一步的参数值。

The important aspect here is that I want to parallelize the process as sfun() is computationally expensive and the issue with that I want to save values for all parallel runs.这里的重要方面是我想并行化进程,因为 sfun() 在计算上很昂贵,并且我想保存所有并行运行的值的问题。

I tried solving this in many different ways, but I always get a different error.我尝试以多种不同的方式解决这个问题,但我总是遇到不同的错误。

No need to make a temporary storage array, possible to store the results of Parallel() function directly by:无需制作临时存储阵列,可以通过以下方式直接存储Parallel()函数的结果:

a = Parallel(n_jobs = cpu_count)(delayed(store)(i) for i in inputs)

Most importantly, a is populated in order that the inputs are given.最重要的是,填充 a 以便给出输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM