Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matmul not working for rank 3 tensors (batch_matmul) #452

Open
wilocw opened this issue Oct 16, 2018 · 0 comments
Open

matmul not working for rank 3 tensors (batch_matmul) #452

wilocw opened this issue Oct 16, 2018 · 0 comments

Comments

@wilocw
Copy link

wilocw commented Oct 16, 2018

When trying to apply matmul on rank 3 tensors, I get an error that the tensors must be rank 2. Using TensorFlow in python allows me to perform tensor multiplication, but I am unable to do it in Julia. This function is possible with batch_matmul but it throws the deprecation warning.

The following MWE is based on the tf.matmul documentation.

Python version

>>> import sys; print(sys.executable)
/home/wil/anaconda3/envs/PyCall.jl/bin/python
>>> import numpy as np
>>> import tensorflow as tf; print(tf.__version__)
1.10.0
>>> a = tf.constant(np.arange(1,13,dtype=np.int32), shape=[2,2,3])
>>> a
<tf.Tensor 'Const:0' shape=(2, 2, 3) dtype=int32>
>>> b = tf.constant(np.arange(13,25,dtype=np.int32),shape=[2,3,2])
>>> b
<tf.Tensor 'Const_1:0' shape=(2, 3, 2) dtype=int32>
>>> c = a@b # equiv to c = tf.matmul(a,b)
>>> c
<tf.Tensor 'matmul:0' shape=(2, 2, 2) dtype=int32>

Julia version

julia> ENV["PYTHON"]
"/home/wil/anaconda3/envs/PyCall.jl/bin/python"

julia> using TensorFlow

julia> a = constant(reshape(Int32.(1:12), 2,2,3))
<Tensor Const:1 shape=(2, 2, 3) dtype=Int32>

julia> b = constant(reshape(Int32.(13:24), 2,3,2))
<Tensor Const_2:1 shape=(2, 3, 2) dtype=Int32>

julia> a*b
ERROR: Tensorflow error: Status: Shape must be rank 2 but is rank 3 for 'MatMul' (op: 'MatMul') with input shapes: [2,2,3], [2,3,2].

Stacktrace:
 [1] check_status at /home/wil/.julia/packages/TensorFlow/A6TdG/src/core.jl:374 [inlined]
 [2] Operation(::TensorFlow.NodeDescription) at /home/wil/.julia/packages/TensorFlow/A6TdG/src/core.jl:1043
 [3] #mat_mul#96(::Nothing, ::Nothing, ::Nothing, ::Function, ::Tensor{Int32}, ::Tensor{Int32}) at /home/wil/.julia/packages/TensorFlow/A6TdG/src/ops/imported_ops.jl:683
 [4] mat_mul(::Tensor{Int32}, ::Tensor{Int32}) at /home/wil/.julia/packages/TensorFlow/A6TdG/src/ops/imported_ops.jl:668
 [5] #*#91(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::Tensor{Int32}, ::Tensor{Int32}) at /home/wil/.julia/packages/TensorFlow/A6TdG/src/ops.jl:34
 [6] *(::Tensor{Int32}, ::Tensor{Int32}) at /home/wil/.julia/packages/TensorFlow/A6TdG/src/ops.jl:34
 [7] top-level scope at none:0

julia> batch_matmul(a,b)
┌ Warning: batch_matmul is deprecated. Its functionality is now subsumed by matmul.
│   caller = top-level scope at none:0
└ @ Core none:0
<Tensor BatchMatMul_6:1 shape=(2, 2, 2) dtype=Int32>

Version Info

> tf_versioninfo()
----------------
Library Versions
----------------
Trying to evaluate ENV["TF_USE_GPU"] but got error: KeyError("TF_USE_GPU")
Trying to evaluate ENV["LIBTENSORFLOW"] but got error: KeyError("LIBTENSORFLOW")

tf_version(kind=:backend) = 1.10.0
Trying to evaluate tf_version(kind=:python) but got error: RemoteException(2, CapturedException(UndefVarError(:py_tf), Any[(#95 at TensorFlow.jl:183, 1), (#116 at process_messages.jl:276, 1), (run_work_thunk at process_messages.jl:56, 1), (run_work_thunk at process_messages.jl:65, 1), (#102 at task.jl:259, 1)]))
tf_version(kind=:julia) = 0.10.2

-------------
Python Status
-------------
PyCall.conda = true
ENV["PYTHON"] = /home/wil/anaconda3/envs/PyCall.jl/bin/python
PyCall.PYTHONHOME = /home/wil/anaconda3/envs/PyCall.jl:/home/wil/anaconda3/envs/PyCall.jl
String(read(#= /home/wil/.julia/packages/TensorFlow/A6TdG/src/version.jl:104 =# @cmd("pip --version"))) = pip 18.1 from /home/wil/anaconda3/lib/python3.6/site-packages/pip (python 3.6)

String(read(#= /home/wil/.julia/packages/TensorFlow/A6TdG/src/version.jl:105 =# @cmd("pip3 --version"))) = pip 18.1 from /home/wil/anaconda3/lib/python3.6/site-packages/pip (python 3.6)


------------
Julia Status
------------
Julia Version 1.0.1
Commit 0d713926f8 (2018-09-29 19:05 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: Intel(R) Core(TM) i5-7600 CPU @ 3.50GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, skylake)
Environment:
  JULIA_EDITOR = atom -a
  JULIA_NUM_THREADS = 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant