Giter Site home page Giter Site logo

snowkylin / tensorflow-cn Goto Github PK

View Code? Open in Web Editor NEW
852.0 46.0 145.0 17.72 MB

简单粗暴 TensorFlow (1.X) | A Concise Handbook of TensorFlow (1.X) | 此版本不再更新,新版见 https://tf.wiki

Home Page: https://v1.tf.wiki

Makefile 0.77% Batchfile 1.07% Python 98.16%

tensorflow-cn's Issues

求解 self(inputs)

TensorFlow模型>基础示例:多层感知机(MLP)的一处代码

class MLP(tf.keras.Model):
    def __init__(self):
        super().__init__()
        self.dense1 = tf.keras.layers.Dense(units=100, activation=tf.nn.relu)
        self.dense2 = tf.keras.layers.Dense(units=10)

    def call(self, inputs):
        x = self.dense1(inputs)
        x = self.dense2(x)
        return x

    def predict(self, inputs):
        logits = self(inputs) # 这一行要怎么理解?
        return tf.argmax(logits, axis=-1)

倒数第二行代码, 是不是等价于self.call(inputs)?

TensorFlow GPU 版本的安装叙述

现文档版本(0.3)的“TensorFlow 安装”一章对于 GPU 版本的 TF 安装叙述略过简短,而它相比 TensorFlow 的 CPU 版本安装更加复杂.在“安装前的环境配置”一节中,建议将 CPU 与 GPU 版的安装分开叙述.在此我愿抛砖引玉.我使用的是 Windows 10 x64 平台,GPU 版本的 TF 安装步骤如下:

---------- 以下正文开始 ----------

确认你的电脑适合安装 GPU 版本的 TensorFlow

  1. 确认你的电脑上配置了 Nvidia 的独立显卡
  2. 前往 CUDA GPUs 网页,找到你的电脑上配置的 Nvidia 显卡型号,确认其 Compute Capability 不低于3.0.一般用户的显卡型号可以在 “CUDA-Enabled GeForce Products” 项下找到.
  3. 确认你的电脑上已安装了 Python 3.5 或更高的版本.

GPU 版本的 TensorFlow 安装步骤

GPU 版本 TensorFlow 由 tensorflow-gpu 包支持(该包也支持 CPU,因此无须再安装仅 CPU 版的 TensorFlow),以下是其安装步骤:

  1. 确认你要安装的 tensorflow-gpu 版本.Python 3.6 的用户请选择不低于 TensoFlow-gpu 1.2 的版本.
  2. 前往 GPU Support 页面,根据“Software requirements”的内容确定需要安装的 Nvidia 显卡驱动、CUDA Toolkit 与 cuDNN 版本.如果要安装过往版本的 tensorflow-gpu,可以参考 Build from Source 页面底部“GPU”表格来选择 CUDA Toolkit 与 cuDNN 版本.
    • CUDA Toolkit 历史版本:tensorflow-gpu 1.12 要求安装的 CUDA Toolkit 9.0 并不是最新的 CUDA.注意,请下载 CUDA Toolkit 9.0,而不是 9.1 或其他 9.x 版本.安装在后续步骤说明.
    • cuDNN:cuDNN 的下载需要免费注册成为 Nvidia Developer.tensorflow-gpu 1.12 要求不低于 7.2 版的 cuDNN.安装在后续步骤说明.
  3. 安装 Nvidia 显卡驱动.根据上一步中的显卡驱动版本要求,下载并安装显卡驱动.
  4. 安装 CUDA Toolkit.安装对应版本的 CUDA Toolkit.在安装时,请选择 Custom 安装,并取消勾选其中的显卡驱动组件;这是因为 CUDA 携带的显卡驱动往往较为陈旧.
  5. 安装 cuDNN.cuDNN 下载后是一个内含名为 “cuda” 文件夹的压缩包,将“cuda”下的所有子文件夹解压到 CUDA Toolkit 安装路径下即可.CUDA Toolkit(以 9.0 版为例)的默认安装路径是:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0
  6. 将 CUDA Toolkit 与 cuDNN 加入 PATH 环境变量.由于 cuDNN 已经解压到 CUDA Toolkit 文件夹下,因此只需要向 PATH 添加以下两个路径(这也是 GPU Support 页面底部的步骤):
    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin
    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\extras\CUPTI\libx64
    
  7. 安装 tensorflow-gpu.利用 pip 可以方便地安装 tensorflow-gpu 的最新版本:
    pip3 install tensorflow-gpu
    若被网速或者版本困扰,你也可以访问 tensorflow-gpu 的 PyPi 页面,从左侧的 Release History 中选择要安装的 tensorflow-gpu 版本,下载对应的 whl 文件,然后从本地安装(以 1.12 版为例):
    pip3 install tensorflow_gpu-1.12.0-cp36-cp36m-win_amd64.whl

测试 GPU 版的 TensorFlow 是否正确安装

可用 Python 执行以下命令来测试安装结果:

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

如果打印结果中包含 GPU 设备,则说明 GPU 版的 TensorFlow 已正确地识别了你电脑的 Nvidia 显卡.

---------- 正文结束 ----------

由于我并没有使用 Anaconda,因此安装步骤与文中将有所出入.

Deprecated MNIST data loader

In the MLP example, this line

mnist = tf.contrib.learn.datasets.load_dataset("mnist")

prints tons of deprecation error:

WARNING:tensorflow:From /home/zhanghuimeng/Documents/learnTensorFlow/simple_introduction/multilayer_perceptron.py:9: load_dataset (from tensorflow.contrib.learn.python.learn.datasets) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data.
...

And it actually can't download anything. (The reason might be...) In the end, you might have to download MNIST by hand. (see this)

A better (not deprecated) alternative is:

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

But it cannot download anything either. Finally I had to download from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz and load it by NumPy.

There might be a better alternative, but I still suggest using something not deprecated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.