malmaud / TensorFlow.jl

A Julia wrapper for TensorFlow
Other
885 stars 109 forks source link

Broadcasting turns sideways, when dimensions have to be added #333

Open oxinabox opened 7 years ago

oxinabox commented 7 years ago

I'ld been wondering why I could never write networks with vector biases. Always had to use 2D column matrix basises. And now I understand. And it should have been obvious, because tensorflow is row major.

Anyway, the key point is that TensorFlow 1D tensors act like row-vectors when they are broadcast against matrixes. And julia vectors act-like column vectors when they are broadcast against matrixes. And this behavior is probably unexpected to julia users. Maybe more so that the other is unexpected to python tensorflow users? idk about that.

julia> using TensorFlow

julia> sess = Session()
Session(Ptr{Void} @0x00007f5423436c70)

julia> z_jl = zeros(2,2)
2×2 Array{Float64,2}:
 0.0  0.0
 0.0  0.0

julia> z = constant(z_jl)
<Tensor Const:1 shape=(2, 2) dtype=Float64>

julia> x_jl = [1.0, 0.0]
2-element Array{Float64,1}:
 1.0
 0.0

julia> x=constant(x_jl)
<Tensor Const_3:1 shape=(2) dtype=Float64>

julia> x_jl + z_jl
ERROR: DimensionMismatch("dimensions must match")
Stacktrace:
 [1] promote_shape(::Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}, ::Tuple{Base.OneTo{Int64}}) at ./indices.jl:84
 [2] promote_shape(::Tuple{Base.OneTo{Int64}}, ::Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}) at ./indices.jl:75
 [3] +(::Array{Float64,1}, ::Array{Float64,2}) at ./arraymath.jl:37

julia> x_jl .+ z_jl
2×2 Array{Float64,2}:
 1.0  1.0
 0.0  0.0

julia> run(sess, x + z) #normal addition is always broadcasting in TF
2×2 Array{Float64,2}:
 1.0  0.0
 1.0  0.0
oxinabox commented 5 years ago

This continues to annoy me.