[Solved] An error occurred when paddlepaddle iterated data: typeerror: ‘function’ object is not iterative

Problem Description: when using the reader to read the training data, there is an error, and the error prompt is typeerror: ‘function’ object is not iterative

error message:

TypeError                                 Traceback (most recent call last)
<ipython-input-12-0b74c209241b> in <module>
      2 for pass_id in range(1):
----> 3     for batch_id, data in enumerate(train_reader):
      4         train_cost, train_acc = exe.run(program=fluid.default_main_program(),
      5                                         feed=feeder.feed(data),

TypeError: 'function' object is not iterable

Problem recurrence: when reading data in the loop, the reader defined by padding. Batch() iterates over the data, and enumerate() uses the defined variables. When the function is called, an error will be reported. The error code is as follows:

for batch_id, data in enumerate(train_reader):
    train_cost, train_acc = exe.run(program=fluid.default_main_program(),
                                    feed=feeder.feed(data),
                                    fetch_list=[avg_cost, acc])

Problem-solving: the same as a data reading function obtained by pad DLE. Batch() , the return value is a reader, and the reason for the above error is that it directly trains_ Reader variable, which refers to a function, so you need to add a bracket to get the return value reader of the function

for batch_id, data in enumerate(train_reader()):
    train_cost, train_acc = exe.run(program=fluid.default_main_program(),
                                    feed=feeder.feed(data),
                                    fetch_list=[avg_cost, acc])

In Python variables, when there are no parentheses, the function itself is called. It is a function object, and there is no need to wait for the function to be executed. With brackets, the result of the function is called, and the result of the function execution must be completed

Similar Posts: