繁体   English   中英

在Jupyter Notebook中导入TensorFlow时遇到麻烦,但在bash中运行良好

[英]Trouble with import TensorFlow in Jupyter Notebook while runs fine in bash

我在没有安装anaconda的情况下通过“使用本机pip安装”安装了张量流,并且能够在从Terminal启动的Python控制台中导入TensorFlow。 图片

$ python
Python 2.7.13 (default, Apr  4 2017, 08:47:57)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2017-04-30 20:54:36.278740: W     tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-04-30 20:54:36.278773: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-04-30 20:54:36.278785: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-04-30 20:54:36.278794: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2017-04-30 20:54:36.423246: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:865] OS X does not support NUMA - returning NUMA node zero
2017-04-30 20:54:36.432456: I     tensorflow/core/common_runtime/gpu/gpu_device.cc:887] Found device 0 with     properties:
name: GeForce GTX 1080
major: 6 minor: 1 memoryClockRate (GHz) 1.86
pciBusID 0000:01:00.0
Total memory: 8.00GiB
Free memory: 6.12GiB
2017-04-30 20:54:36.432507: I tensorflow/core/common_runtime/gpu/gpu_device.cc:908] DMA: 0
2017-04-30 20:54:36.432511: I tensorflow/core/common_runtime/gpu/gpu_device.cc:918] 0:   Y
2017-04-30 20:54:36.432519: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0)
>>> print(sess.run(hello))
Hello, TensorFlow!
>>>

但是我在Jupiter Notebook中运行了相同的代码,结果显示出来。 图片

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-d7933b52e0de> in <module>()
----> 1 import tensorflow as tf
      2 hello = tf.constant('Hello, TensorFlow!')
      3 sess = tf.Session()
      4 print(sess.run(hello))

ImportError: No module named tensorflow

我该如何解决这个问题?

Python,Tensorflow和Jupyter要么必须处于保存版本python2 / python3。 以下是使用python3设置jupyter和Tensorflow的命令

sudo apt install python3-pip
pip3 install --upgrade pip   ##optional
pip3 install jupyter


##Install Tensorflow
pip3 install --upgrade tensorflow

Test
$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>>

##Launch jupyter
jupyter notebook   
(In Ubuntu you may need to launch ~/.local/bin/jupyter-notebook until you relaunch the bash i.e. PATH issue)
##Create new notepad and run
import tensorflow as tf  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM