Skip to main content
 首页 » 编程设计

python之这段 Haskell 代码是否等同于这段 Python 代码

2025年05月04日32leader

我正在尝试将 Python 程序移植到 Haskell,而我对 NumPy(Python 程序使用的)还很陌生,所以我想知道为什么这段代码不等价。 这是我的 Haskell 代码:

data NNetwork = NNetwork { nlayers :: Int 
                         , sizes   :: [Int] 
                         , biases  :: [[Float]] 
                         , weights :: [[Float]] } 
                deriving (Show, Ord, Eq) 
 
buildNetwork :: [Int] -> NNetwork 
buildNetwork sizes = NNetwork { nlayers = length sizes 
                              , sizes   = sizes 
                              , biases  = map (\y -> replicate y (sig . toFloat . rands $ y)) sizes 
                              , weights = map (\y -> replicate y (toFloat $ rands y)) sizes } 
feedforward :: NNetwork -> Float -> [[Float]] 
feedforward net a = map (equation a) (zip (weights net) (biases net)) 
 
toFloat x = fromIntegral x :: Float 
 
sig :: Float -> Float 
sig a = 1 / (1 + exp (-a)) 
 
rands :: Int -> Int 
rands x = (7 * x) `mod` 11 
 
equation :: Float -> ([Float], [Float]) -> [Float] 
equation a (w, b) = map sig $ zipWith (+) (dot w (rep w a)) b 
  where dot = zipWith (*) 
        rep a b = replicate (length a) b 

和原始的 Python 代码:

class Network(object): 
 
    def __init__(self, sizes): 
        self.num_layers = len(sizes) 
        self.sizes = sizes 
        self.biases = [np.random.randn(y, 1) for y in sizes[1:]] 
        self.weights = [np.random.randn(y, x)  
                        for x, y in zip(sizes[:-1], sizes[1:])] 
 
def sigmoid(z): 
    return 1.0/(1.0+np.exp(-z)) 
 
def feedforward(self, a): 
        """Return the output of the network if "a" is input.""" 
        for b, w in zip(self.biases, self.weights): 
            a = sigmoid(np.dot(w, a)+b) 
        return a 

我正在尝试将一个非常简单的神经网络程序从 Python 移植到 Haskell,因为我更喜欢 Haskell。我也担心我做错了什么,因为 Haskell 代码要冗长得多。

- 谢谢!

请您参考如下方法:

首先:注意 Python 版本缺少 deriving (Show, Eq, Ord) 的等价物— 尝试实现相应的 __magic__方法并查看添加了多少行代码。没有这些,== , <= , > ,以及 print Network()几乎没有意义。

基本上冗长主要来自类型签名。另外,你可以移动rands到 where在 buildNetwork 下屏蔽并完全摆脱 toFloat通过替换对 toFloat 的任何调用与 fromIntegral没有类型注释。再加上一些其他微小的重构。

一般来说,在某些情况下,您可以预料到某些事情在通常更为简洁的语言中会更加冗长。我敢肯定,随着你的神经网络程序朝着更丰富的代码库发展,Haskell 将比 Python 更简洁,忽略了可能存在的 Python 神经网络库可能比它们的(可能不存在的)Haskell 对应物更成熟.

data NNetwork = NNetwork { nlayers :: Int 
                         , sizes   :: [Int] 
                         , biases  :: [[Float]] 
                         , weights :: [[Float]] } 
                deriving (Show, Ord, Eq) 
 
buildNetwork sizes = 
  NNetwork { nlayers = length sizes 
           , sizes   = sizes 
           , biases  = map (\y -> replicate y (sig . fromIntegral . rands $ y)) sizes 
           , weights = map (\y -> replicate y (fromIntegral . rands $ y)) sizes } 
  where rands x = (7 * x) `mod` 11 
 
feedforward net a = map (equation a) (zip (weights net) (biases net)) 
 
sig a = 1 / (1 + exp (-a)) 
 
equation a (w, b) = map sig $ zipWith (+) (dot w rep) b 
  where dot = zipWith (*) 
        rep = replicate (length w) a 

而且您可以在 buildNetwork 中进行一些微重构。删除一些小的重复,但这只会缩短代码行,并可能降低代码对领域专家的可读性:

buildNetwork sizes = 
  NNetwork { nlayers = length sizes 
           , sizes   = sizes 
           , biases  = nameMe sig 
           , weights = nameMe id } 
  where nameMe fn = map (replicate y (fn y')) sizes 
        y'        = fromIntegral $ y * 7 `mod` 11