gpt4 book ai didi

haskell - Haskell奇怪的 `zip`错误

转载 作者:行者123 更新时间:2023-12-02 11:08:20 24 4
gpt4 key购买 nike

我正在尝试压缩两个[[Float]]数组,然后对每个元素进行一次计算。我认为生成的压缩数组将具有[([Float], [Float])]类型。

但是在Haskell告诉我的地方,我不断出错:

[1 of 1] Compiling Mod               ( Mod.hs, interpreted )

NN.hs:38:42:
Couldn't match type ‘[Float]’ with ‘Float’
Expected type: [Float]
Actual type: [[Float]]
In the second argument of ‘zip’, namely ‘bs’
In the second argument of ‘map’, namely ‘(zip ws bs)’
Failed, modules loaded: none.

我似乎无法弄清楚MCVE,因为如果(kinda/sorta)试图这样做,那会是什么:
feedforward :: ([[Float]], [[Float]]) -> Float -> [Float]
feedforward (ws, bs) a = map (equation a) (zip ws bs)
where equation a (w, b) = (sum $ zipWith (*) w b) + a

但这实际上可以编译!而我的真实代码如下所示:
data NNetwork = NNetwork { nlayers :: Int
, sizes :: [Int]
, biases :: [[Float]]
, weights :: [[Float]] }
deriving (Show, Ord, Eq)

toFloat x = fromIntegral x :: Float

sig :: Float -> Float
sig a = 1 / (1 + exp (-a))


feedforward :: NNetwork -> Float -> [Float]
feedforward net a = map (eq22 a) (zip ws bs)
where bs = biases net
ws = weights net
dot a b = sum $ zipWith (*) a b
rep a b = replicate (length a) b
eq22 a (w, b) = sig $ (dot w (rep w a)) + b

但是 无效!

- 提前致谢

最佳答案

问题是:

eq22 :: Float -> ([Float], [Float]) ->  Float
eq22 a (w, b) = sig $ (dot w (rep w a)) + b

除了在这种情况下 b[Float]并用作 Float(请参阅 + b)。您是说要通过 b来实现某种功能吗?

同样,这些不是数组,而是列表。

关于haskell - Haskell奇怪的 `zip`错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32750414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com