【问题标题】:dataset API RecursionError: maximum recursion depth exceeded数据集 API RecursionError:超出最大递归深度
【发布时间】:2019-05-08 12:59:15
【问题描述】:

我正在尝试使用 tensorflow 数据集 API 和地图功能构建一个可扩展的最小最大缩放器。

首先我遍历我的数据集以找到所有特征的最小值和最大值 (3),然后我想使用 map 函数将最小/最大值缩放器应用于数据集。

这是我的简单代码。

import numpy as np
import tensorflow as tf

b = np.array([[1, 2, 3], [4, 5, 6], [7,8,9],[10,11,12]])
b_ds = tf.data.Dataset.from_tensor_slices(b).batch(2)

my_iterator = b_ds.make_one_shot_iterator()

def compute_min_max(i, my_min, my_max):
    new_batch = my_iterator.get_next()
    my_min = tf.minimum(my_min,tf.reduce_min(new_batch, axis=0))
    my_max = tf.maximum(my_max,tf.reduce_max(new_batch, axis=0))
    return [i+1, my_min, my_max]

i = tf.constant(0)
feat_min = tf.Variable([10,10,10],dtype=tf.int64)
feat_max = tf.Variable([0,0,0],dtype=tf.int64)

c = lambda i, min, max: i < 2
b = lambda i, min, max: compute_min_max(i, min, max)
res_i, res_min, res_max = tf.while_loop(c, b, loop_vars=[i, feat_min, feat_max])

def min_max_ds(feat):
    return tf.cast(feat-res_min,dtype=tf.float64)/tf.cast(res_max-res_min, dtype=tf.float64)

minmax_scaled_ds = b_ds.map(min_max_ds)

scaled_batch = minmax_scaled_ds.make_one_shot_iterator().get_next()

with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    print(sess.run((res_min, res_max, scaled_batch)))

当我执行这段代码时,我得到一个

RecursionError: 超出最大递归深度

我的猜测是 min_max_ds 函数会为每个批次左右回调 tf.while_loop 语句,但我不知道如何冻结 res_min 和 res_max 以便它们在 min_max_ds 函数中用作常量。

【问题讨论】:

    标签: python-3.x tensorflow tensorflow-datasets


    【解决方案1】:

    也许您可以使用以下方法为递归深度设置更高的限制:

    sys.setrecursionlimit(10000)
    

    python3.X 中的默认值为 1000。更大的值可能会起作用。

    【讨论】:

    • 如果您自己尝试过,那么请对您的答案中的文字充满信心。
    猜你喜欢
    • 2017-03-24
    • 2020-10-08
    • 2020-07-13
    • 2018-09-24
    • 2019-01-18
    • 1970-01-01
    • 2019-03-23
    • 2019-04-18
    • 2020-11-28
    相关资源
    最近更新 更多