小白同志如何在利用anaconda和pycharm中搭建自己的tensorflow环境?

    科技2025-03-15  16

    1.下载pycharm和anaconda,直接默认安装

    如果不会请参照B站up主 飞机: Python+Anaconda+PyCharm的安装和基本使用【适合完全零基础】不只是教你如何安装,还告诉你为什么这么安装 b站链接 提醒:anaconda可以用清华镜像下载比较快。

    2.搭建tensorflow环境

    因为需要下载各种包,所以存在速度会慢,这是我们第一步需要切换镜像

    channels: - defaults show_channel_urls: true channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda default_channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2 custom_channels: conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

    复制几个default_channels的链接

    如果存在闪退 或者切换不成功 请收看今天的新闻联播,不对 切换镜像

    Anaconda Prompt下的各种命令

    1.因为tensorflow支持3.4-3,6的python版本,所以我们这里推荐搭建python3.6的版本 输入:

    conda create -n tensorflow python=3.6

    注:会提醒yes or no ,按y回车即可 2、在Anacoda Prompt下启动tensorflow环境

    activate tensorflow

    这是激活tensorflow环境

    3、安装cpu版本的TensorFlow

    pip install --upgrade --ignore-installed tensorflow

    注:都是在Anacoda Prompt下输入 4.测试是否安装成功 测试代码:

    (tensorflow) C:\Users\Kings>python Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 12:30:02) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import tensorflow as tf >>> hello = tf.constant('Hello, TensorFlow!') >>> sess = tf.Session() 2019-07-13 20:19:32.814107: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 >>> print(sess.run(hello)) b'Hello, TensorFlow!' >>> sess.close()

    如果到达这一步,恭喜你CPU版本的tensorflow安装成功。

    与pycharm搭建

    前提你已经下载安装好了pycharm。 切换到tensorflow解释器即可 切换项目解释器,我用的是汉化,非汉化找setting

    这里切换到现有环境下,找到

    我的是这个地址

    C:\Users\kingS\anaconda3\envs\tensorflow\python.exe

    找到自己本机对应的tensorflow环境地址就行。

    代码测试

    import tensorflow as tf hello = tf.constant('hello tensorflow!') sess = tf.Session() print(sess.run(hello)) sess.close()

    输出

    C:\Users\kingS\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint8 = np.dtype([("qint8", np.int8, 1)]) C:\Users\kingS\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_quint8 = np.dtype([("quint8", np.uint8, 1)]) C:\Users\kingS\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:528: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint16 = np.dtype([("qint16", np.int16, 1)]) C:\Users\kingS\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:529: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_quint16 = np.dtype([("quint16", np.uint16, 1)]) C:\Users\kingS\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:530: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint32 = np.dtype([("qint32", np.int32, 1)]) C:\Users\kingS\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:535: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. np_resource = np.dtype([("resource", np.ubyte, 1)]) 2020-10-08 11:09:00.935180: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 b'hello tensorflow!' Process finished with exit code 0

    收工

    参考文章

    https://blog.csdn.net/ITLearnHall/article/details/81708148

    https://blog.csdn.net/devcloud/article/details/95945189

    Processed: 0.012, SQL: 8