python和Keras.backend常见函数

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## python和Keras.backend常见函数

ilufei2019 2018-05-18 12:05:00 浏览484

## python 常见函数(v3.0)

#### lambda 匿名函数

``````# lambda <params>:<result>
func = lambda x, y: x + y
print(func(1, 2))
# 输出 3
``````

#### map 函数

``````# map(func, sequnce[, sequnce,....]) -> iterator
>> list(map(lambda x: x+2, [1,2,3]))
[3, 4, 5]
>> list(map(pow, [1,2,3], [2,3,4]))
[1, 8, 81]
``````

#### filter 函数

``````# filter(func or None, sequence) -> iterator
>> list(filter((lambda x: x>0),range(-5,5)))
[1,2,3,4]
>> list(filter(None,range(-5,5)))
[-5, -4, -3, -2, -1, 1, 2, 3, 4]
``````

#### map 函数

``````# map(func, sequence)
>> map(lambda x: x*x*x, range(1, 11)
<map object at 0x7fafdf0d6978>
>> list(map(lambda x: x*x*x, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
``````

#### reduce 函数

``````# reduce(func, sequence, starting_value)
# starting_value 为初始调用值, 可为空或省略
>> reduce(lambda x, y: x + y, range(1, 11))
55
``````

``````import functools
functools.reduce(lambda x, y: x + y, range(1, 11))
``````

#### zip 函数

``````# zip([sequence, ...])
>> x = [1, 2, 3]; y = ['a', 'b', 'c']
>> list(zip(x, y))
[(1,'a'),(2,'b'),(3,'c')]
>> list(zip(*zip(x, y)))
[(1,2,3),('a','b','c')]
# 长度不一样
>> x = [1, 2, 3]; y = ['a', 'b', 'c', 'd']
>> list(zip(x, y))
[(1,'a'),(2,'b'),(3,'c')]
>> list(zip(*zip(x, y)))
[(1,2,3),('a','b','c')]
``````

## keras 样本打散

``````# data_x 是 numpy.array 对象
indices = numpy.random.permutation(data_x.shape[0]) # shape[0]表示第0轴的长度，通常是训练数据的数量
rand_data_x = data_x[indices]
rand_data_y = data_y[indices] # data_y就是标记（label）
``````

ilufei2019
+ 关注