第一

开发环境搭建

1. tensorflow的环境搭建

windows下安装cpu版tensorflow:

pip install tensorflow

在ubuntu上安装gpu版tensorflow:

a. 在硬件上装上英伟达独立显卡

例如:买来的nvidia geforce gtx 1070 公版显卡,发现显卡体积太大,机箱容纳不下显卡; 显卡要求最小功率为500W,台式机电源额定功率为250W;显卡上有SLI接口用于多个独显集成;

首先解决机箱问题,需要使用独立显卡外接排线,将显卡装在机箱外面,若500W电源只为显卡供电且没有接主板,则需要将电源上的主板连接口的绿线和任意黑线短接。

其次解决电源功率太小问题,买一个600W额定功率的大电源,同时使用250W的原装电源和600W的外加电源,250W的电源用于主板,硬盘,cpu的供电;600W电源用于独立显卡的供电

b. 更新显卡驱动

c. 安装gpu版tensorflow

注意:最好是使用virtualenv安装python环境,步骤如下:

首先,安装一个最基本的python环境,基本的python环境中已经安装了pip;如果没有安装pip,mac下可使用sudo easy_install pip来安装pip;

其次,安装virtualenv:例如下面的例子:

cd ~/Project

mkdir tensorflow_project

cd tensorflow_project

virtualenv --no-site-packages tensorflow_env

cd tensorflow_env

source ./bin/activate

最后,在该virtualenv环境下安装tensorflow:使用命令pip install tensorflow即可;

2. 第一个tensorflow程序(tensorflow基本写法)

# coding: UTF-8

import tensorflow as tf

# 定义常量
one = tf.constant(1)

# 定义变量
state = tf.Variable(0, name='result')

# 定义运算
temp = tf.add(state, one)

# 定义tensor的op操作
op_update = tf.assign(state, temp)
# op_init = tf.initialize_all_variables()
op_init = tf.global_variables_initializer()

# 运行
with tf.Session() as sess:
    sess.run(op_init)
    for i in range(10):
        sess.run(op_update)
        print(sess.run(state))

注意:

a. tensorflow程序中一般包括定义常量,变量,运算; 其次为tensor和op; 最后是在gpu中运行tensor

第二

基本概念与入门

1.基本概念

张量:

例如:tf.constant([1.0, 2.0], name="cons")

计算图:

graph1 = tf.Graph()

with graph1.as_default():

  a = tf.Variable(tf.random([2, 3], stddev=1, seed=1))

with tf.Session(graph=graph1) as sess:

  init_var = tf.initialize_all_variables()

  sess.run(init_var)

会话:

sess = tf.Session()

sess.run(init_var)

sess.close()

with tf.Session() as sess:

  sess.run(init_var)

  

sess= tf.InteractiveSession()

sess.run(init_var)

sess.close()

config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)

sess = tf.Session(config)

前向传播算法:

反向传播算法:

监督学习:

深度学习:

2.神经网络解决分类问题的基本步骤:

首先,提取特征向量作为神经网络的输入

其次,定义神经网络结构

再者,训练神经网络

最后,预测未知的数据

3. 使用前向传播算法的例子:

例如:

#!~/Project/tensorflow_project/tensorflow_env/bin/python
# coding=utf-8

import tensorflow as tf
import numpy as np

#1.提取特征值
x = tf.constant([[0.7, 0.9]])

#2.建立神经网络结构
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))

#3.前向传播算法
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)

#4.反向传播算法

init_var = tf.initialize_all_variables()

with tf.Session() as sess:
        sess.run(init_var)
        print sess.run(y)

4. 使用占位符实现多输入的例子:

例如:

#!~/Project/tensorflow_project/tensorflow_env/bin/python
# coding=utf-8

import tensorflow as tf
import numpy as np

#1.提取特征值
#x = tf.constant([[0.7, 0.9]])
x = tf.placeholder(tf.float32, name="x-input", shape=(3,2))

#2.建立神经网络结构
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))

#3.前向传播算法
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)

#4.反向传播算法

init_var = tf.initialize_all_variables()

with tf.Session() as sess:
        sess.run(init_var)
        print sess.run(y, feed_dict={x:[[0.7, 0.9], [0.1, 0.4], [0.5, 0.8]]})

5. 完整的训练神经网络的例子

注意:训练神经网路的三个步骤

首先,定义神经网络结构及使用前向传播算法输出结果

其次,定义损失函数及选择反向传播算法

最后,在会话上反复运行反向传播算法

例如:

#!~/Project/tensorflow_project/tensorflow_env/bin/python
# coding=utf-8

import tensorflow as tf
import numpy as np
from numpy.random import RandomState

#1.提取特征值
x = tf.placeholder(tf.float32, name="x-input", shape=(None,2))
y_ = tf.placeholder(tf.float32, name="y-input", shape=(None,1))

#2.建立神经网络结构
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))

#3.前向传播算法
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)

#定义测试数据
X = RandomState(1).rand(128, 2)
Y = [[int(x1+x2<1)] for (x1, x2) in X]

#定义损失函数
loss = -tf.reduce_mean(y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0)))
train_step = tf.train.AdamOptimizer(0.00001).minimize(loss)

init_var = tf.initialize_all_variables()

with tf.Session() as sess:
        sess.run(init_var)
        print sess.run(w1)
        print sess.run(w2)

        #开始训练
        ALL_SIZE = 50000
        BATCH_SIZE = 10
        for i in range(ALL_SIZE):
                start = (i * BATCH_SIZE) % 128
                end = min(start + BATCH_SIZE, 128)

                sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})
                total_loss = sess.run(loss, feed_dict={x: X, y_: Y})
                print "After", i, "times trainning, lossing rate is ", total_loss
        print(sess.run(w1))
        print(sess.run(w2))

第三

Mnist问题

1.下载数据集

下载地址为

http://yann.lecun.com/exdb/mnist/

下载后的文件分别为:

train-images-idx3-ubyte.gz

train-labels-idx1-ubyte.gz

t10k-images-idx3-ubyte.gz

t10k-labels-idx1-ubyte.gz

例如:编写代码下载mnist数据集,并检验数据集

#!/~/Project/tensorflow_project/tensorflow_env/bin/python
# coding = utf-8

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

print mnist.train.num_examples

print mnist.validation.num_examples

print mnist.test.num_examples

print mnist.train.images[0]

print mnist.train.labels[0]

batch_size = 100
xs, ys = mnist.train.next_batch(batch_size)

print xs.shape

print ys.shape

print xs

print ys

注意:

首先,使用input_data下载并读取数据集;

再者,打印数据集的各项信息来验证数据集;

《Tensorflow从入门到精通》的更多相关文章

  1. 简单物联网:外网访问内网路由器下树莓派Flask服务器

    最近做一个小东西,大概过程就是想在教室,宿舍控制实验室的一些设备. 已经在树莓上搭了一个轻量的flask服务器,在实验室的路由器下,任何设备都是可以访问的:但是有一些限制条件,比如我想在宿舍控制我种花 ...

  2. 利用ssh反向代理以及autossh实现从外网连接内网服务器

    前言 最近遇到这样一个问题,我在实验室架设了一台服务器,给师弟或者小伙伴练习Linux用,然后平时在实验室这边直接连接是没有问题的,都是内网嘛.但是回到宿舍问题出来了,使用校园网的童鞋还是能连接上,使 ...

  3. 外网访问内网Docker容器

    外网访问内网Docker容器 本地安装了Docker容器,只能在局域网内访问,怎样从外网也能访问本地Docker容器? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Docker容器 ...

  4. 外网访问内网SpringBoot

    外网访问内网SpringBoot 本地安装了SpringBoot,只能在局域网内访问,怎样从外网也能访问本地SpringBoot? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装Java 1 ...

  5. 外网访问内网Elasticsearch WEB

    外网访问内网Elasticsearch WEB 本地安装了Elasticsearch,只能在局域网内访问其WEB,怎样从外网也能访问本地Elasticsearch? 本文将介绍具体的实现步骤. 1. ...

  6. 怎样从外网访问内网Rails

    外网访问内网Rails 本地安装了Rails,只能在局域网内访问,怎样从外网也能访问本地Rails? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Rails 默认安装的Rails端口 ...

  7. 怎样从外网访问内网Memcached数据库

    外网访问内网Memcached数据库 本地安装了Memcached数据库,只能在局域网内访问,怎样从外网也能访问本地Memcached数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装 ...

  8. 怎样从外网访问内网CouchDB数据库

    外网访问内网CouchDB数据库 本地安装了CouchDB数据库,只能在局域网内访问,怎样从外网也能访问本地CouchDB数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Cou ...

  9. 怎样从外网访问内网DB2数据库

    外网访问内网DB2数据库 本地安装了DB2数据库,只能在局域网内访问,怎样从外网也能访问本地DB2数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动DB2数据库 默认安装的DB2 ...

  10. 怎样从外网访问内网OpenLDAP数据库

    外网访问内网OpenLDAP数据库 本地安装了OpenLDAP数据库,只能在局域网内访问,怎样从外网也能访问本地OpenLDAP数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动 ...

随机推荐

  1. 关于box-shadow和drop-shadow的显著区别

    一.box-shadow box-shadow是css3中新增的属性,用于增加边框阴影,让原有的元素变得更多样性,它名下有四位小弟,老大控制水平方向偏移,老二控制垂直方向偏移,老三控制模糊度,最小的老 ...

  2. 关于CentOS7下docker-ce无法删除镜像的问题

    未完待续.... 从旧版的docker删除后安装了新版的docker-ce 发现之前镜像无法删除,并且重新pull不能覆盖,会出现两个一模一样的镜像. [root@localhost ~]# dock ...

  3. binarySearch(int[] a,int fromIndex,int toIndex, int key)的用法

    package com.Summer_0420.cn; import java.util.Arrays; /** * @author Summer * binarySearch(int[] a,int ...

  4. 环境部署(四):Linux下查看JDK安装路径

    在安装好Git.JDK和jenkins之后,就需要在jenkins中进行对应的设置,比如在全局工具配置模块,需要写入JDK的安装路径. 这篇博客,介绍几种常见的在Linux中查看JDK路径的方法... ...

  5. shell笔记-常用

    shell提取文件名: http://blog.csdn.net/u011544778/article/details/50773053 一.使用${} 1.${var##*/}该命令的作用是去掉变量 ...

  6. ASP.NET Core中代码使用X509证书,部署到IIS上后报错:System cannot find the specified file 的解决办法(转载)

    问: I am trying to embrace the mysteries of SSL communication and have found a great tutorial on this ...

  7. SpringBoot整合Druid数据源

    关于SpringBoot数据源请参考我上一篇文章:https://www.cnblogs.com/yueshutong/p/9409295.html 一:Druid介绍 1. Druid是什么? Dr ...

  8. (通用版)salesforce中soql及sosl的伪‘Like’模糊检索

    salesforce里有soql.sosl两种查询语法,soql针对模糊搜索也有‘like’关键字,然而只能针对其自带字段如:Name.Id:对于自定义添加的字段如:Message__c.Note__ ...

  9. Python-生成器_监听文件输入的例子_37

    def tail(filename): f = open(filename,encoding='utf-8') while True: line = f.readline() if line.stri ...

  10. 移动web、webApp、混合APP、原生APP、androd H5混合开发 当无网络下,android怎么加载H5界面

    PhoneGap是一个采用HTML,CSS和JavaScript的技术,创建移动跨平台移动应用程序的快速开发平台.它使开发者能够在网页中调用IOS,Android,Palm,Symbian,WP7,W ...