Problem Description: I wrote the machine translation model according to the official document of paddlepaddle, and this error occurred. I compared the code in the document, and there was no error
error message:
Original sentence:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-27-e241afef7936> in <module>()
20
21 print("Original sentence:")
---> 22 print(" ".join([src_dict[w] for w in feed_data[0][0][1:-1]]))
23
24 print("Translated score and sentence:")
TypeError: 'map' object is not subscriptable
Problem recurrence:
exe = Executor(place)
exe.run(framework.default_startup_program())
for data in test_data():
feed_data = map(lambda x: [x[0]], data)
feed_dict = feeder.feed(feed_data)
feed_dict['init_ids'] = init_ids
feed_dict['init_scores'] = init_scores
results = exe.run(
framework.default_main_program(),
feed=feed_dict,
fetch_list=[translation_ids, translation_scores],
return_numpy=False)
problem analysis:
in Python 3, map will return an iterative object of map type, which is different from the object directly obtained by subscript. In Python 2, there is no problem. In case of this problem, you only need to modify the code to a python 3 compatible mode
problem solving:
If you want to get the map object by subscript, you can first turn the map object into a list object, so you can get it directly by subscript
exe = Executor(place)
exe.run(framework.default_startup_program())
for data in test_data():
feed_data = list(map(lambda x: [x[0]], data))
feed_dict = feeder.feed(feed_data)
feed_dict['init_ids'] = init_ids
feed_dict['init_scores'] = init_scores
results = exe.run(
framework.default_main_program(),
feed=feed_dict,
fetch_list=[translation_ids, translation_scores],
return_numpy=False)
Problem development:
the map() method is a built-in method in Python. The map() method in python2 is different from that in python3. In view of the necessity of everything, it will consume memory to return all the data, so it will be modified to the form of generated object, that is, it will be obtained when it is retrieved, and it will only take effect once