# Converting PyObject to a Julia array

**URL:** https://discourse.julialang.org/t/converting-pyobject-to-a-julia-array/18445
**Category:** Performance
**Created:** [December 8, 2018, 12:42am UTC](https://discourse.julialang.org/t/converting-pyobject-to-a-julia-array/18445 "2018-12-08T00:42:24Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![monty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/monty/32/44234_2.png) [@monty](https://discourse.julialang.org/u/monty)
#### Post date: [December 8, 2018, 12:42am UTC](https://discourse.julialang.org/t/converting-pyobject-to-a-julia-array/18445/1 "2018-12-08T00:42:24Z")

</div>

How to convert PyObject into a Julia array

```julia
import PyCall
tf = PyCall.pyimport("tensorflow")
o = tf[:random_normal]((3,3))
PyObject <tf.Tensor: id=27, shape=(3, 3), dtype=float32, numpy=
array([[0.02705349, 0.3173014 , 0.48765275],
       [0.03795605, 0.7796892 , 0.01696282],
       [0.9305789 , 0.3821437 , 0.26405492]], dtype=float32)>

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [December 8, 2018, 12:56am UTC](https://discourse.julialang.org/t/converting-pyobject-to-a-julia-array/18445/2 "2018-12-08T00:56:56Z")

</div>

Friendly nudge: [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [December 8, 2018, 1:22am UTC](https://discourse.julialang.org/t/converting-pyobject-to-a-julia-array/18445/3 "2018-12-08T01:22:28Z")

</div>

Convert it to a numpy array [python - Convert a tensor to numpy array in Tensorflow? - Stack Overflow](https://stackoverflow.com/questions/34097281/how-can-i-convert-a-tensor-into-a-numpy-array-in-tensorflow)

PyCall then automatically converts it to a Julia’s `Array` (provided that element type can be converted).

---

<div class="post-metadata">

### Author: ![monty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/monty/32/44234_2.png) [@monty](https://discourse.julialang.org/u/monty)
#### Post date: [December 8, 2018, 1:51am UTC](https://discourse.julialang.org/t/converting-pyobject-to-a-julia-array/18445/4 "2018-12-08T01:51:18Z")

</div>

it does not seem to work:

```julia
import PyCall
tf = PyCall.pyimport("tensorflow")
o = tf[:random_normal]((3,3))[:eval]
PyObject <tf.Tensor: id=27, shape=(3, 3), dtype=float32, numpy=
array([[0.02705349, 0.3173014 , 0.48765275],
       [0.03795605, 0.7796892 , 0.01696282],
       [0.9305789 , 0.3821437 , 0.26405492]], dtype=float32)>

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [December 8, 2018, 1:56am UTC](https://discourse.julialang.org/t/converting-pyobject-to-a-julia-array/18445/5 "2018-12-08T01:56:13Z")

</div>

Don’t you need to execute the `eval` function as well?

---

<div class="post-metadata">

### Author: ![monty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/monty/32/44234_2.png) [@monty](https://discourse.julialang.org/u/monty)
#### Post date: [December 8, 2018, 3:36pm UTC](https://discourse.julialang.org/t/converting-pyobject-to-a-julia-array/18445/6 "2018-12-08T15:36:59Z")

</div>

actually this is what works:

```julia
o = tf[:random_normal]((3,3))[:numpy]()
3×3 Array{Float32,2}:
 -1.35462 -0.0406312 -0.993727
 -0.8862 1.15086 -1.85081
  0.461948 -1.41717 -0.993305

```

Thank you very much!

---

<div class="post-metadata">

### Author: ![wookyoung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wookyoung/32/157_2.png) [@wookyoung](https://discourse.julialang.org/u/wookyoung)
#### Post date: [December 9, 2018, 1:36am UTC](https://discourse.julialang.org/t/converting-pyobject-to-a-julia-array/18445/7 "2018-12-09T01:36:46Z")

</div>

I found the answer from the issue on the TensorFlow-Examples.  
[https://github.com/aymericdamien/TensorFlow-Examples/issues/40#issuecomment-405892316](https://github.com/aymericdamien/TensorFlow-Examples/issues/40#issuecomment-405892316)

so using PyCall in that way.

```julia
using PyCall
tf = pyimport("tensorflow")
o = tf[:random_normal]((3,3))
@pyimport tensorflow.python.keras.backend as K
sess = K.get_session()
array = sess[:run](o)

```

and by adding `Base.getproperty` to `PyObject`.

```julia
Base.getproperty(o::PyObject, name::Symbol) = (:o == name ? getfield : getindex)(o, name)

array = sess.run(o)

```

now it gets more python-like code.
