Heewony
Heewony

Reputation: 91

How can I apply tf.map_fn using multiple inputs in Tensorflow

I try to get the turning point using tf.map_fn to multiple inputs using Tensorflow in Pycharm.
However, when I try to do this,
I get the error: TypeError: testzz() missing 1 required positional argument: 'data'

How can I solve this problem? Or How can I get the size of idxCut to use a for-loop?

Development contents.

I want to find the TPR (Turning Point Ratio) about idxCut in the data using a for-loop.
I used a for-loop to obtain the TPR between idx, idx-1 and idx + 1.
I want to find data[idx] is higher than the others data[idx-1, idx+1].

    def testtt(data):
        ### Cut-off Threshold
        newData = data[5:num_input - 5]   # shape = [1, 100]
        idxCut = tf.where(newData > cutoff) + 5
        idxCut = tf.squeeze(idxCut)   
        # The size of idxCut is always variable. shape = [1, 10] or shape = [1, 27] or etc

        tq = tf.map_fn(testzz, (idxCut, data), dtype=tf.int32)
        print('tqqqq ', tq)
    def testzz(idxCut, data):
        v1 = tf.where(data[idxCut] > data[idxCut - 1], 1, 0)
        v2 = tf.where(data[idxCut] > data[idxCut + 1], 1, 0)
        return tf.where(v1 + v2 > 1, 1, 0)
Traceback (most recent call last):
  File "D:/PycharmProject/Test_DCGAN_BioSignal/test_xcorr_all.py", line 263, in <module>
    tprX = testtt(zX)
  File "D:/PycharmProject/Test_DCGAN_BioSignal/test_xcorr_all.py", line 149, in testtt
    tq = tf.map_fn(testzz, (idxCut, data), dtype=tf.int32)
  File "C:\Users\UserName\Anaconda3\envs\TSFW_pycharm\lib\site-packages\tensorflow\python\ops\functional_ops.py", line 494, in map_fn
    maximum_iterations=n)
  File "C:\Users\UserName\Anaconda3\envs\TSFW_pycharm\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 3291, in while_loop
    return_same_structure)
  File "C:\Users\UserName\Anaconda3\envs\TSFW_pycharm\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 3004, in BuildLoop
    pred, body, original_loop_vars, loop_vars, shape_invariants)
  File "C:\Users\UserName\Anaconda3\envs\TSFW_pycharm\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2939, in _BuildLoop
    body_result = body(*packed_vars_for_body)
  File "C:\Users\UserName\Anaconda3\envs\TSFW_pycharm\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 3260, in <lambda>
    body = lambda i, lv: (i + 1, orig_body(*lv))
  File "C:\Users\UserName\Anaconda3\envs\TSFW_pycharm\lib\site-packages\tensorflow\python\ops\functional_ops.py", line 483, in compute
    packed_fn_values = fn(packed_values)

TypeError: testzz() missing 1 required positional argument: 'data'

Upvotes: 3

Views: 1610

Answers (1)

javidcf
javidcf

Reputation: 59731

When you give multiple tensors to tf.map_fn, their elements are not passed as independent arguments to the given function, but as a tuple instead. Do this:

def testzz(inputs):
    idxCut, data = inputs
    v1 = tf.where(data[idxCut] > data[idxCut - 1], 1, 0)
    v2 = tf.where(data[idxCut] > data[idxCut + 1], 1, 0)
    return tf.where(v1 + v2 > 1, 1, 0)

Upvotes: 5

Related Questions