From 2a1e4d291c4809314951313b764675f68eb6da8f Mon Sep 17 00:00:00 2001 From: Saransh Chopra Date: Tue, 11 Oct 2022 20:49:27 +0530 Subject: [PATCH] Back to create_bias --- src/layers/basic.jl | 6 +++--- src/layers/conv.jl | 6 +++--- src/utils.jl | 9 +++------ 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/layers/basic.jl b/src/layers/basic.jl index 51c5fda9b1..42813cb5f7 100644 --- a/src/layers/basic.jl +++ b/src/layers/basic.jl @@ -155,7 +155,7 @@ struct Dense{F, M<:AbstractMatrix, B} bias::B σ::F function Dense(W::M, bias = true, σ::F = identity) where {M<:AbstractMatrix, F} - b = _create_bias(W, bias, size(W,1)) + b = create_bias(W, bias, size(W,1)) new{F,M,typeof(b)}(W, b, σ) end end @@ -228,7 +228,7 @@ struct Scale{F, A<:AbstractArray, B} bias::B σ::F function Scale(scale::A, bias::B = true, σ::F = identity) where {A<:AbstractArray, B<:Union{Bool, AbstractArray}, F} - b = _create_bias(scale, bias, size(scale)...) + b = create_bias(scale, bias, size(scale)...) new{F, A, typeof(b)}(scale, b, σ) end end @@ -403,7 +403,7 @@ struct Bilinear{F,A,B} σ::F function Bilinear(W::A, bias = true, σ::F = identity) where {A<:AbstractArray, F} ndims(A) == 3 || throw(ArgumentError("expected a 3-array of weights")) - b = _create_bias(W, bias, size(W,1)) + b = create_bias(W, bias, size(W,1)) new{F,A,typeof(b)}(W, b, σ) end end diff --git a/src/layers/conv.jl b/src/layers/conv.jl index 36aa5c8430..003395c15d 100644 --- a/src/layers/conv.jl +++ b/src/layers/conv.jl @@ -156,7 +156,7 @@ function Conv(w::AbstractArray{T,N}, b = true, σ = identity; stride = expand(Val(N-2), stride) dilation = expand(Val(N-2), dilation) pad = calc_padding(Conv, pad, size(w)[1:N-2], dilation, stride) - bias = _create_bias(w, b, size(w, N)) + bias = create_bias(w, b, size(w, N)) return Conv(σ, w, bias, stride, pad, dilation, groups) end @@ -293,7 +293,7 @@ function ConvTranspose(w::AbstractArray{T,N}, bias = true, σ = identity; stride = expand(Val(N-2), stride) dilation = expand(Val(N-2), dilation) pad = calc_padding(ConvTranspose, pad, size(w)[1:N-2], dilation, stride) - b = _create_bias(w, bias, size(w, N-1) * groups) + b = create_bias(w, bias, size(w, N-1) * groups) return ConvTranspose(σ, w, b, stride, pad, dilation, groups) end @@ -441,7 +441,7 @@ function CrossCor(w::AbstractArray{T,N}, bias = true, σ = identity; stride = expand(Val(N-2), stride) dilation = expand(Val(N-2), dilation) pad = calc_padding(CrossCor, pad, size(w)[1:N-2], dilation, stride) - b = _create_bias(w, bias, size(w, N)) + b = create_bias(w, bias, size(w, N)) return CrossCor(σ, w, b, stride, pad, dilation) end diff --git a/src/utils.jl b/src/utils.jl index d2e0206d4f..10ea2982b5 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -504,7 +504,7 @@ randn32(rng::AbstractRNG, dims::Integer...) = Base.randn(rng, Float32, dims...) randn32(rng::AbstractRNG) = (dims...,) -> Base.randn(rng, Float32, dims...) """ - _create_bias(weights, bias, size...) + create_bias(weights, bias, size...) Return a bias parameter for a layer, based on the value given to the constructor's keyword `bias=bias`. @@ -514,17 +514,14 @@ to the constructor's keyword `bias=bias`. * `bias::AbstractArray` uses the array provided, provided it has the correct size. It does not at present correct the `eltype` to match that of `weights`. """ -function _create_bias(weights::AbstractArray, bias::Bool, dims::Integer...) +function create_bias(weights::AbstractArray, bias::Bool, dims::Integer...) bias ? fill!(similar(weights, dims...), 0) : false end -function _create_bias(weights::AbstractArray, bias::AbstractArray, dims::Integer...) +function create_bias(weights::AbstractArray, bias::AbstractArray, dims::Integer...) size(bias) == dims || throw(DimensionMismatch("expected bias of size $(dims), got size $(size(bias))")) bias end -# TODO figure out whether we want to document or deprecate this -const create_bias = _create_bias - # Other