用于矩阵向量加法的 Julia 中的双重广播?

Double Broadcasting in Julia for Matrix Vector Addition?

这里是新手 Julia 的问题。给定两个数组,

W = [randn(3,2), randn(3,2)]
b = [randn(3), randn(3)]

我想做一个“嵌套广播”,

W .+ b = [W[1].+b[1], W[2].+b[2]]

到目前为止我能想到的最好的是,

[Wi.+bi (Wi,bi) for zip(W,b)]

来自 Python 的背景,这感觉有点亵渎神灵。在 Julia 中有更好的方法吗?

您可以执行以下操作:

julia> W = [randn(3,2), randn(3,2)]
2-element Array{Array{Float64,2},1}:
 [0.39179718902868116 -0.5387622679356612; -0.594274465053327 0.018804631512093436; -2.273706742420988 -0.4638617400026042]
 [0.3249960563405678 -0.4877554417492699; 0.5036437919340767 1.3172770503034696; 0.03501532820428975 -0.2675024677340758]

julia> b = [randn(3), randn(3)]
2-element Array{Array{Float64,1},1}:
 [1.2571527266220441, 0.21599608118129476, 0.21498843153804936]
 [-0.528960345932853, 0.5610435189953311, -0.8636370930615718]

# A helper function which broadcasts once
julia> s_(Wi, bi) = Wi .+ bi
s_ (generic function with 1 method)

# Broadcast the helper function
julia> s_.(W, b)
2-element Array{Array{Float64,2},1}:
 [1.6489499156507252 0.718390458686383; -0.3782783838720323 0.2348007126933882; -2.0587183108829388 -0.24887330846455485]
 [-0.20396428959228524 -1.016715787682123; 1.0646873109294077 1.8783205692988008; -0.828621764857282 -1.1311395607956476]

如评论中所述,您可以使用可用的 user-definable infix operators 之一来命名辅助函数,这样可以使用更好的语法(下面使用的特定符号可以通过键入 \oplus然后 Tab):

julia> ⊕(x, y) = x .+ y
⊕ (generic function with 1 method)

julia> W .⊕ b
2-element Array{Array{Float64,2},1}:
 [1.6489499156507252 0.718390458686383; -0.3782783838720323 0.2348007126933882; -2.0587183108829388 -0.24887330846455485]
 [-0.20396428959228524 -1.016715787682123; 1.0646873109294077 1.8783205692988008; -0.828621764857282 -1.1311395607956476]


或者 - 再次如评论中所述 - 如果您不想明确声明辅助函数:

julia> ((Wi, bi)->Wi.+bi).(W, b)
2-element Array{Array{Float64,2},1}:
 [1.6489499156507252 0.718390458686383; -0.3782783838720323 0.2348007126933882; -2.0587183108829388 -0.24887330846455485]
 [-0.20396428959228524 -1.016715787682123; 1.0646873109294077 1.8783205692988008; -0.828621764857282 -1.1311395607956476]

(我个人更难阅读最后一个版本,但是 YMMV)

想播广播就播broadcast:

julia> broadcast.(+, W, b)
2-element Array{Array{Float64,2},1}:
 [-0.7364111670769904 0.010994354421031916; -0.9010128415786036 0.22868802910609998; 1.2030371118617933 0.21305414210853912]
 [0.19183885867446926 0.5362077496502086; 1.5909421118115665 0.1235808501390212; 1.5190965380769597 0.1883638848487652]     

julia> [W[1].+b[1], W[2].+b[2]]
2-element Array{Array{Float64,2},1}:
 [-0.7364111670769904 0.010994354421031916; -0.9010128415786036 0.22868802910609998; 1.2030371118617933 0.21305414210853912]
 [0.19183885867446926 0.5362077496502086; 1.5909421118115665 0.1235808501390212; 1.5190965380769597 0.1883638848487652]