LoginSignup
12
2

More than 3 years have passed since last update.

64日目 pip install tensorflowしたら2.0がやってきました。

Last updated at Posted at 2019-12-08

Telsorflowをインストールしたので、さっそく実行。
しかしエラーで動かない!?

$ python calc1.py 
2019-12-08 20:09:10.641892: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-12-08 20:09:10.667849: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fac4a3c0dc0 executing computations on platform Host. Devices:
2019-12-08 20:09:10.667893: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): Host, Default Version
Traceback (most recent call last):
  File "calc1.py", line 12, in <module>
    sess = tf.Session()
AttributeError: module 'tensorflow' has no attribute 'Session'

Sessionなんて知らないよ???

調べてみました。最近のTensorflowはSessionいらずになったらしい。

AttributeError: module 'tensorflow' has no attribute 'Session'について
https://ja.stackoverflow.com/questions/59780/attributeerror-module-tensorflow-has-no-attribute-session%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6

もしやと思ってバージョンを見てみたら・・・

$ pip list | grep tensorflow
tensorflow                         2.0.0              
tensorflow-estimator               2.0.1 

pip install tensolflowでインストールされたのは、tensolflow2.0.0 だったのでした。

参考にしたのは「Pythonによるスクレイピング&機械学習 開発テクニックBeautifulSoup、scikit-learn、TensorFlowを使ってみよう」https://www.socym.co.jp/support/s-1079

よくみたら、インストール時にconda install -c conda-forge tensorflow=1.1.0 とバージョンを指定していました。しまったなあ。

tensolflow2.0.0で動くように修正してみます。

calc1.py
import tensorflow as tf
a = tf.constant(1234)
b = tf.constant(5000)

add_op = a + b

#sess = tf.Session()
#res = sess.run(add_op) 
#print(res)

tf.print(add_op)

記述が簡単になったならいいんじゃないかな。

$ python calc1.py 
2019-12-08 20:16:15.480543: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-12-08 20:16:15.512089: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7ffedb3c6c60 executing computations on platform Host. Devices:
2019-12-08 20:16:15.512124: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): Host, Default Version
6234

結果は得られたけれど、足し算するのに数秒かかって、謎のメッセージがやたらでる。
このメッセージを調べてみると、pipではだいたいどのパソコンでも動くやつがインストールされるけど遅い。ソースからビルドすると各環境で最適化されるので速くなるかもしれない、とのこと。なるほど。あとで何とかしよう。
(30分)

12
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
2