先看一下参数对比
map(func, *iterables)
nest.map_structure(func, *structure, **check_types_dict)
相同:
两者都是对一个可循环结构的元素依次应用函数的过程。
不同的是:
map()返回一个map 对象,
map_structure()返回一个与参数structure有相同arity的structure。
看下面的小例子,可以很清晰的显示我所说的:
def a(x):
return x**2
map_ob = map(a,[1,2,3])
b = list(map(a,[1,2,3]))
c = nest.map_structure(a,[1,2,3])
print('b',b,'\nc',c)
print('map_ob_type',type(map_ob))
print('b_type',type(b))
print('c_type',type(c))
运行结果如下:
也就是说,两个函数都是对list[1,2,3]中的每个元素应用函数a(),
map()返回一个map()对象,我们需要转化为list结构。
map_structure(),因为传入的参数是list,所以返回值也是list结构。
注意:运行代码时需要导入nest这个包,
用 from tensorflow.python.util import nest 即可