我正在努力学习tensorflow,并使用一本书来获得一些初步的见解。我目前正在尝试运行的代码也可以在这里找到:
我正在使用Windows 10
,python 3.9.7
和tensorflow 2.6.0
。我安装了conda在这里使用的所有软件包,并且已经尝试重新安装tensorflow,这并没有解决问题。
下面是给我错误的一行代码。
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# Define constants(hyperparameters) used by the algorithm
learning_rate = 0.01
training_epochs = 100
# Set up data to run the regression
x_train = np.linspace(-1, 1, 101)
y_train = 2 * x_train + np.random.randn(*x_train.shape) * 0.33
# Set up input and output variables
X = tf.constant(x_train, dtype=tf.float32)
Y = tf.constant(y_train, dtype=tf.float32)
w = tf.Variable(0.0, name="weights", dtype=tf.float32)
# Define the cost function
cost = lambda : tf.square(Y - tf.multiply(X, w))
# Define operaton for each iteration of the learning algorithm
train_op = tf.keras.optimizers.SGD(learning_rate)
在最后一行中,我得到以下错误
ModuleNotFoundError: No module named 'keras'
除了其他软件包之外,我还试着安装keras v2.6.0
。然而,这并没有帮助,据我所知,tensorflow在它的更新版本中附带了keras。所以我又把它移走了。
我认为我的tensorflow安装一定有问题,但我很困惑,因为我能够使用软件包的其他部分没有问题。我按照下面的说明安装了tensorflow:
https://anaconda.org/conda-forge/tensorflow。
我已经看过一条类似的线索。然而,其中一项评论中提供的链接对我没有任何帮助:
更新:
这是安装在环境中的所有软件包的列表(我使用了“conda”)。目前,keras
没有显示为单独的包,因为我再次删除了它:
(AI_Learning) C:\Users\Username>conda list
# packages in environment at F:\Programs\Anaconda\envs\AI_Learning:
#
# Name Version Build Channel
_r-mutex 1.0.0 anacondar_1
_tflow_select 2.3.0 mkl
abseil-cpp 20210324.2 h0e60522_0 conda-forge
absl-py 1.0.0 pyhd8ed1ab_0 conda-forge
aiohttp 3.8.1 py39hb82d6ee_0 conda-forge
aiosignal 1.2.0 pyhd8ed1ab_0 conda-forge
argon2-cffi 20.1.0 py39h2bbff1b_1
astor 0.8.1 pyh9f0ad1d_0 conda-forge
astunparse 1.6.3 pyhd8ed1ab_0 conda-forge
async-timeout 4.0.1 pyhd8ed1ab_0 conda-forge
async_generator 1.10 pyhd3eb1b0_0
attrs 21.2.0 pyhd3eb1b0_0
backcall 0.2.0 pyhd3eb1b0_0
blas 1.0 mkl
bleach 4.0.0 pyhd3eb1b0_0
blinker 1.4 py_1 conda-forge
bottleneck 1.3.2 py39h7cc1a96_1
brotli 1.0.9 ha925a31_2
brotlipy 0.7.0 py39h2bbff1b_1003
ca-certificates 2021.10.8 h5b45459_0 conda-forge
cached-property 1.5.2 hd8ed1ab_1 conda-forge
cached_property 1.5.2 pyha770c72_1 conda-forge
cachetools 4.2.4 pyhd8ed1ab_0 conda-forge
certifi 2021.10.8 py39hcbf5309_1 conda-forge
cffi 1.15.0 py39h2bbff1b_0
charset-normalizer 2.0.4 pyhd3eb1b0_0
click 8.0.3 pyhd3eb1b0_0
colorama 0.4.4 pyhd3eb1b0_0
cryptography 35.0.0 py39h71e12ea_0
cvxopt 1.2.7 py39h73f7814_1 conda-forge
cycler 0.11.0 pyhd3eb1b0_0
dataclasses 0.8 pyhc8e2a94_3 conda-forge
debugpy 1.5.1 py39hd77b12b_0
decorator 5.1.0 pyhd3eb1b0_0
defusedxml 0.7.1 pyhd3eb1b0_0
dsdp 5.8 h6e01ec9_1203 conda-forge
entrypoints 0.3 py39haa95532_0
fftw 3.3.10 nompi_h77347bd_102 conda-forge
flatbuffers 2.0.0 h0e60522_0 conda-forge
fonttools 4.25.0 pyhd3eb1b0_0
freeglut 3.0.0 h6538335_5
freetype 2.10.4 hd328e21_0
frozenlist 1.2.0 py39hb82d6ee_1 conda-forge
gast 0.4.0 pyh9f0ad1d_0 conda-forge
giflib 5.2.1 h8d14728_2 conda-forge
glpk 4.65 hdc00fd2_2
google-auth 1.35.0 pyh6c4a22f_0 conda-forge
google-auth-oauthlib 0.4.6 pyhd8ed1ab_0 conda-forge
google-pasta 0.2.0 pyh8c360ce_0 conda-forge
grpcio 1.38.1 py39hb76b349_0 conda-forge
gsl 2.7 hdfb1a43_0 conda-forge
h5py 3.6.0 nompi_py39hd4deaf1_100 conda-forge
hdf5 1.12.1 nompi_h2a0e4a3_100 conda-forge
hmmlearn 0.2.6 py39h5d4886f_1 conda-forge
icc_rt 2019.0.0 h0cc432a_1
icu 68.1 h6c2663c_0
idna 3.3 pyhd3eb1b0_0
importlib-metadata 4.8.2 py39haa95532_0
importlib_metadata 4.8.2 hd3eb1b0_0
intel-openmp 2021.4.0 haa95532_3556
ipykernel 6.4.1 py39haa95532_1
ipython 7.29.0 py39hd4e2768_0
ipython_genutils 0.2.0 pyhd3eb1b0_1
jasper 2.0.14 hea7d32e_2
jedi 0.18.0 py39haa95532_1
jinja2 3.0.2 pyhd3eb1b0_0
joblib 1.1.0 pyhd3eb1b0_0
jpeg 9d h2bbff1b_0
jsonschema 3.2.0 pyhd3eb1b0_2
jupyter_client 7.0.6 pyhd3eb1b0_0
jupyter_core 4.9.1 py39haa95532_0
jupyterlab_pygments 0.1.2 py_0
keras-preprocessing 1.1.2 pyhd8ed1ab_0 conda-forge
kiwisolver 1.3.1 py39hd77b12b_0
krb5 1.19.2 h20d022d_3 conda-forge
libblas 3.9.0 12_win64_mkl conda-forge
libcblas 3.9.0 12_win64_mkl conda-forge
libclang 11.1.0 default_h5c34c98_1 conda-forge
libcurl 7.79.1 h789b8ee_1 conda-forge
liblapack 3.9.0 12_win64_mkl conda-forge
liblapacke 3.9.0 12_win64_mkl conda-forge
libopencv 4.5.1 py39h27d8466_0 conda-forge
libpng 1.6.37 h2a8f88b_0
libprotobuf 3.14.0 h7755175_0 conda-forge
libssh2 1.10.0 h680486a_2 conda-forge
libtiff 4.2.0 hd0e1b90_0
libwebp 1.2.0 h2bbff1b_0
libwebp-base 1.2.0 h2bbff1b_0
lz4-c 1.9.3 h2bbff1b_1
m2w64-bwidget 1.9.10 2
m2w64-bzip2 1.0.6 6
m2w64-expat 2.1.1 2
m2w64-fftw 3.3.4 6
m2w64-flac 1.3.1 3
m2w64-gcc-libgfortran 5.3.0 6
m2w64-gcc-libs 5.3.0 7
m2w64-gcc-libs-core 5.3.0 7
m2w64-gettext 0.19.7 2
m2w64-gmp 6.1.0 2
m2w64-gsl 2.1 2
m2w64-libiconv 1.14 6
m2w64-libjpeg-turbo 1.4.2 3
m2w64-libogg 1.3.2 3
m2w64-libpng 1.6.21 2
m2w64-libsndfile 1.0.26 2
m2w64-libtiff 4.0.6 2
m2w64-libvorbis 1.3.5 2
m2w64-libwinpthread-git 5.0.0.4634.697f757 2
m2w64-libxml2 2.9.3 4
m2w64-mpfr 3.1.4 4
m2w64-openblas 0.2.19 1
m2w64-pcre 8.38 2
m2w64-speex 1.2rc2 3
m2w64-speexdsp 1.2rc3 3
m2w64-tcl 8.6.5 3
m2w64-tk 8.6.5 3
m2w64-tktable 2.10 5
m2w64-wineditline 2.101 5
m2w64-xz 5.2.2 2
m2w64-zlib 1.2.8 10
markdown 3.3.6 pyhd8ed1ab_0 conda-forge
markupsafe 2.0.1 py39h2bbff1b_0
matplotlib 3.5.0 py39haa95532_0
matplotlib-base 3.5.0 py39h6214cd6_0
matplotlib-inline 0.1.2 pyhd3eb1b0_2
mistune 0.8.4 py39h2bbff1b_1000
mkl 2021.4.0 h0e2418a_729 conda-forge
mkl-service 2.4.0 py39h2bbff1b_0
mkl_fft 1.3.1 py39h277e83a_0
mkl_random 1.2.2 py39hf11a4ad_0
msys2-conda-epoch 20160418 1
multidict 5.2.0 py39hb82d6ee_1 conda-forge
munkres 1.1.4 py_0
nbclient 0.5.3 pyhd3eb1b0_0
nbconvert 6.1.0 py39haa95532_0
nbformat 5.1.3 pyhd3eb1b0_0
nest-asyncio 1.5.1 pyhd3eb1b0_0
nltk 3.6.5 pyhd3eb1b0_0
notebook 6.4.6 py39haa95532_0
numexpr 2.7.3 py39hb80d3ca_1
numpy 1.21.2 py39hfca59bb_0
numpy-base 1.21.2 py39h0829f74_0
oauthlib 3.1.1 pyhd8ed1ab_0 conda-forge
olefile 0.46 pyhd3eb1b0_0
opencv 4.5.1 py39hcbf5309_0 conda-forge
openssl 1.1.1l h8ffe710_0 conda-forge
opt_einsum 3.3.0 pyhd8ed1ab_1 conda-forge
packaging 21.3 pyhd3eb1b0_0
pandas 1.3.4 py39h6214cd6_0
pandocfilters 1.4.3 py39haa95532_1
parso 0.8.2 pyhd3eb1b0_0
pickleshare 0.7.5 pyhd3eb1b0_1003
pillow 8.4.0 py39hd45dc43_0
pip 21.2.4 py39haa95532_0
prometheus_client 0.12.0 pyhd3eb1b0_0
prompt-toolkit 3.0.20 pyhd3eb1b0_0
protobuf 3.14.0 py39h415ef7b_1 conda-forge
py-opencv 4.5.1 py39h832f523_0 conda-forge
pyasn1 0.4.8 py_0 conda-forge
pyasn1-modules 0.2.7 py_0 conda-forge
pycparser 2.21 pyhd3eb1b0_0
pygments 2.10.0 pyhd3eb1b0_0
pyjwt 2.3.0 pyhd8ed1ab_1 conda-forge
pyopenssl 21.0.0 pyhd3eb1b0_1
pyparsing 3.0.4 pyhd3eb1b0_0
pyqt 5.12.3 py39hb0d2dfa_4 conda-forge
pyqt5-sip 4.19.18 pypi_0 pypi
pyqtchart 5.12 pypi_0 pypi
pyqtwebengine 5.12.1 pypi_0 pypi
pyrsistent 0.18.0 py39h196d8e1_0
pysocks 1.7.1 py39haa95532_0
python 3.9.7 h6244533_1
python-dateutil 2.8.2 pyhd3eb1b0_0
python-flatbuffers 1.12 pyhd8ed1ab_1 conda-forge
python_abi 3.9 2_cp39 conda-forge
pytz 2021.3 pyhd3eb1b0_0
pyu2f 0.1.5 pyhd8ed1ab_0 conda-forge
pywin32 228 py39hbaba5e8_1
pywinpty 0.5.7 py39haa95532_0
pyzmq 22.3.0 py39hd77b12b_2
qt 5.12.9 h5909a2a_4 conda-forge
r-base 3.5.1 hac0af64_1
r-timedate 3043.102 r351h6f4ce42_0
r-timeseries 3062.100 r35_0 conda-forge
regex 2021.8.3 py39h2bbff1b_0
requests 2.26.0 pyhd3eb1b0_0
requests-oauthlib 1.3.0 pyh9f0ad1d_0 conda-forge
rsa 4.8 pyhd8ed1ab_0 conda-forge
scikit-learn 1.0.1 py39hf11a4ad_0
scipy 1.7.1 py39hbe87c03_2
send2trash 1.8.0 pyhd3eb1b0_1
setuptools 58.0.4 py39haa95532_0
six 1.16.0 pyhd3eb1b0_0
snappy 1.1.8 ha925a31_3 conda-forge
sqlite 3.36.0 h2bbff1b_0
tbb 2021.4.0 h59b6b97_0
tensorboard 2.6.0 pyhd8ed1ab_1 conda-forge
tensorboard-data-server 0.6.0 py39hcbf5309_1 conda-forge
tensorboard-plugin-wit 1.8.0 pyh44b312d_0 conda-forge
tensorflow 2.6.0 mkl_py39h31650da_0
tensorflow-base 2.6.0 mkl_py39h9201259_0
tensorflow-estimator 2.6.0 pyh7b7c402_0
termcolor 1.1.0 py_2 conda-forge
terminado 0.9.4 py39haa95532_0
testpath 0.5.0 pyhd3eb1b0_0
threadpoolctl 2.2.0 pyh0d69192_0
tika 1.24 pyh9f0ad1d_0 conda-forge
tk 8.6.11 h2bbff1b_0
tornado 6.1 py39h2bbff1b_0
tqdm 4.62.3 pyhd8ed1ab_0 conda-forge
traitlets 5.1.1 pyhd3eb1b0_0
typing-extensions 4.0.1 hd8ed1ab_0 conda-forge
typing_extensions 4.0.1 pyha770c72_0 conda-forge
tzdata 2021e hda174b7_0
urllib3 1.26.7 pyhd3eb1b0_0
vc 14.2 h21ff451_1
vs2015_runtime 14.27.29016 h5e58377_2
wcwidth 0.2.5 pyhd3eb1b0_0
webencodings 0.5.1 py39haa95532_1
werkzeug 2.0.1 pyhd8ed1ab_0 conda-forge
wheel 0.35.1 pyh9f0ad1d_0 conda-forge
win_inet_pton 1.1.0 py39haa95532_0
wincertstore 0.2 py39haa95532_2
winpty 0.4.3 4
wrapt 1.13.3 py39hb82d6ee_1 conda-forge
xz 5.2.5 h62dcd97_0
yarl 1.7.2 py39hb82d6ee_1 conda-forge
zipp 3.6.0 pyhd3eb1b0_0
zlib 1.2.11 h62dcd97_4
zstd 1.4.9 h19a0ad4_0
提前感谢您的帮助!
发布于 2022-10-26 12:48:09
使用Windows 10、Python3.8和Python3.9,我也遇到了同样的问题。在创建conda期间,我安装了python和Tensorflow-GPU。
conda create --name tf_gpu_py_39 tensorflow-gpu python=3.9
Python3.8和Python3.9以同一期结束:
ModuleNotFoundError: No module named 'keras'
就像奥什尔福神已经赎罪了一样。我使用conda list查找包。
...
tensorflow 2.6.0
tensorflow-base 2.6.0
tensorflow-estimator 2.6.0
tensorflow-gpu 2.6.0
...
一切都在那里,卡拉斯应该是坦索弗洛的一部分。
@Aushilfgod的一个评论说,除了使用导入tensorflow作为tf之外,您还应该使用来自tensorflow的tensorflow和tf.keras.的调用。我很惊讶,这是成功的,因为它不应该改变任何事情。但我很高兴,如果它对某人有效,肯定对我不起作用。
我无法让Tensorflow-GPU2.6.0运行。只有降级对我有效,即使这不是最令人满意的方式。执行conda install -c anaconda tensorflow-gpu=2.5
后,所有内容都相应地“更新”,请参见conda list
tensorflow 2.5.0
tensorflow-base 2.5.0
tensorflow-estimator 2.5.0
tensorflow-gpu 2.5.0
这个问题就消失了。我希望这会有所帮助,即使这不是一个完美的解决方案,更像是一个解决办法。
https://stackoverflow.com/questions/70324362
复制相似问题