TensorFlow 2.0 の Variable
を ndarray
に変換するには tensorflow.Variable.numpy()
を使います。
import tensorflow
x = tensorflow.Variable(tensorflow.random.uniform([4, 4]))
print(x)
y = x.numpy()
print(y)
$ # macOS Mojave (10.14) + TensorFlow 2.0.0b1
$ python3 sample.py
<tf.Variable 'Variable:0' shape=(4, 4) dtype=float32, numpy=
array([[0.4963708 , 0.5265337 , 0.645676 , 0.36317182],
[0.88785803, 0.6770747 , 0.34829807, 0.7676312 ],
[0.64356697, 0.1472162 , 0.6687521 , 0.6949041 ],
[0.9588698 , 0.0772624 , 0.69979465, 0.9192028 ]], dtype=float32)>
[[0.4963708 0.5265337 0.645676 0.36317182]
[0.88785803 0.6770747 0.34829807 0.7676312 ]
[0.64356697 0.1472162 0.6687521 0.6949041 ]
[0.9588698 0.0772624 0.69979465 0.9192028 ]]
参考資料
-
TensorFlow 2.0 ベータ版
https://www.tensorflow.org/beta?hl=ja -
初心者のための TensorFlow 2.0 入門
https://www.tensorflow.org/beta/tutorials/quickstart/beginner?hl=ja -
Effective TensorFlow 2.0
https://www.tensorflow.org/beta/guide/effective_tf2