TensorFlow.jl icon indicating copy to clipboard operation
TensorFlow.jl copied to clipboard

ones and ones_like

Open pevnak opened this issue 6 years ago • 4 comments

Hi All,

I would like to implement unsorted_segment_mean as described in https://github.com/tensorflow/tensorflow/issues/7389 but I am missing ones_like function. Is it exported?

I have tried to use similar from Julia and it is not overloaded.

Thanks for help. Tomas

pevnak avatar Sep 05 '17 10:09 pevnak

These operations are in python TensorFlow implemented in python, not in the C API. But they can be implemented in Julia too.

julia> using TensorFlow

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

julia> x = placeholder(Float32; shape=[3,-1])
<Tensor placeholder:1 shape=(3, ?) dtype=Float32>

julia> y=fill(constant(1f0), TensorFlow.shape(x))
<Tensor Fill:1 shape=unknown dtype=Float32>

Related, I think that maybe out use of Base.ones is currently off. I think it should match to that fill definition, not to a constant definition. (Which to me would make more sense as just the user calling constant(ones(size)), since constants are wrapped around julia types.

of course we could also overload: ones(::Type{Tensor}, ::AbstractTensor)=fill(..., and ones(::Type{Tensor}, ::Any)=constant(ones(...,

oxinabox avatar Sep 07 '17 06:09 oxinabox

Thanks. This is nice.

pevnak avatar Sep 07 '17 06:09 pevnak

+1 on the ones function using fill and not constant.

Besides for that, what are your thoughts on ones being overloaded such that it serves the purpose of ones and ones_like in TensorFlow. It seems like there's a balance to be struck here between being idiomatic with julia and accessible to TensorFlow users. I'm leaning towards only having ones.

Let me know what you think and I can get started on this.

@malmaud thoughts?

RatanRSur avatar Oct 04 '17 19:10 RatanRSur

Sorry for not getting back to you. I am undecided on this. Indeed there is a balance to be struck between idiomatic julia and accessible to TensorFlow users. In this case I lean very much towards idiomatic julia.

However, then the question becomes, what is idiomatic julia in this case?

I think ones(x::Tensor) for ones_like is idiomatic.

Is ones(::Type{Tensor}, x::Any) idiomatic? Here I am unsure.

Consider the 0.6 deprecations:

julia> ones(rand(3,3))
3×3 Array{Float64,2}:
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0

julia> ones(Int, rand(3,3))
WARNING: ones(T::Type, arr) is deprecated, use ones(T, size(arr)) instead.
Stacktrace:
 [1] depwarn(::String, ::Symbol) at ./deprecated.jl:64
 [2] ones(::Type{T} where T, ::Array{Float64,2}) at ./deprecated.jl:51
 [3] eval(::Module, ::Any) at ./boot.jl:235
 [4] eval_user_input(::Any, ::Base.REPL.REPLBackend) at ./REPL.jl:66
 [5] macro expansion at ./REPL.jl:97 [inlined]
 [6] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73
while loading no file, in expression starting on line 0
3×3 Array{Int64,2}:
 1  1  1
 1  1  1
 1  1  1

oxinabox avatar Oct 11 '17 04:10 oxinabox