[英]Python Numpy: How does numpy.exp(x) coerce the return value of this sigmoid function into an ndarray?
I'm looking at a preliminary neural network example using the numpy package in python, and I am a little confused about what appears to be some magical type coercion. 我正在看一个使用python中的numpy包的初步神经网络示例,对于似乎是某种魔术类型强制的东西我有些困惑。
Take this sigmoid function as reference, it uses numpy's numpy.exp(x)
to calculate e to the power of x: 以这个sigmoid函数为参考,它使用numpy的
numpy.exp(x)
来计算e的x的幂:
def sigmoid(z):
return 1.0 / (1.0 + numpy.exp(-z))
This is very straight forward - it looks like it should return a float, and indeed does so when fed a float or integer as argument: 这非常简单-看起来应该返回浮点数,并且在以浮点数或整数作为参数时确实如此:
sigmoid(0) # => 0.5
What is strange to me is that (according to some footnotes on numpy) when passed a numpy ndarray this function will return a numpy ndarray with the sigmoid function applied to each element: 对我来说奇怪的是(根据numpy的一些脚注),当传递numpy ndarray时,此函数将返回一个numpy ndarray,并将sigmoid函数应用于每个元素:
sigmoid(numpy.random.randn(2,2)) # => 2x2 ndarray
This makes very little sense to me as the function clearly returns a float divided by a statement containing the numpy functions. 这对我来说意义不大,因为该函数明显返回一个浮点数除以包含numpy函数的语句。 How can numpy coerce the return value like this?
numpy如何像这样强制返回值? How is this even possible?
这怎么可能?
Is their something very strange about python functions that I'm missing? 他们对我缺少的python函数感到很奇怪吗? Can numpy somehow change the return property of the function object it executes inside of?
numpy是否可以以某种方式更改它在其中执行的函数对象的return属性?
Given a scalar, your function returns a float wrapped in a numpy
type: 给定一个标量,您的函数将返回一个以
numpy
类型包装的浮点数:
In [136]: sigmoid(0)
Out[136]: 0.5
In [137]: type(_)
Out[137]: numpy.float64
Given an array, it returns an array of the same shape (but float dtype): 给定一个数组,它将返回一个相同形状的数组(但为float dtype):
In [140]: sigmoid(np.arange(3))
Out[140]: array([0.5 , 0.73105858, 0.88079708])
np.exp
does that. np.exp
做到了。
The +
and /
produce a numpy
object if one of the arguments is an numpy
object. 的
+
和/
产生numpy
对象,如果其中一个参数是一个numpy
对象。 The interpreter translates the +
into a call to __add__
or __radd__
. 解释器将
+
转换为对__add__
或__radd__
的调用。 There are various rules when the arguments are different types, but in most cases, if either argument is numpy
array, the calculation is controlled by the numpy
version of __add__
. 当参数为不同类型时有多种规则,但是在大多数情况下,如果其中一个参数为
numpy
数组,则计算由__add__
的numpy
版本__add__
。
So in 1.0 / (1.0 + numpy.exp(-z))
, the np.exp
produces an array, and the rest of the calculation follows suit. 因此,在
1.0 / (1.0 + numpy.exp(-z))
, np.exp
会生成一个数组,其余的计算也照做。
And the normal behavior of math calculations involving numpy
arrays is to apply the calculation to each element of the array(s). 涉及
numpy
数组的数学计算的正常行为是将计算应用于数组的每个元素。 We say they operate element-wise. 我们说他们在元素方面运作。 It gets more complicated when working with several arrays that differ in
shape
; 当使用多个
shape
不同的数组时,它变得更加复杂; then broadcasting
rules apply. 然后适用
broadcasting
规则。
There are also numpy
operations that combine elements of an array, eg np.sum
, and ones that perform more complication combinations like matrix multiplication. 还有一些
numpy
操作,它们结合数组的元素(例如np.sum
)和执行更多复杂组合(例如矩阵乘法)的操作。
The math
module version of exp
only works with Python scalars: exp
的math
模块版本仅适用于Python标量:
In [141]: import math
In [142]: 1.0 / (1.0 + math.exp(-0))
Out[142]: 0.5
In [143]: type(_)
Out[143]: float
In [144]: 1.0 / (1.0 + math.exp(-np.arange(3)))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-144-fa5530a5ba95> in <module>()
----> 1 1.0 / (1.0 + math.exp(-np.arange(3)))
TypeError: only size-1 arrays can be converted to Python scalars
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.