0

我正在执行以下操作:

  1. 我有张量流 DNN 层的列表。nn.append(tf.layers.dense(...))
  2. 上面的每个列表都附加到 np.memmap 对象列表中。nnList[i] = nn
  3. 我可以访问 memmap 列表并检索张量。但是当尝试访问其中的张量时,joblib.parallel它会返回“无”类型的对象。但是,内部的 memmap 列表的长度是正确的joblib.parallel

我在下面附上了一个示例代码。

    import os
    import tempfile
    import numpy as np
    import tensorflow as tf
    from joblib import Parallel, delayed, load, dump

    tmpFolder = tempfile.mkdtemp()
    __nnFile = os.path.join(tmpFolder, 'nn.mmap')
    nnList = np.memmap(__nnFile, dtype=object, mode='w+', shape=(5))

    def main():
        for i in range(5):
            nn = []
            input = tf.placeholder(dtype=tf.float32, shape=(1, 8))
            nn.append(tf.layers.dense(inputs=input, units=8, activation=tf.sigmoid,  
                                        trainable=False))
            nn.append(tf.layers.dense(inputs=nn[0], units=2, activation=tf.sigmoid,  
                                        trainable=False))

            nnList[i] = nn

        print('nnList: ' + str(len(nnList)))
        for i in range(5):
            nn = nnList[i]
            print(nn)
            print(nn[-1])
            print('---------------------------  ' + str(i))

        with Parallel(n_jobs = -1) as parallel:
            parallel(delayed(func1)(i) for i in range(5))

    def func1(i):
        print('nnList: ' + str(len(nnList)))
        for x in range(5):
            nn = nnList[x]
            print(nn)
            print('---------------------------  ' + str(x))

    if __name__ == '__main__':
        main()

上面的代码给出了这个输出。请注意数组的长度以及张量如何变为None.

    nnList: 5
    [<tf.Tensor 'dense/Sigmoid:0' shape=(1, 8) dtype=float32>, <tf.Tensor 'dense_1/Sigmoid:0' shape=(1, 2) dtype=float32>]
    Tensor("dense_1/Sigmoid:0", shape=(1, 2), dtype=float32)
    ---------------------------  0
    [<tf.Tensor 'dense_2/Sigmoid:0' shape=(1, 8) dtype=float32>, <tf.Tensor 'dense_3/Sigmoid:0' shape=(1, 2) dtype=float32>]
    Tensor("dense_3/Sigmoid:0", shape=(1, 2), dtype=float32)
    ---------------------------  1
    [<tf.Tensor 'dense_4/Sigmoid:0' shape=(1, 8) dtype=float32>, <tf.Tensor 'dense_5/Sigmoid:0' shape=(1, 2) dtype=float32>]
    Tensor("dense_5/Sigmoid:0", shape=(1, 2), dtype=float32)
    ---------------------------  2
    [<tf.Tensor 'dense_6/Sigmoid:0' shape=(1, 8) dtype=float32>, <tf.Tensor 'dense_7/Sigmoid:0' shape=(1, 2) dtype=float32>]
    Tensor("dense_7/Sigmoid:0", shape=(1, 2), dtype=float32)
    ---------------------------  3
    [<tf.Tensor 'dense_8/Sigmoid:0' shape=(1, 8) dtype=float32>, <tf.Tensor 'dense_9/Sigmoid:0' shape=(1, 2) dtype=float32>]
    Tensor("dense_9/Sigmoid:0", shape=(1, 2), dtype=float32)
    ---------------------------  4
    nnList: 5
    None
    ---------------------------  0
    None
    ---------------------------  1
    None
    ---------------------------  2
    None
    ---------------------------  3
    None
    ---------------------------  4

我怎样才能访问里面的张量joblib.parallel?请帮忙。

4

1 回答 1

0

当时就发现了问题。希望它对将来的人有所帮助。

这个None问题与张量无关。我joblib.Parallel以错误的方式使用该功能。

应该将变量传递delayed给分叉的进程可以访问(我怎么在文档中忽略了这一点!)。正确的方法:

with Parallel(n_jobs = -1) as parallel:
    parallel(delayed(func1)(i, WHATEVER_VARIABLE_I_WANT) for i in range(5))
于 2018-11-08T00:52:35.460 回答