我最近在新的Python 3.6.3安装上使用pip
安装了Tensorflow。
以下代码:
import tensorflow as tf
msgconst = tf.Constant("henlo wrlod lol <4")
thesession = tf.Session()
print(thesession.run(msgconst))
thesession.close()
给出错误
Traceback (most recent call last):
File "C:/Users/[my username]/PycharmProjects/TFlowTest/aergaetgaertgasrtg.py", line 3, in <module>
msgconst = tf.Constant("henlo wrlod lol <4")
AttributeError: module 'tensorflow' has no attribute 'Constant'
问题不在于有另一个名为tensorflow
的文件正在导入而不是真正的库;其他一切都存在。我可以做得很好:
import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
print(tf.test.is_built_with_cuda())
print(tf.test.gpu_device_name())
给
True
2017-12-06 21:03:22.807800: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2017-12-06 21:03:23.147288: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1030] Found device 0 with properties:
name: GeForce GTX 980M major: 5 minor: 2 memoryClockRate(GHz): 1.1265
pciBusID: 0000:01:00.0
totalMemory: 4.00GiB freeMemory: 3.32GiB
2017-12-06 21:03:23.147553: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 980M, pci bus id: 0000:01:00.0, compute capability: 5.2)
/device:GPU:0
Process finished with exit code 0
此问题在卸载和重新安装Tensorflow后仍然存在;这是在安装它时打印的pip
C:\Program Files\Python36-64\Scripts>pip install tensorflow-gpu --no-cache-dir
Collecting tensorflow-gpu
Downloading tensorflow_gpu-1.4.0-cp36-cp36m-win_amd64.whl (67.6MB)
100% |████████████████████████████████| 67.6MB ...
Requirement already satisfied: numpy>=1.12.1 in c:\program files\python36-64\lib\site-packages (from tensorflow-gpu)
Requirement already satisfied: wheel>=0.26 in c:\program files\python36-64\lib\site-packages (from tensorflow-gpu)
Requirement already satisfied: six>=1.10.0 in c:\program files\python36-64\lib\site-packages (from tensorflow-gpu)
Requirement already satisfied: enum34>=1.1.6 in c:\program files\python36-64\lib\site-packages (from tensorflow-gpu)
Requirement already satisfied: tensorflow-tensorboard<0.5.0,>=0.4.0rc1 in c:\program files\python36-64\lib\site-packages (from tensorflow-gpu)
Requirement already satisfied: protobuf>=3.3.0 in c:\program files\python36-64\lib\site-packages (from tensorflow-gpu)
Requirement already satisfied: html5lib==0.9999999 in c:\program files\python36-64\lib\site-packages (from tensorflow-tensorboard<0.5.0,>=0.4.0rc1->tensorflow-gpu)
Requirement already satisfied: werkzeug>=0.11.10 in c:\program files\python36-64\lib\site-packages (from tensorflow-tensorboard<0.5.0,>=0.4.0rc1->tensorflow-gpu)
Requirement already satisfied: markdown>=2.6.8 in c:\program files\python36-64\lib\site-packages (from tensorflow-tensorboard<0.5.0,>=0.4.0rc1->tensorflow-gpu)
Requirement already satisfied: bleach==1.5.0 in c:\program files\python36-64\lib\site-packages (from tensorflow-tensorboard<0.5.0,>=0.4.0rc1->tensorflow-gpu)
Requirement already satisfied: setuptools in c:\program files\python36-64\lib\site-packages (from protobuf>=3.3.0->tensorflow-gpu)
Installing collected packages: tensorflow-gpu
Successfully installed tensorflow-gpu-1.4.0
有什么建议吗?
答案 0 :(得分:2)
你会讨厌听到这个,但它是
tf.constant(
不
tf.Constant(
(请注意,第一个没有大写。)
我刚刚在我的环境中使用了您的代码来注意这一点,所以我可以说我确实在您的代码中遇到了问题,但是当使用小案例constant
时,它确实有效。