【szm2019】TF的学习笔记

人工智能

#1

准备环境

有监督学习(Supervised learning):

通过生成一个函数将输入映射为一个合适的输出(通常也称为标记,多数情况下训练集都是有人工专家标注生成的)。例如分类问题,分类器更加输入向量和输出的分类标记模拟了一个函数,对于新的输入向量,得到它的分类结果。

无监督学习(Unsupervised learning):

与有监督学习相比,训练集没有人为标注的结果。常见的无监督学习算法有聚类。

半监督学习:

介于监督学习与无监督学习之间。

sudo apt-get install python-pip python-dev

sudo pip install --upgrade
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl

pip install --upgrade pip

sudo add-apt-repository ppa:mystic-mirage/komodo-edit


#2

不知道怎么回事 不能贴多图 所以 这里记录到博客 点这里


#3

点击上传图片,引用就可以,markdown的用法百度一下~


#4

嗯嗯 首先 谢谢你的提醒 :grin: 我试过了 不行的 先是手动拖进去上传了 然后 还是显示 什么访客不能只能插入一张图片 然后 我把已经上传的 图片的地址记下来了 然后使用 makedown的语法标记图片引用 无奈:joy: 前后试了 好几次 还是 提示 访客只能插入一张图片 当然 我我确定我是肯定登录的:sweat_smile:


#5

我给你,开一下权限~你再试一试


#6

测试


#7

:smile:貌似 可以了 这样 就舒服多了 谢谢你:grin::grin:


#8

我安装tensorflow的时候也好艰辛,但是现在还没解决用spyder编码时,无法引用tensorflow,不知道你们有没有遇到


#9

我 是用Ubuntu的, 然后按照 例子跟着 走一遍代码 就行了 中间可能有点小问题 但多试几次 就可以通过了

无法引用的话 确定安装好的Tensorflow的话 看看是否是哪里 有无 打错字母符号 我有时候 就会有马虎 犯错

其他的 我就不清楚的了

建议你 开个求助帖子 上几张图片 肯定 有大神帮你解决的:grinning:


#10

我准备一会儿再试一下,不行的话就去求助了


#11

嗯嗯 稳住 你可以的:muscle:


#12

使用tf实现Logistic Regression算法

-- coding: utf-8 --

import tensorflow as tf

# 导入input_data用于自动下载
from tensorflow.examples.tutorials.mnist import input_data
# 安装MNIST数据集
mnist = input_data.read_data_sets("MNIST_Data/data/", one_hot=True)

# 一些参数
learning_rate = 0.01
training_epochs = 25
batch_size = 100
display_step = 1

# 放置占位符,用于在计算时接收输入值

# mnist图像数据 28*28=784
x = tf.placeholder(tf.float32, [None, 784])

# 图像类别,总共10类
y = tf.placeholder(tf.float32, [None, 10])

#创建两个变量,分别用来存放权重值W和偏置值b
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# 使用Tensorflow提供的回归模型softmax,y代表输出
pred = tf.nn.softmax(tf.matmul(x, W) + b)

# reduce_sum 对所有类别求和,reduce_mean 对和取平均
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))

# 往graph中添加新的操作,计算梯度,计算参数的更新
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# 初始化之前创建的变量的操作
init = tf.initialize_all_variables()

# 启动初始化
with tf.Session() as sess:
    sess.run(init)
    # 开始训练模型
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(mnist.train.num_examples/batch_size)
        # 每一轮迭代total_batches
        for i in range(total_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            # 使用batch data训练数据
            _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs,
                                                          y: batch_ys})
            # 将每个batch的损失相加求平均
            avg_cost += c / total_batch
        # 每一轮打印损失
        if (epoch+1) % display_step == 0:
            print "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost)

    print "Optimization Finished!"

    # 评估模型,tf.argmax能给出某个tensor对象在某一维上数据最大值的索引。因为标签是由0,1组成了one-hot vector,返回的索引就是数值为1的位置
    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))


    # 计算正确预测项的比例,因为tf.equal返回的是布尔值,使用tf.cast可以把布尔值转换成浮点数,tf.reduce_mean是求平均值
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # 在session中启动accuracy,输入是MNIST中的测试集
    print "Accuracy:", accuracy.eval({x: mnist.test.images[:3000], y: mnist.test.labels[:3000]})


#13

week3 在MNIST手写数字数据集上,使用LeNet5模型进行训练和测试,评估准确率。

# -*- coding: utf-8 -*-
import tensorflow as tf
# 配置神经网络的参数
INPUT_NODE = 784
OUTPUT_NODE = 10

IMAGE_SIZE = 28
NUM_CHANNELS = 1
NUM_LABELS = 10
# 第一层卷积的深度和尺寸
CONV1_DEEP = 32
CONV1_SIZE = 5
# 第二层卷积的深度和尺寸
CONV2_DEEP = 64
CONV2_SIZE = 5
# 全连接的层的节点个数
FC_SIZE = 512

def inference(input_tensor, train, regularizer):
	# 第一层 卷积层
	# 定义输入的28x28x1的矩阵的原始MNIST的原始数据,输出28x28x32的举证
    with tf.variable_scope('layer1-conv1'):
        conv1_weights = tf.get_variable(
            "weight", [CONV1_SIZE, CONV1_SIZE, NUM_CHANNELS, CONV1_DEEP],
            initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv1_biases = tf.get_variable("bias", [CONV1_DEEP], initializer=tf.constant_initializer(0.0))
        # 使用CONV1 边长为5 深度为32的过滤器 过滤器的移动步长为1 且使用的全0填充
        conv1 = tf.nn.conv2d(input_tensor, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')
        relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))
	# 第二层 池化层
	# 使用全0填充并且移动的步数为2.输入28x28x32 ,输出14x14x32
    with tf.name_scope("layer2-pool1"):
        pool1 = tf.nn.max_pool(relu1, ksize = [1,2,2,1],strides=[1,2,2,1],padding="SAME")
	# 第三层 卷积层
	# 输入14x14x32的矩阵,输出14x14x64
    with tf.variable_scope("layer3-conv2"):
        conv2_weights = tf.get_variable(
            "weight", [CONV2_SIZE, CONV2_SIZE, CONV1_DEEP, CONV2_DEEP],
            initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv2_biases = tf.get_variable("bias", [CONV2_DEEP], initializer=tf.constant_initializer(0.0))
         # 使用CONV2 边长为5 深度为64的过滤器 过滤器的移动步长为1 且使用的全0填充
        conv2 = tf.nn.conv2d(pool1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME')
        relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))
	# 第四层 池化层
	# 输入14x14x64 输出为7x7x64  
	# 用pool2.get_shape函数,可以得到第四层的输出维度.
    with tf.name_scope("layer4-pool2"):
        pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
        pool_shape = pool2.get_shape().as_list()
		# 维度中包括一个batch的数据的个数,pool_shape[0]为一个batch中的数据
        nodes = pool_shape[1] * pool_shape[2] * pool_shape[3]
		# 通过tf.reshape,函数将第四层输出一个batch的变量
        reshaped = tf.reshape(pool2, [pool_shape[0], nodes])
	# 第五层 全连接层
	# 输入一个拉直后一组向量
	# 向量长度为7x7x64=3136 ,输出FC_SIZE=512的向量
    with tf.variable_scope('layer5-fc1'):
        fc1_weights = tf.get_variable("weight", [nodes, FC_SIZE],
                                      initializer=tf.truncated_normal_initializer(stddev=0.1))
        if regularizer != None: tf.add_to_collection('losses', regularizer(fc1_weights))
        fc1_biases = tf.get_variable("bias", [FC_SIZE], initializer=tf.constant_initializer(0.1))

        fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_weights) + fc1_biases)
        if train: fc1 = tf.nn.dropout(fc1, 0.5)
	# 第六层 全连接层
    with tf.variable_scope('layer6-fc2'):
		# 声明第六层的变量继续传播,输入一组长度为 FC_SIZE = 512的向量
        fc2_weights = tf.get_variable("weight", [FC_SIZE, NUM_LABELS],
                                      initializer=tf.truncated_normal_initializer(stddev=0.1))
        # 输出一组长度为10的向量
        if regularizer != None: tf.add_to_collection('losses', regularizer(fc2_weights))
        fc2_biases = tf.get_variable("bias", [NUM_LABELS], initializer=tf.constant_initializer(0.1))
        logit = tf.matmul(fc1, fc2_weights) + fc2_biases
	# 返回第六层的输出
    return logit
# -*- coding: utf-8 -*-
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_inference
import numpy as np
import os
# 配置神经网络的参数
# 一个训练batch中的训练数据个数,数字越小 训练过程月接接近
BATCH_SIZE = 100
# 基础的训练率
LEARNING_RATE_BASE = 0.8
# 训练的衰减率
LEARNING_RATE_DECAY = 0.99
# 描述模型复杂的正则化在损失函数的系数
REGULARIZATION_RATE = 0.0001
# 训练的轮数
TRAINING_STEPS = 30000
# 滑动的平均衰减率
MOVING_AVERAGE_DECAY = 0.99
# 模型保存的路径和文件名
MODEL_SAVE_PATH="MNIST_model/"
MODEL_NAME="mnist_model"


def train(mnist):
	# 定义输出为4维矩阵的placeholder
    x = tf.placeholder(tf.float32, [
        BATCH_SIZE,# 第一维表示一个batch中样例的个数
        mnist_inference.IMAGE_SIZE,# 第二维度表示的图书的尺寸
        mnist_inference.IMAGE_SIZE,# 第三维度表示的图书的尺寸
        mnist_inference.NUM_CHANNELS],# 第四维度表示图片的深度 RBG格式的深度为5
                       name='x-input')
    y_ = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input')

    regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)
	# 直接使用mnist_inference中定义的前向传播
    y = mnist_inference.inference(x,True, regularizer)
	# 初始化迭代轮数
    global_step = tf.Variable(0, trainable=False)

	# 定义损失函数,学习率,滑动平均操作和过程
    variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
    variables_averages_op = variable_averages.apply(tf.trainable_variables())
    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
    cross_entropy_mean = tf.reduce_mean(cross_entropy)
    loss = cross_entropy_mean + tf.add_n(tf.get_collection('losses'))
    learning_rate = tf.train.exponential_decay(
        LEARNING_RATE_BASE,# 基础的学习率 在这个基础上递减
        global_step,# 当前的迭代轮数
        mnist.train.num_examples / BATCH_SIZE,# 过完所有的训练数据的需要的迭代次数
		LEARNING_RATE_DECAY,# 训练的衰减率
        staircase=True)
    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
    with tf.control_dependencies([train_step, variables_averages_op]):
        train_op = tf.no_op(name='train')

	# 初始化Tensorflow的持久化类 开始训练
    saver = tf.train.Saver()
    with tf.Session() as sess:
        tf.global_variables_initializer().run()
		# 在训练过程中
        for i in range(TRAINING_STEPS):
            xs, ys = mnist.train.next_batch(BATCH_SIZE)
			# 输入的训练集格式调整一个四维的矩阵,并将这个调整数据传入下面的sess.run
            reshaped_xs = np.reshape(xs, (
                BATCH_SIZE,
                mnist_inference.IMAGE_SIZE,
                mnist_inference.IMAGE_SIZE,
                mnist_inference.NUM_CHANNELS))
            _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: reshaped_xs, y_: ys})
            if i % 1000 == 0:
                print("After %d training step(s), loss on training batch is %g." % (step, loss_value))
                saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)


def main(argv=None):
	#载入MNIST的数据集
    mnist = input_data.read_data_sets("./MNIST_data/", one_hot=True)
    train(mnist)

if __name__ == '__main__':
    tf.app.run()



# -*- coding: utf-8 -*-
import time
import math
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
import mnist_inference
import mnist_train

def evaluate(mnist):
    with tf.Graph().as_default() as g:
        # 定义输出为4维矩阵的placeholder
        x = tf.placeholder(tf.float32, [
            mnist_train.BATCH_SIZE,
            mnist_inference.IMAGE_SIZE,
            mnist_inference.IMAGE_SIZE,
            mnist_inference.NUM_CHANNELS],
                           name='x-input')
        y_ = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input')
        validate_feed = {x: mnist.test.images, y_: mnist.test.labels}
        global_step = tf.Variable(0, trainable=False)

        regularizer = tf.contrib.layers.l2_regularizer(mnist_train.REGULARIZATION_RATE)
		# 直接使用mnist_inference中定义的前向传播
		y = mnist_inference.inference(x, False, regularizer)
        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
		# 定义损失函数,学习率,滑动平均操作和过程
        variable_averages = tf.train.ExponentialMovingAverage(mnist_train.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        n = math.ceil(mnist.test.num_examples / mnist_train.BATCH_SIZE)
        for i in range(int(n)):
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(mnist_train.MODEL_SAVE_PATH)
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)
                    global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                    xs, ys = mnist.test.next_batch(mnist_train.BATCH_SIZE)
                    reshaped_xs = np.reshape(xs, (
                        mnist_train.BATCH_SIZE,
                        mnist_inference.IMAGE_SIZE,
                        mnist_inference.IMAGE_SIZE,
                        mnist_inference.NUM_CHANNELS))
                    accuracy_score = sess.run(accuracy, feed_dict={x:reshaped_xs, y_:ys})
                    print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score))
                else:
                    print('No checkpoint file found')
                    return


def main(argv=None):
	#载入训练的MNIST的数据集
    mnist = input_data.read_data_sets("./MNIST_data/", one_hot=True)
    evaluate(mnist)

if __name__ == '__main__':
    main()

训练

测试

很明显这样的实验结果和预期不太一样 可能是基础的训练率 LEARNING_RATE_BASE = 0.8 过大 所以我尝试可以试着减小100倍后 LEARNING_RATE_BASE = 0.008 就可以得到下面的 数据结果

训练

测试


#14

使用AlexNet模型,训练图片分类器,并评估准确率。(图片分类数据集:CIFAR-10 dataset 链接:http://www.cs.toronto.edu/~kriz/cifar.html


参考Tensorflow官方 https://github.com/tensorflow/models/tree/master/tutorials/image/cifar10/

进行修改 请多多指教 :smile:

cifar.10

# -*- coding: utf-8 -*-
# pylint: disable=missing-docstring
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import gzip
import os
import re
import sys
import tarfile

import tensorflow.python.platform
from six.moves import urllib
import tensorflow as tf

import cifar10_input

FLAGS = tf.app.flags.FLAGS

# #基本的模型参数
tf.app.flags.DEFINE_integer('batch_size', 128,
                            """Number of images to process in a batch.""")
tf.app.flags.DEFINE_string('data_dir', '/tmp/cifar10_data',
                           """Path to the CIFAR-10 data directory.""")

# CIFAR-10数据集的全局常量
IMAGE_SIZE = cifar10_input.IMAGE_SIZE
NUM_CLASSES = cifar10_input.NUM_CLASSES
NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN = cifar10_input.NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN
NUM_EXAMPLES_PER_EPOCH_FOR_EVAL = cifar10_input.NUM_EXAMPLES_PER_EPOCH_FOR_EVAL


# 训练过程的常量。
MOVING_AVERAGE_DECAY = 0.9999     # 用于移动平均线的衰减。
NUM_EPOCHS_PER_DECAY = 350.0      # 学习速率衰减之后的时间。
LEARNING_RATE_DECAY_FACTOR = 0.1  # 学习率衰减因子。
INITIAL_LEARNING_RATE = 0.1       # 初始学习率。

# 如果一个模型使用多个GPU进行训练,则使用tower_name将所有的操作名称前缀区分操作
TOWER_NAME = 'tower'

DATA_URL = 'http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz'


def _activation_summary(x):
  """Helper to create summaries for activations.

  Creates a summary that provides a histogram of activations.
  Creates a summary that measure the sparsity of activations.

  Args:
    x: Tensor
  Returns:
    nothing
  """
  # 如果这是多GPU训练,请从名称中删除“tower_ [0-9] /”会话
  # 这有助于张量板上显示的清晰度。
  tensor_name = re.sub('%s_[0-9]*/' % TOWER_NAME, '', x.op.name)
  tf.summary.histogram(tensor_name + '/activations', x)
  tf.summary.scalar(tensor_name + '/sparsity', tf.nn.zero_fraction(x))


def _variable_on_cpu(name, shape, initializer):
  """Helper to create a Variable stored on CPU memory.

  Args:
    name: name of the variable
    shape: list of ints
    initializer: initializer for Variable

  Returns:
    Variable Tensor
  """
  with tf.device('/cpu:0'):
    var = tf.get_variable(name, shape, initializer=initializer)
  return var


def _variable_with_weight_decay(name, shape, stddev, wd):
  """Helper to create an initialized Variable with weight decay.

  Note that the Variable is initialized with a truncated normal distribution.
  A weight decay is added only if one is specified.

  Args:
    name: name of the variable
    shape: list of ints
    stddev: standard deviation of a truncated Gaussian
    wd: add L2Loss weight decay multiplied by this float. If None, weight
        decay is not added for this Variable.

  Returns:
    Variable Tensor
  """
  var = _variable_on_cpu(name, shape,
                         tf.truncated_normal_initializer(stddev=stddev))
  if wd:
    weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name='weight_loss')
    tf.add_to_collection('losses', weight_decay)
  return var


def distorted_inputs():
  """Construct distorted input for CIFAR training using the Reader ops.

  Returns:
    images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
    labels: Labels. 1D tensor of [batch_size] size.

  Raises:
    ValueError: If no data_dir
  """
  if not FLAGS.data_dir:
    raise ValueError('Please supply a data_dir')
  data_dir = os.path.join(FLAGS.data_dir, 'cifar-10-batches-bin')
  return cifar10_input.distorted_inputs(data_dir=data_dir,
                                        batch_size=FLAGS.batch_size)


def inputs(eval_data):
  """Construct input for CIFAR evaluation using the Reader ops.

  Args:
    eval_data: bool, indicating if one should use the train or eval data set.

  Returns:
    images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
    labels: Labels. 1D tensor of [batch_size] size.

  Raises:
    ValueError: If no data_dir
  """
  if not FLAGS.data_dir:
    raise ValueError('Please supply a data_dir')
  data_dir = os.path.join(FLAGS.data_dir, 'cifar-10-batches-bin')
  return cifar10_input.inputs(eval_data=eval_data, data_dir=data_dir,
                              batch_size=FLAGS.batch_size)


def inference(images):
  """Build the CIFAR-10 model.

  Args:
    images: Images returned from distorted_inputs() or inputs().

  Returns:
    Logits.
  """
  # 我们使用tf.get_variable()而不是实例化所有变量
  # tf.Variable()为了在多个GPU训练运行中共享变量。
  # 如果我们只在一个GPU上运行这个模型,我们可以简化这个功能
  # 用tf.Variable()替换tf.get_variable()的所有实例。
  #
  # conv1
  with tf.variable_scope('conv1') as scope:
    kernel = _variable_with_weight_decay('weights', shape=[5, 5, 3, 64],
                                         stddev=1e-4, wd=0.0)
    conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME')
    biases = _variable_on_cpu('biases', [64], tf.constant_initializer(0.0))
    bias = tf.nn.bias_add(conv, biases)
    conv1 = tf.nn.relu(bias, name=scope.name)
    _activation_summary(conv1)

  # pool1
  pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
                         padding='SAME', name='pool1')
  # norm1
  norm1 = tf.nn.lrn(pool1, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75,
                    name='norm1')

  # conv2
  with tf.variable_scope('conv2') as scope:
    kernel = _variable_with_weight_decay('weights', shape=[5, 5, 64, 64],
                                         stddev=1e-4, wd=0.0)
    conv = tf.nn.conv2d(norm1, kernel, [1, 1, 1, 1], padding='SAME')
    biases = _variable_on_cpu('biases', [64], tf.constant_initializer(0.1))
    bias = tf.nn.bias_add(conv, biases)
    conv2 = tf.nn.relu(bias, name=scope.name)
    _activation_summary(conv2)

  # norm2
  norm2 = tf.nn.lrn(conv2, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75,
                    name='norm2')
  # pool2
  pool2 = tf.nn.max_pool(norm2, ksize=[1, 3, 3, 1],
                         strides=[1, 2, 2, 1], padding='SAME', name='pool2')

  # local3
  with tf.variable_scope('local3') as scope:
    # 把所有东西都移到深度,这样我们可以执行一个单一的矩阵乘法。
    dim = 1
    for d in pool2.get_shape()[1:].as_list():
      dim *= d
    reshape = tf.reshape(pool2, [FLAGS.batch_size, dim])

    weights = _variable_with_weight_decay('weights', shape=[dim, 384],
                                          stddev=0.04, wd=0.004)
    biases = _variable_on_cpu('biases', [384], tf.constant_initializer(0.1))
    local3 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name=scope.name)
    _activation_summary(local3)

  # local4
  with tf.variable_scope('local4') as scope:
    weights = _variable_with_weight_decay('weights', shape=[384, 192],
                                          stddev=0.04, wd=0.004)
    biases = _variable_on_cpu('biases', [192], tf.constant_initializer(0.1))
    local4 = tf.nn.relu(tf.matmul(local3, weights) + biases, name=scope.name)
    _activation_summary(local4)

  # softmax, i.e. softmax(WX + b)
  with tf.variable_scope('softmax_linear') as scope:
    weights = _variable_with_weight_decay('weights', [192, NUM_CLASSES],
                                          stddev=1/192.0, wd=0.0)
    biases = _variable_on_cpu('biases', [NUM_CLASSES],
                              tf.constant_initializer(0.0))
    softmax_linear = tf.add(tf.matmul(local4, weights), biases, name=scope.name)
    _activation_summary(softmax_linear)

  return softmax_linear


def loss(logits, labels):
  """Add L2Loss to all the trainable variables.

  Add summary for for "Loss" and "Loss/avg".
  Args:
    logits: Logits from inference().
    labels: Labels from distorted_inputs or inputs(). 1-D tensor
            of shape [batch_size]

  Returns:
    Loss tensor of type float.
  """
  # Reshape the labels into a dense Tensor of
  # shape [batch_size, NUM_CLASSES].
  sparse_labels = tf.reshape(labels, [FLAGS.batch_size, 1])
  indices = tf.reshape(tf.range(FLAGS.batch_size), [FLAGS.batch_size, 1])
  concated = tf.concat([indices, sparse_labels],1)
  dense_labels = tf.sparse_to_dense(concated,
                                    [FLAGS.batch_size, NUM_CLASSES],
                                    1.0, 0.0)

  # Calculate the average cross entropy loss across the batch.
  cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
    logits = logits, labels = dense_labels, name ='cross_entropy_per_example')
  cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
  tf.add_to_collection('losses', cross_entropy_mean)

  # The total loss is defined as the cross entropy loss plus all of the weight
  # decay terms (L2 loss).
  return tf.add_n(tf.get_collection('losses'), name='total_loss')


def _add_loss_summaries(total_loss):
  """Add summaries for losses in CIFAR-10 model.

  Generates moving average for all losses and associated summaries for
  visualizing the performance of the network.

  Args:
    total_loss: Total loss from loss().
  Returns:
    loss_averages_op: op for generating moving averages of losses.
  """
  # 计算所有亏损的移动平均数和总亏损。
  loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg')
  losses = tf.get_collection('losses')
  loss_averages_op = loss_averages.apply(losses + [total_loss])

  # 附上所有个人损失和全部损失的标量总结; do the
  # 计算平均版本的损失相同。
  for l in losses + [total_loss]:
    # 将每个损失命名为 '(raw)' 并命名移动平均版本的损失
    # 作为原始的损失名称。
    tf.summary.scalar(l.op.name +' (raw)', l)
    tf.summary.scalar(l.op.name, loss_averages.average(l))

  return loss_averages_op


def train(total_loss, global_step):
  """Train CIFAR-10 model.

  Create an optimizer and apply to all trainable variables. Add moving
  average for all trainable variables.

  Args:
    total_loss: Total loss from loss().
    global_step: Integer Variable counting the number of training steps
      processed.
  Returns:
    train_op: op for training.
  """
  # 影响学习率的变数。
  num_batches_per_epoch = NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN / FLAGS.batch_size
  decay_steps = int(num_batches_per_epoch * NUM_EPOCHS_PER_DECAY)

  # 根据步数,以指数方式衰减学习速率。
  lr = tf.train.exponential_decay(INITIAL_LEARNING_RATE,
                                  global_step,
                                  decay_steps,
                                  LEARNING_RATE_DECAY_FACTOR,
                                  staircase=True)
  tf.summary.scalar('learning_rate', lr)

  # 生成所有损失和相关汇总的移动平均线。
  loss_averages_op = _add_loss_summaries(total_loss)

  # 计算 gradients.
  with tf.control_dependencies([loss_averages_op]):
    opt = tf.train.GradientDescentOptimizer(lr)
    grads = opt.compute_gradients(total_loss)

  # 运行 gradients
  apply_gradient_op = opt.apply_gradients(grads, global_step=global_step)

  # 为可训练变量添加直方图。
  for var in tf.trainable_variables():
    tf.summary.histogram(var.op.name, var)

  # 为渐变添加直方图。
  for grad, var in grads:
    if grad is not None:
      tf.summary.histogram(var.op.name + '/gradients', grad)

  # 跟踪所有可训练变量的移动平均值。
  variable_averages = tf.train.ExponentialMovingAverage(
      MOVING_AVERAGE_DECAY, global_step)
  variables_averages_op = variable_averages.apply(tf.trainable_variables())

  with tf.control_dependencies([apply_gradient_op, variables_averages_op]):
    train_op = tf.no_op(name='train')

  return train_op


def maybe_download_and_extract():
  """Download and extract the tarball from Alex's website."""
  dest_directory = FLAGS.data_dir
  if not os.path.exists(dest_directory):
    os.makedirs(dest_directory)
  filename = DATA_URL.split('/')[-1]
  filepath = os.path.join(dest_directory, filename)
  if not os.path.exists(filepath):
    def _progress(count, block_size, total_size):
      sys.stdout.write('\r>> Downloading %s %.1f%%' % (filename,
          float(count * block_size) / float(total_size) * 100.0))
      sys.stdout.flush()
    filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath,
                                             reporthook=_progress)
    print()
    statinfo = os.stat(filepath)
    print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')
    tarfile.open(filepath, 'r:gz').extractall(dest_directory)


cifar10_train.py

# -*- coding: utf-8 -*-
"""A binary to train CIFAR-10 using a single GPU.

Accuracy:
cifar10_train.py achieves ~86% accuracy after 100K steps (256 epochs of
data) as judged by cifar10_eval.py.

Speed: With batch_size 128.

System        | Step Time (sec/batch)  |     Accuracy
------------------------------------------------------------------
1 Tesla K20m  | 0.35-0.60              | ~86% at 60K steps  (5 hours)
1 Tesla K40m  | 0.25-0.35              | ~86% at 100K steps (4 hours)

Usage:
Please see the tutorial and website for how to download the CIFAR-10
data set, compile the program and train the model.

http://tensorflow.org/tutorials/deep_cnn/
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from datetime import datetime
import os.path
import time

import tensorflow.python.platform
from tensorflow.python.platform import gfile

import numpy as np
from six.moves import xrange
import tensorflow as tf

import cifar10

FLAGS = tf.app.flags.FLAGS

tf.app.flags.DEFINE_string('train_dir', '/tmp/cifar10_train',
                           """Directory where to write event logs """
                           """and checkpoint.""")
tf.app.flags.DEFINE_integer('max_steps', 1000000,
                            """Number of batches to run.""")
tf.app.flags.DEFINE_boolean('log_device_placement', False,
                            """Whether to log device placement.""")


def train():
  """Train CIFAR-10 for a number of steps."""
  with tf.Graph().as_default():
    global_step = tf.Variable(0, trainable=False)

    # 获取CIFAR-10的图像和标签。
    images, labels = cifar10.distorted_inputs()

    logits = cifar10.inference(images)

    # 计算损失。
    loss = cifar10.loss(logits, labels)

    # 构建一个图形,用一批示例和示例来训练模型
    # 更新模型参数。
    train_op = cifar10.train(loss, global_step)

    # Create a saver.
    saver = tf.train.Saver(tf.all_variables())

    summary_op = tf.summary.merge_all()

    init = tf.initialize_all_variables()

    # 开始运行
    sess = tf.Session(config=tf.ConfigProto(
        log_device_placement=FLAGS.log_device_placement))
    sess.run(init)

    # 开始 queue runners.
    tf.train.start_queue_runners(sess=sess)

    summary_writer = tf.summary.FileWriter(FLAGS.train_dir,
                                            graph_def=sess.graph_def)

    for step in xrange(FLAGS.max_steps):
      start_time = time.time()
      _, loss_value = sess.run([train_op, loss])
      duration = time.time() - start_time

      assert not np.isnan(loss_value), 'Model diverged with loss = NaN'

      if step % 10 == 0:
        num_examples_per_step = FLAGS.batch_size
        examples_per_sec = num_examples_per_step / duration
        sec_per_batch = float(duration)

        format_str = ('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                      'sec/batch)')
        print (format_str % (datetime.now(), step, loss_value,
                             examples_per_sec, sec_per_batch))

      if step % 100 == 0:
        summary_str = sess.run(summary_op)
        summary_writer.add_summary(summary_str, step)

      # 保存模型
      if step % 1000 == 0 or (step + 1) == FLAGS.max_steps:
        checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt')
        saver.save(sess, checkpoint_path, global_step=step)


def main(argv=None): 
  cifar10.maybe_download_and_extract()
  if gfile.Exists(FLAGS.train_dir):
    gfile.DeleteRecursively(FLAGS.train_dir)
  gfile.MakeDirs(FLAGS.train_dir)
  train()


if __name__ == '__main__':
  tf.app.run()


#15

使用AlexNet模型,训练图片分类器,并评估准确率。(图片分类数据集:CIFAR-10 dataset 链接:http://www.cs.toronto.edu/~kriz/cifar.html)

卷积神经网络

概观

CIFAR-10分类是机器学习一个共同的基准问题。的问题是跨10个类别RGB 32×32像素的图像进行分类:

airplane, automobile, bird, cat, deer, dog, frog, horse, ship, and truck.

文件 目的
cifar10_input.py 读取本地CIFAR-10二进制文件格式。
cifar10.py 构建CIFAR-10模型。
cifar10_train.py 训练CPU或GPU上的CIFAR-10模型。
cifar10_multi_gpu_train.py 训练上的多GPU的CIFAR-10模型。
cifar10_eval.py 评估一个CIFAR-10模型的预测性能。

CIFAR-10 Model

所述CIFAR-10网络主要是包含在 cifar10.py。完整的训练图包含大约765操作。我们发现,我们可以通过构建具有以下模块的图形使代码可重用大部分:

1.模型输入: inputs()distorted_inputs()分别添加读操作和预处理CIFAR图像进行评估和培训。

2.模型预测: inference() 补充说,提供的图像进行推理,即分类操作。

3.模型训练: loss()train()添加计算损失,渐变,变量更新和可视化操作的摘要。

Model Inputs

该模型的输入部分由函数构建inputs()和 distorted_inputs()从CIFAR-10的二进制数据文件中读取图像。这些文件包含固定字节长度的记录,所以我们使用 tf.FixedLengthRecordReader

图像被处理如下

他们被裁剪为24×24像素,集中评估或 随机进行培训。

他们大约变白 ,使模型不敏感的动态范围。

对于训练,我们还采用了一系列随机扭曲,人为地增加了数据集的大小:

随机翻转图像左到右。

随机扭曲的图像亮度。

随机扭曲的图像对比度。

Model Prediction

该模型的预测部分由构成inference()这增加操作,以计算函数logits预测的。该模型的那部分组织如下:

图层名称 描述
conv1 卷积和整流线性活化。
pool1 最大池。
norm1 局部反应正常化。
conv2 卷积和整流线性活化。
norm2 局部反应正常化。
pool2 最大池。
local3 完全连接与整流线性激活层。
local4 完全连接与整流线性激活层。
softmax_linear 线性变换,以产生logits。

下面是从TensorBoard描述推理操作中产生的曲线图:

练习:的输出inference是un-normalized logits。尝试编辑的网络架构返回使用标准化的预测 tf.nn.softmax。

inputs()和inference() 函数提供所有必要的执行模型的评估的组件。我们现在转移我们对建筑施工重点培养的模式。

练习:在该模型体系结构 inference() 从在指定的CIFAR-10模型的不同稍有 CUDA的convnet。特别是,亚历克斯的原始模型的顶层本地连接并没有完全连接。尝试编辑架构以精确地再现本地连接架构的顶层。

模型训练

用于训练网络执行N路分类通常的方法是 多项式回归,又名。SOFTMAX回归。使用SoftMax回归施加 SOFTMAX非线性到网络的输出,并计算 交叉熵 的归一化预测和一个之间 1-hot encoding 标签。对于正规化,我们也在应用平常 权衰减损失所有学习型变量。该模型的目标函数是交叉熵损失与所有这些权衰减项的总和,由返回的loss()功能。

我们用它想象中TensorBoard ==tf.summary.scalar:==

2

我们使用标准的训练模型 梯度下降 算法与学习率 呈指数衰减 随着时间的推移。

3

train() 函数将需要减少通过计算梯度和更新学习型变量的目标的操作(见 ==tf.train.GradientDescentOptimizer== 详情)。它返回执行训练和更新模型一个批次的图像所需要的所有计算的操作。

Launching and Training the Model

我们已经建立了模型,现在让我们来启动它并运行该脚本的操作培训cifar10_train.py

注意:您运行在CIFAR-10教程的任何目标的第一次,CIFAR-10数据集自动下载。该数据集是160MB〜所以你可能想抓住的一个咖啡一杯快速为你的第一次运行。

该脚本报告每10步的总损耗,以及在其中最后一批数据进行处理的速度。需要注意几点:

  • 首批数据的可极为缓慢(例如几分钟)作为预处理线程填满改组队列20000个处理CIFAR图像。
  • 报损是最新一批的平均损失。请记住,这损失是交叉熵和所有权衰减项的总和。
  • 保持在一个批次的处理速度的眼睛。Tesla K40c得到上面所示的编号。如果您在CPU上运行,预计性能下降。

练习:实验时,有时让人恼火的第一堂训练步骤可能需要这么长时间。尝试减少最初填补了队列的图像数量。搜索min_fraction_of_examples_in_queue 在cifar10_input.py。

从返回的终端文本cifar10_train.py提供了最小的洞察模型是如何训练。我们希望更深入地了解培训期间模型:

Is the loss really decreasing or is that just noise? Is the model being provided appropriate images? Are the gradients, activations and weights reasonable? What is the learning rate currently at?

TensorBoard提供了这种功能,显示从周期性地导出的数据cifar10_train.py经由 tf.summary.FileWriter

例如,我们可以看激活和稀疏的程度如何分配local3训练过程中的功能演变:

4

5

Evaluating a Model

现在让我们来评估训练模型表现如何在保持了数据集。该模型是由脚本进行评估cifar10_eval.py。它构建与模型inference()函数和使用所有10,000张评估集合CIFAR-10。它计算在1精度:顶部预测多久图像的正确标签相匹配。

为了监测模型训练过程中如何改进,评估脚本定期运行由创建最新的检查点文件cifar10_train.py

python cifar10_eval.py

要小心,不要运行在同一个GPU的评估和培训二进制,否则你可能会耗尽内存。考虑运行评估在一个单独的GPU(如果可用)或同时在同一GPU上运行评价暂停训练二进制文件。

你应该看到的输出:

代码演示


ImportError: No module named models.image.cifar10

将原代码中的

   from tensorflow.models.image.cifar10 import cifar10

改为:

  import cifar10
  import cifar10_eval 
  import cifar10_input

用自己修改后的cifar10代码,不用tensorflow库中的。

(ps: :sweat_smile:6级没通过 好多翻译 是借助谷歌翻译的 所以难免 有所疏忽 敬请谅解 )


#16

下次,过6级就容易了,:smile:


#17

:stuck_out_tongue_closed_eyes: 嘿嘿,谢谢你