不得不说优达学城的课程作为入门还真是不错,打算明年买一个纳米课程试一下。

说明

任务2可以说是真正开始进入深度学习的领域
还是用任务1处理好的数据集

任务分为3个阶段

  • 梯度下降算法(线性分类器)
  • 批随机梯度下降算法(线性分类器)
  • 一层隐藏层

数据集

使用noMNIST数据集
基于机器性能及运行时间的考量
训练集大小:30000,测试集大小:10000

注意点

该代码要求tensorflow >=0.12
可以用下面这行代码测试tensorflow版本

python -c 'import tensorflow as tf; print(tf.__version__)'  # for Python 2
python3 -c 'import tensorflow as tf; print(tf.__version__)'  # for Python 3

梯度下降算法(线性分类器)

代码

# These are all the modules we'll be using later. Make sure you can import them
# before proceeding further.
from __future__ import print_function
import numpy as np
import tensorflow as tf
from six.moves import cPickle as pickle
from six.moves import rangepickle_file = 'notMNIST.pickle'with open(pickle_file, 'rb') as f:save = pickle.load(f)train_dataset = save['train_dataset']train_labels = save['train_labels']valid_dataset = save['valid_dataset']valid_labels = save['valid_labels']test_dataset = save['test_dataset']test_labels = save['test_labels']del save  # hint to help gc free up memoryprint('Training set', train_dataset.shape, train_labels.shape)print('Validation set', valid_dataset.shape, valid_labels.shape)print('Test set', test_dataset.shape, test_labels.shape)image_size = 28
num_labels = 10def reformat(dataset, labels):dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32)# Map 0 to [1.0, 0.0, 0.0 ...], 1 to [0.0, 1.0, 0.0 ...]labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)return dataset, labels
train_dataset, train_labels = reformat(train_dataset, train_labels)
valid_dataset, valid_labels = reformat(valid_dataset, valid_labels)
test_dataset, test_labels = reformat(test_dataset, test_labels)
print('Training set', train_dataset.shape, train_labels.shape)
print('Validation set', valid_dataset.shape, valid_labels.shape)
print('Test set', test_dataset.shape, test_labels.shape)# With gradient descent training, even this much data is prohibitive.
# Subset the training data for faster turnaround.
train_subset = 10000graph = tf.Graph()
with graph.as_default():# Input data.# Load the training, validation and test data into constants that are# attached to the graph.tf_train_dataset = tf.constant(train_dataset[:train_subset, :])tf_train_labels = tf.constant(train_labels[:train_subset])tf_valid_dataset = tf.constant(valid_dataset)tf_test_dataset = tf.constant(test_dataset)# Variables.# These are the parameters that we are going to be training. The weight# matrix will be initialized using random values following a (truncated)# normal distribution. The biases get initialized to zero.weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels]))biases = tf.Variable(tf.zeros([num_labels]))# Training computation.# We multiply the inputs with the weight matrix, and add biases. We compute# the softmax and cross-entropy (it's one operation in TensorFlow, because# it's very common, and it can be optimized). We take the average of this# cross-entropy across all training examples: that's our loss.logits = tf.matmul(tf_train_dataset, weights) + biasesloss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels))# Optimizer.# We are going to find the minimum of this loss using gradient descent.optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)# Predictions for the training, validation, and test data.# These are not part of training, but merely here so that we can report# accuracy figures as we train.train_prediction = tf.nn.softmax(logits)valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases)test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases)num_steps = 801def accuracy(predictions, labels):return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))/ predictions.shape[0])with tf.Session(graph=graph) as session:# This is a one-time operation which ensures the parameters get initialized as# we described in the graph: random weights for the matrix, zeros for the# biases. tf.global_variables_initializer().run()print('Initialized')for step in range(num_steps):# Run the computations. We tell .run() that we want to run the optimizer,# and get the loss value and the training predictions returned as numpy# arrays._, l, predictions = session.run([optimizer, loss, train_prediction])if (step % 100 == 0):print('Loss at step %d: %f' % (step, l))print('Training accuracy: %.1f%%' % accuracy(predictions, train_labels[:train_subset, :]))# Calling .eval() on valid_prediction is basically like calling run(), but# just to get that one numpy array. Note that it recomputes all its graph# dependencies.print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels))print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels))

运行结果

批梯度下降算法(线性分类器)

代码

# These are all the modules we'll be using later. Make sure you can import them
# before proceeding further.
from __future__ import print_function
import numpy as np
import tensorflow as tf
from six.moves import cPickle as pickle
from six.moves import rangepickle_file = 'notMNIST.pickle'with open(pickle_file, 'rb') as f:save = pickle.load(f)train_dataset = save['train_dataset']train_labels = save['train_labels']valid_dataset = save['valid_dataset']valid_labels = save['valid_labels']test_dataset = save['test_dataset']test_labels = save['test_labels']del save  # hint to help gc free up memoryprint('Training set', train_dataset.shape, train_labels.shape)print('Validation set', valid_dataset.shape, valid_labels.shape)print('Test set', test_dataset.shape, test_labels.shape)image_size = 28
num_labels = 10def reformat(dataset, labels):dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32)# Map 0 to [1.0, 0.0, 0.0 ...], 1 to [0.0, 1.0, 0.0 ...]labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)return dataset, labels
train_dataset, train_labels = reformat(train_dataset, train_labels)
valid_dataset, valid_labels = reformat(valid_dataset, valid_labels)
test_dataset, test_labels = reformat(test_dataset, test_labels)
print('Training set', train_dataset.shape, train_labels.shape)
print('Validation set', valid_dataset.shape, valid_labels.shape)
print('Test set', test_dataset.shape, test_labels.shape)batch_size = 128def accuracy(predictions, labels):return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))/ predictions.shape[0])graph = tf.Graph()
with graph.as_default():# Input data. For the training data, we use a placeholder that will be fed# at run time with a training minibatch.tf_train_dataset = tf.placeholder(tf.float32,shape=(batch_size, image_size * image_size))tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))tf_valid_dataset = tf.constant(valid_dataset)tf_test_dataset = tf.constant(test_dataset)# Variables.weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels]))biases = tf.Variable(tf.zeros([num_labels]))# Training computation.logits = tf.matmul(tf_train_dataset, weights) + biasesloss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels))# Optimizer.optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)# Predictions for the training, validation, and test data.train_prediction = tf.nn.softmax(logits)valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases)test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases)num_steps = 3001with tf.Session(graph=graph) as session:tf.global_variables_initializer().run()print("Initialized")for step in range(num_steps):# Pick an offset within the training data, which has been randomized.# Note: we could use better randomization across epochs.offset = (step * batch_size) % (train_labels.shape[0] - batch_size)# Generate a minibatch.batch_data = train_dataset[offset:(offset + batch_size), :]batch_labels = train_labels[offset:(offset + batch_size), :]# Prepare a dictionary telling the session where to feed the minibatch.# The key of the dictionary is the placeholder node of the graph to be fed,# and the value is the numpy array to feed to it.feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}_, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict)if (step % 500 == 0):print("Minibatch loss at step %d: %f" % (step, l))print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels))print("Validation accuracy: %.1f%%" % accuracy(valid_prediction.eval(), valid_labels))print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels))

运行结果

一层隐藏层

代码

# These are all the modules we'll be using later. Make sure you can import them
# before proceeding further.
from __future__ import print_function
import numpy as np
import tensorflow as tf
from six.moves import cPickle as pickle
from six.moves import rangepickle_file = 'notMNIST.pickle'with open(pickle_file, 'rb') as f:save = pickle.load(f)train_dataset = save['train_dataset']train_labels = save['train_labels']valid_dataset = save['valid_dataset']valid_labels = save['valid_labels']test_dataset = save['test_dataset']test_labels = save['test_labels']del save  # hint to help gc free up memoryprint('Training set', train_dataset.shape, train_labels.shape)print('Validation set', valid_dataset.shape, valid_labels.shape)print('Test set', test_dataset.shape, test_labels.shape)image_size = 28
num_labels = 10def reformat(dataset, labels):dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32)# Map 0 to [1.0, 0.0, 0.0 ...], 1 to [0.0, 1.0, 0.0 ...]labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)return dataset, labels
train_dataset, train_labels = reformat(train_dataset, train_labels)
valid_dataset, valid_labels = reformat(valid_dataset, valid_labels)
test_dataset, test_labels = reformat(test_dataset, test_labels)
print('Training set', train_dataset.shape, train_labels.shape)
print('Validation set', valid_dataset.shape, valid_labels.shape)
print('Test set', test_dataset.shape, test_labels.shape)batch_size = 128def accuracy(predictions, labels):return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))/ predictions.shape[0])graph = tf.Graph()
with graph.as_default():# Input data. For the training data, we use a placeholder that will be fed# at run time with a training minibatch.tf_train_dataset = tf.placeholder(tf.float32,shape=(batch_size, image_size * image_size))tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))tf_valid_dataset = tf.constant(valid_dataset)tf_test_dataset = tf.constant(test_dataset)hidden_nodes = 1024# Variables.weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes]))biases1 = tf.Variable(tf.zeros([hidden_nodes]))weights2 = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels]))biases2 = tf.Variable(tf.zeros([num_labels])) # Training computation.logits = tf.matmul(tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1), weights2) + biases2loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels))# Optimizer.optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)# Predictions for the training, validation, and test data.train_prediction = tf.nn.softmax(logits)valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1), weights2) + biases2)test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1), weights2) + biases2)num_steps = 3001with tf.Session(graph=graph) as session:tf.global_variables_initializer().run()print("Initialized")for step in range(num_steps):# Pick an offset within the training data, which has been randomized.# Note: we could use better randomization across epochs.offset = (step * batch_size) % (train_labels.shape[0] - batch_size)# Generate a minibatch.batch_data = train_dataset[offset:(offset + batch_size), :]batch_labels = train_labels[offset:(offset + batch_size), :]# Prepare a dictionary telling the session where to feed the minibatch.# The key of the dictionary is the placeholder node of the graph to be fed,# and the value is the numpy array to feed to it.feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}_, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict)if (step % 500 == 0):print("Minibatch loss at step %d: %f" % (step, l))print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels))print("Validation accuracy: %.1f%%" % accuracy(valid_prediction.eval(), valid_labels))print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels))

运行结果

优达学城 深度学习 任务2相关推荐

  1. 优达学城 深度学习 任务1

    这几天刚好有环境,打算学习一下深度学习 看了一圈介绍,发现优达学城的深度学习课程作为入门课程还是不错的 今天看了第一章节的视频,顺便做了任务1 任务1难度不大,按照网站上的说明可以完成下载.打包等工作 ...

  2. 优达学城-深度学习笔记(一)

    优达学城-深度学习笔记(一) 标签: 机器学习 优达学城-深度学习笔记一 一 神经网络简介 最大似然概率 交叉熵Cross entropy 1交叉熵代码实现 2多类别交叉熵 对数几率回归的误差函数co ...

  3. 优达学城深度学习之三(上)——卷积神经网络

    学习如何用神经网络来解决分类问题. 开始都会说什么是机器学习?机器学习的应用是什么?用机器在海量数据中学习得到可以解决一类问题的办法,这就是我的理解.图像处理.文本处理.无人驾驶.等,深度学习最热门的 ...

  4. 优达学城深度学习之六——TensorFlow卷积神经网络

    TensorFlow卷积层 TensorFlow 提供了 tf.nn.conv2d() 和 tf.nn.bias_add() 函数来创建你自己的卷积层. # Output depth k_output ...

  5. 优达学城深度学习之七——TensorFlow卷积神经网络

    一.胶囊网络 池化运算会丢失一些图像信息.这是因为为了获得更小的特征级图像表示,池化会丢弃像素信息.与池化层相比,有一些分类方法不会丢弃空间信息,而是学习各个部分之间的关系(例如眼睛.鼻子和嘴之间的空 ...

  6. 优达学城深度学习(之四)——jupyter notebook

    Jupyter notebook 是什么? 欢迎学习本课程--如何使用 Jupyter notebook.Jupyter notebook 是一种 Web 应用,能让用户将说明文本.数学方程.代码和可 ...

  7. 优达学城深度学习之五——卷积神经网络

    梯度下降算法推导与实现 import matplotlib.pyplot as plt import numpy as np import pandas as pd#Some helper funct ...

  8. 优达学城深度学习之三(下)——卷积神经网络

    一.One-Hot编码 计算机在表示多结果的分类时,使用One-Hot编码是比较常见的处理方式.即每个对象都有对应的列. 二.最大似然率 下面是两幅图像,比较两幅图像,试通过概率的方法来讨论一下为什么 ...

  9. 优达学城深度学习之二——矩阵数学和Numpy复习

    一.数据维度 维度(scalar),张量(Tensor).3表示零维张量,[1 2 3]表示一维张量,矩阵表示二维张量,任何大于二维张量就叫张量(Tensor).如下图所示: 二.Numpy简介 2. ...

最新文章

  1. Linux的epoll
  2. 《Windows驱动开发技术详解》读书笔记(一)
  3. c语言----预处理
  4. NFS方式挂载rootfs的设置方法
  5. android package.xml,Android自动化编译设置AndroidManifest.xml中package值(包名)
  6. .NET的轻量级IOC框架芮双随笔
  7. 搭建Silverlight2.0开发环境
  8. Transformer好文章阅读链接
  9. linux6.5 yum命令,CentOS6.5使用yum命令方便快捷安装Nginx
  10. vSAN 内存或 SSD 拥堵已达到阈值限制 (2071384)
  11. arduino的矩阵示例程序_用树莓派 Arduino 制造 LED 矩阵彩灯
  12. 一周水题集锦 2017 9.4
  13. 阿里云Kubernetes SpringCloud 实践进行时(5): 弹性服务与容错处理
  14. 计算机系制作网页,613331-付军科-计算机系网页设计与制作实训报告【荐】.doc
  15. 介绍一本好书《海量数据库解决方案》
  16. 2021年最新林学类期刊JCR影响因子及分区
  17. c语言编程自幂数,【C语言基础】-自幂数优化-这个算法快得像一道闪电
  18. python 爬取淘宝网课
  19. 网站广告1像素1元,超有创意的百万像素网站
  20. 克鲁斯卡尔算法(Kruskal)求最小生成树(MST)过程详解

热门文章

  1. js中box和box()的区别
  2. 小程序超出文字显示为省略号(代码简单的无话可说)
  3. 如何做分销网站的关键词研究
  4. Go语言头秃之路(七)
  5. vue开发环境及项目1
  6. 《微力无边》经典语录
  7. 老闪创业那些事儿(70)——SaaS的新玩法
  8. 《求医不如求己》之12运动功法 (自家珍藏)(转载)
  9. APPSO 视频号-空白昵称(全系统通用)
  10. IDEA 本地调试spark程序 Exception in thread main java.lang.NoSuchMethodError: scala.collection.immutable.