这段Python代码试图做什么

发布于 2024-08-22 04:09:20 字数 2140 浏览 3 评论 0原文

下面的python代码是按照某种特殊顺序遍历(c,g)的2D网格,该顺序存储在“jobs”和“job_queue”中。但在尝试理解代码后我不确定它是哪种顺序。有人能够讲述顺序并为每个函数的目的提供一些解释吗?谢谢和问候!

import Queue

c_begin, c_end, c_step = -5,  15, 2  
g_begin, g_end, g_step =  3, -15, -2  

def range_f(begin,end,step):  
    # like range, but works on non-integer too  
    seq = []  
    while True:  
        if step > 0 and begin > end: break  
        if step < 0 and begin < end: break  
        seq.append(begin)  
        begin = begin + step  
    return seq  

def permute_sequence(seq):  
    n = len(seq)  
    if n <= 1: return seq  

    mid = int(n/2)  
    left = permute_sequence(seq[:mid])  
    right = permute_sequence(seq[mid+1:])  

    ret = [seq[mid]]  
    while left or right:  
        if left: ret.append(left.pop(0))  
        if right: ret.append(right.pop(0))  

    return ret  

def calculate_jobs():  
    c_seq = permute_sequence(range_f(c_begin,c_end,c_step))  
    g_seq = permute_sequence(range_f(g_begin,g_end,g_step))  
    nr_c = float(len(c_seq))  
    nr_g = float(len(g_seq))  
    i = 0  
    j = 0  
    jobs = []  

    while i < nr_c or j < nr_g:  
        if i/nr_c < j/nr_g:  
            # increase C resolution  
            line = []  
            for k in range(0,j):  
                line.append((c_seq[i],g_seq[k]))  
            i = i + 1  
            jobs.append(line)  
        else:  
            # increase g resolution  
            line = []  
            for k in range(0,i):  
                line.append((c_seq[k],g_seq[j]))  
            j = j + 1  
            jobs.append(line)  
    return jobs  

def main():  

    jobs = calculate_jobs()  
    job_queue = Queue.Queue(0)  

    for line in jobs:  
        for (c,g) in line:  
            job_queue.put((c,g))  

main()

编辑:

每个(c,g)都有一个值。代码实际上是在(c,g)的二维网格中搜索,找到值最小的网格点。我猜代码正在使用某种启发式搜索算法?原始代码在这里 http://www.csie。 ntu.edu.tw/~cjlin/libsvmtools/gridsvr/gridregression.py,这是一个脚本,用于搜索 svm 算法两个参数 c 和 g 的最佳值,并具有最小的验证误差。

The following python code is to traverse a 2D grid of (c, g) in some special order, which is stored in "jobs" and "job_queue". But I am not sure which kind of order it is after trying to understand the code. Is someone able to tell about the order and give some explanation for the purpose of each function? Thanks and regards!

import Queue

c_begin, c_end, c_step = -5,  15, 2  
g_begin, g_end, g_step =  3, -15, -2  

def range_f(begin,end,step):  
    # like range, but works on non-integer too  
    seq = []  
    while True:  
        if step > 0 and begin > end: break  
        if step < 0 and begin < end: break  
        seq.append(begin)  
        begin = begin + step  
    return seq  

def permute_sequence(seq):  
    n = len(seq)  
    if n <= 1: return seq  

    mid = int(n/2)  
    left = permute_sequence(seq[:mid])  
    right = permute_sequence(seq[mid+1:])  

    ret = [seq[mid]]  
    while left or right:  
        if left: ret.append(left.pop(0))  
        if right: ret.append(right.pop(0))  

    return ret  

def calculate_jobs():  
    c_seq = permute_sequence(range_f(c_begin,c_end,c_step))  
    g_seq = permute_sequence(range_f(g_begin,g_end,g_step))  
    nr_c = float(len(c_seq))  
    nr_g = float(len(g_seq))  
    i = 0  
    j = 0  
    jobs = []  

    while i < nr_c or j < nr_g:  
        if i/nr_c < j/nr_g:  
            # increase C resolution  
            line = []  
            for k in range(0,j):  
                line.append((c_seq[i],g_seq[k]))  
            i = i + 1  
            jobs.append(line)  
        else:  
            # increase g resolution  
            line = []  
            for k in range(0,i):  
                line.append((c_seq[k],g_seq[j]))  
            j = j + 1  
            jobs.append(line)  
    return jobs  

def main():  

    jobs = calculate_jobs()  
    job_queue = Queue.Queue(0)  

    for line in jobs:  
        for (c,g) in line:  
            job_queue.put((c,g))  

main()

EDIT:

There is a value for each (c,g). The code actually is to search in the 2D grid of (c,g) to find a grid point where the value is the smallest. I guess the code is using some kind of heuristic search algorithm? The original code is here http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/gridsvr/gridregression.py, which is a script to search for svm algorithm the best values for two parameters c and g with minimum validation error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾夏年华 2024-08-29 04:09:20

permute_sequence 对值列表重新排序,使中间的值排在第一位,然后是每一半的中点,然后是剩余四个四分之一的中点,依此类推。因此,permute_sequence(range(1000)) 是这样开始的:

    [500, 250, 750, 125, 625, 375, ...]

calculate_jobs 使用 permute_sequence 提供的一维坐标序列交替填充行和列。

如果您最终要搜索整个 2D 空间,这并不能帮助您更快完成。您不妨按顺序扫描所有点。但我认为这个想法是在搜索中尽早找到最小值的合适近似。我怀疑你也可以通过随机打乱列表来做到这一点。

xkcd 读者会注意到小便池协议只会给出结果略有不同(可能更好):

    [0, 1000, 500, 250, 750, 125, 625, 375, ...]

permute_sequence reorders a list of values so that the middle value is first, then the midpoint of each half, then the midpoints of the four remaining quarters, and so on. So permute_sequence(range(1000)) starts out like this:

    [500, 250, 750, 125, 625, 375, ...]

calculate_jobs alternately fills in rows and columns using the sequences of 1D coordinates provided by permute_sequence.

If you're going to search the entire 2D space eventually anyway, this does not help you finish sooner. You might as well just scan all the points in order. But I think the idea was to find a decent approximation of the minimum as early as possible in the search. I suspect you could do about as well by shuffling the list randomly.

xkcd readers will note that the urinal protocol would give only slightly different (and probably better) results:

    [0, 1000, 500, 250, 750, 125, 625, 375, ...]
扛刀软妹 2024-08-29 04:09:20

以下是 permute_sequence 的实际应用示例:

print permute_sequence(range(8))
# prints [4, 2, 6, 1, 5, 3, 7, 0]
print permute_sequence(range(12))
# prints [6, 3, 9, 1, 8, 5, 11, 0, 7, 4, 10, 2]

我不确定为什么它使用此顺序,因为在 main 中,似乎所有候选我认为,对 (c,g) 仍在评估。

Here is an example of permute_sequence in action:

print permute_sequence(range(8))
# prints [4, 2, 6, 1, 5, 3, 7, 0]
print permute_sequence(range(12))
# prints [6, 3, 9, 1, 8, 5, 11, 0, 7, 4, 10, 2]

I'm not sure why it uses this order, because in main, it appears that all candidate pairs of (c,g) are still evaluated, I think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文