python中map()和reduce()函数的区别
来源:互联网 发布:淘宝首页链接一键生成 编辑:程序博客网 时间:2024/05/17 02:39
python中map()和reduce的区别
问题来源
在使用深度学习(dl)框架时,使用函数tf.reduce_mean()后,产生了疑问,为了弄清楚其具体的意义,查找了一些资料。这里我用更直白的方式向大家解释一下python中map()
和reduce()
的用法和区别。
map()和reduce()的区别
一句话
map()
是对一个向量/矩阵中每个元素同时进行某一操作返回值与原向量维度相同; reduce()
是对向量/矩阵一个序列中的元素依次进行某一操作,返回一个数值。
实例
map()
a = [2,4,6,8]
print map(lambda x:2*x, a)
结果为:[4, 8, 12, 16] map()
函数的操作,是对向量a
中的每个元素,都做
reduce()
a = [1,2,3,4]
print reduce(lambda x,y:x+y,a)
结果为:20 reduce()
函数的操作,从第一个元素开始,将每个元素累加。换句话说,
step1:第一个元素与第二个元素相加:
step2:step1结果和第三个元素相加:
step3:step2结果和第四个元素相加:
拓展
tf.reduce_mean()
这是tensorflow官方文档tf.reduce_mean()
函数的说明
reduce_mean(input_tensor,reduction_indices=None,keep_dims=False, name=None)
input_tensor: The tensor to reduce. Should have numeric type.
reduction_indices: The dimensions to reduce. IfNone
(the defaut), reduces all dimensions.
# x is [[1., 1. ]]
# [2., 2.]]
tf.reduce_mean(x) ==> 1.5
tf.reduce_mean(x, 0) ==> [1.5, 1.5]
tf.reduce_mean(x, 1) ==> [1., 2.]
input_tensor
是输入的张量。 redunction_indices
进行求平均操作的维度,默认为对张量中所有元素求平均
因此不指定参数时,输出值为
指定indice为0时,对每一列中的所有元素求平均,输出结果为
指定indice为1时,对每一行中的所有元素求平均,输出结果为
- python中map()和reduce()函数的区别
- python中map,filter和reduce函数区别
- python的reduce函数和map函数
- python中map和reduce函数的学习
- Python 中 map,reduce,和filter 内置函数的方法
- python之map和reduce的区别
- python的map和reduce函数
- python中map和reduce的应用
- Python 中 map和reduce 的用法
- Python函数Map()和reduce()的区别和第二天学习Python的心得体会
- python map()和reduce()函数
- Python---map和reduce函数
- Python的map/reduce函数
- python中的高阶函数map(),reduce(),filter()的区别
- python中的zip与map和filter与reduce函数的区别
- python中map()函数和reduce()函数的使用
- Python中的map()函数和reduce()函数的用法
- Python中的map()函数和reduce()函数的用法
- Parameter index out of range (1 > number of parameters, which is 0).
- java内存区域划分及原理。网上总结。
- 通达oa破解版/通达oa破解补丁/通达oa破解补丁下载
- jQuery Mobile中链接(包含button和表单提交的链接)的data-*选项
- 欢迎使用CSDN-markdown编辑器
- python中map()和reduce()函数的区别
- Linux 磁盘管理
- Postman用法简介
- /etc/profile和$HOME/.bash_profile
- 理解Python的With语句
- 简单数论总结
- Java:单例模式的七种写法
- 通达OA应对后门检测的临时方法
- SVN错误:Attempted to lock an already-locked dir