map() 是python自带的函数
nest.map_structure() 是tensorflow中定义的功能类似于 map() 的函数
先看一下参数对比
map(func, *iterables)
nest.map_structure(func, *structure, **check_types_dict)
相同:
两者都是对一个可循环结构的元素依次应用函数的过程。
不同的是:
map()返回一个map 类对象,
map_structure()返回一个与参数structure有相同类型的structure。
看下面的小例子,可以很清晰的显示我所说的:
from tensorflow.python.util import nest
def function(x):
return x**2
map_result = map(function,[1,2,3])
nest_map_result = nest.map_structure(function,[1,2,3])
print("--------map_result_information--------")
print("map_result=",map_result)
print("type(map_result)=",type(map_result))
print("list(map_result)=",list(map_result))
print("\n--------nes_map_result_information--------")
print("nest_map_result=",nest_map_result)
print("type(nest_map_result)=",type(nest_map_result))
运行结果如下:
--------map_result_information--------
map_result= <map object at 0x7fb153eb59b0>
type(map_result)= <class 'map'>
list(map_result)= [1, 4, 9]
--------nes_map_result_information--------
nest_map_result= [1, 4, 9]
type(nest_map_result)= <class 'list'>
也就是说,两个函数都是对list[1,2,3]中的每个元素应用函数function(),
map()返回一个map()对象,我们需要转化为list结构。
map_structure(),因为传入的参数是list,所以返回值也是list结构。
参考:https://blog.csdn.net/weixin_41700555/article/details/85011957