TensorFlow 初使用

安装环境

  1. Python 3.9

  2. Anaconda 3 环境变量 anaconda3与anaconda3\Scripts

  3. 参考参考2官网其他

    conda --version
    

环境

创建 conda 环境

conda create -n tensorlow python=3.8
Collecting package metadata (current_repodata.json): done
Solving environment: done


==> WARNING: A newer version of conda exists. <==
  current version: 4.8.3
  latest version: 4.9.2

Please update conda by running

    $ conda update -n base -c defaults conda



## Package Plan ##

  environment location: XXXXXXX\anaconda3\envs\tensortflow

  added / updated specs:
    - python=3.8


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ca-certificates-2020.10.14 |                0         122 KB
    certifi-2020.6.20          |     pyhd3eb1b0_3         155 KB
    openssl-1.1.1h             |       he774522_0         4.8 MB
    pip-20.2.4                 |   py39haa95532_0         1.8 MB
    python-3.9.0               |       h6244533_2        16.4 MB
    setuptools-50.3.1          |   py39haa95532_1         740 KB
    sqlite-3.33.0              |       h2a8f88b_0         809 KB
    tzdata-2020d               |       h14c3975_0         110 KB
    wheel-0.35.1               |     pyhd3eb1b0_0          38 KB
    wincertstore-0.2           |   py39h2bbff1b_0          15 KB
    ------------------------------------------------------------
                                           Total:        25.0 MB

The following NEW packages will be INSTALLED:

  ca-certificates    pkgs/main/win-64::ca-certificates-2020.10.14-0
  certifi            pkgs/main/noarch::certifi-2020.6.20-pyhd3eb1b0_3
  openssl            pkgs/main/win-64::openssl-1.1.1h-he774522_0
  pip                pkgs/main/win-64::pip-20.2.4-py39haa95532_0
  python             pkgs/main/win-64::python-3.9.0-h6244533_2
  setuptools         pkgs/main/win-64::setuptools-50.3.1-py39haa95532_1
  sqlite             pkgs/main/win-64::sqlite-3.33.0-h2a8f88b_0
  tzdata             pkgs/main/noarch::tzdata-2020d-h14c3975_0
  vc                 pkgs/main/win-64::vc-14.1-h0510ff6_4
  vs2015_runtime     pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_3
  wheel              pkgs/main/noarch::wheel-0.35.1-pyhd3eb1b0_0
  wincertstore       pkgs/main/win-64::wincertstore-0.2-py39h2bbff1b_0
  zlib               pkgs/main/win-64::zlib-1.2.11-h62dcd97_4


Proceed ([y]/n)? y


Downloading and Extracting Packages
tzdata-2020d         | 110 KB    | #################################### | 100%
wincertstore-0.2     | 15 KB     | #################################### | 100%
setuptools-50.3.1    | 740 KB    | #################################### | 100%
certifi-2020.6.20    | 155 KB    | #################################### | 100%
pip-20.2.4           | 1.8 MB    | ##########################5          |  74%

激活 conda 环境

conda init   # 重启CMD.exe
conda info --envs
  
conda activate tensorflow 
(tensortflow) C:\Windows\system32>

根据要在 conda 环境中安装的 TensorFlow 版本

python版本过高导致TensorFlow不能正确安装。此时需要安装低版本的python

conda install python=3.5 #降低pyth版本
(tensorflow)C:>
pip install tensorflow
pip install https://files.pythonhosted.org/packages/61/e1/1e713485caee272e7be779ac10bb0cec36ace3d9f981e067a9105a736791/tensorflow-2.3.1-cp38-cp38-win_amd64.whl
pip install --ignore-installed --upgrade tensorflow

我这里是需要翻墙的 不然下有些包总是显示超时:
tensorflow 安装

Google 使用 wheel 标准分发,以.whl后缀的ZIP格式文件

输入python 输入以下代码:

import tensorflow as tf 
message =tf.constant('Welcome to Tensorflow')
with tf.Session() as sess:
	print ( sess.run(message).decode() )

tensorflow版本2.0无法兼容版本1.0. tf.disable_eager_execution()

#!/usr/bin/python3
import tensorflow.compat.v1 as tf
tf.disable_eager_execution() #保证sess.run()能够正常运行 
message=tf.constant('Welcome')
with tf.Session() as sess:
	 print(sess.run(message).decode())  #

禁用conda 环境

deactivate   	  #windows 
source deactivate #MAC / Ubuntu 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
欢迎来到 tensorflow 体验第四关! 在这一关,我们将学习如何使用 TensorFlow 构建一个简单的神经网络,并对手写数字进行分类。 首先,让我们导入所需的库和数据集: ```python import tensorflow as tf from tensorflow import keras import matplotlib.pyplot as plt # 加载 MNIST 手写数字数据集 mnist = keras.datasets.mnist (train_images, train_labels), (test_images, test_labels) = mnist.load_data() ``` 接下来,让我们对数据进行预处理。我们将把像素值缩放到 0 到 1 的范围内,并将标签转换为 one-hot 编码: ```python # 将像素值缩放到 0 到 1 的范围内 train_images = train_images / 255.0 test_images = test_images / 255.0 # 将标签转换为 one-hot 编码 train_labels = keras.utils.to_categorical(train_labels) test_labels = keras.utils.to_categorical(test_labels) ``` 现在,我们可以构建一个简单的神经网络模型。我们将使用两个隐藏层,每个隐藏层包含 128 个神经元,并使用 softmax 激活函数进行输出: ```python # 构建一个简单的神经网络模型 model = keras.Sequential([ keras.layers.Flatten(input_shape=(28, 28)), keras.layers.Dense(128, activation=tf.nn.relu), keras.layers.Dense(128, activation=tf.nn.relu), keras.layers.Dense(10, activation=tf.nn.softmax) ]) ``` 接下来,我们需要编译模型并指定损失函数、优化器和评估指标: ```python # 编译模型 model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) ``` 最后,我们可以训练模型并评估其性能: ```python # 训练模型 model.fit(train_images, train_labels, epochs=5) # 评估模型 test_loss, test_acc = model.evaluate(test_images, test_labels) print('Test accuracy:', test_acc) ``` 恭喜你完成了 tensorflow 体验第四关!现在你已经知道如何构建一个简单的神经网络,并对手写数字进行分类。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ynchyong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值