8. Dropout and Strides For Larger Models

Intro

这是深度学习第8课。

本科结束后,你将会理解并知道如何使用:

  • Stride lengths来快速建立模型并减少内存消耗;
  • Dropout 来对抗过拟合;

这两个技术在大型模型中很有用。

Lesson

[1]

from IPython.display import YouTubeVideo
YouTubeVideo('fwNLf4t7MR8', width=800, height=450)

Sample Code

[2]

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from tensorflow.python import keras
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropoutimg_rows, img_cols = 28, 28
num_classes = 10def data_prep(raw):out_y = keras.utils.to_categorical(raw.label, num_classes)num_images = raw.shape[0]x_as_array = raw.values[:,1:]x_shaped_array = x_as_array.reshape(num_images, img_rows, img_cols, 1)out_x = x_shaped_array / 255return out_x, out_ytrain_size = 30000
train_file = "../input/digit-recognizer/train.csv"
raw_data = pd.read_csv(train_file)x, y = data_prep(raw_data)model = Sequential()
model.add(Conv2D(30, kernel_size=(3, 3),strides=2,activation='relu',input_shape=(img_rows, img_cols, 1)))
model.add(Dropout(0.5))
model.add(Conv2D(30, kernel_size=(3, 3), strides=2, activation='relu'))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))model.compile(loss=keras.losses.categorical_crossentropy,optimizer='adam',metrics=['accuracy'])
model.fit(x, y,batch_size=128,epochs=2,validation_split = 0.2)
/opt/conda/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.from ._conv import register_converters as _register_convertersTrain on 33600 samples, validate on 8400 samples
Epoch 1/2
33600/33600 [==============================] - 14s 408us/step - loss: 0.6243 - acc: 0.8026 - val_loss: 0.2235 - val_acc: 0.9354
Epoch 2/2
33600/33600 [==============================] - 14s 417us/step - loss: 0.2664 - acc: 0.9176 - val_loss: 0.1315 - val_acc: 0.9630<tensorflow.python.keras._impl.keras.callbacks.History at 0x7f58d9f92c88>

 

Exercise: Dropout and Strides For Larger Models

Introduction

您已经构建了一个模型来识别MNIST for Fashion数据集中的服装类型。 现在,您将使模型更大,指定更大的stride lengths并应用dropout。 这些更改将使您的模型更快,更准确。

这是深度学习课程的最后一步。

Starter Code

Data Preparation

你需要运行以下单元

【3】

import numpy as np
from sklearn.model_selection import train_test_split
from tensorflow.python import kerasimg_rows, img_cols = 28, 28
num_classes = 10def prep_data(raw, train_size, val_size):y = raw[:, 0]out_y = keras.utils.to_categorical(y, num_classes)x = raw[:,1:]num_images = raw.shape[0]out_x = x.reshape(num_images, img_rows, img_cols, 1)out_x = out_x / 255return out_x, out_yfashion_file = "../input/fashionmnist/fashion-mnist_train.csv"
fashion_data = np.loadtxt(fashion_file, skiprows=1, delimiter=',')
x, y = prep_data(fashion_data, train_size=50000, val_size=5000)

Sample Model Code

[4]

fashion_model = Sequential()
fashion_model.add(Conv2D(12, kernel_size=(3, 3), strides=2,activation='relu',input_shape=(img_rows, img_cols, 1)))
fashion_model.add(Conv2D(12, (3, 3), strides=2, activation='relu'))
fashion_model.add(Flatten())
fashion_model.add(Dense(128, activation='relu'))
fashion_model.add(Dense(num_classes, activation='softmax'))fashion_model.compile(loss=keras.losses.categorical_crossentropy,optimizer='adam',metrics=['accuracy'])fashion_model.fit(train_x, train_y,batch_size=batch_size,epochs=epochs,validation_split = 0.2)

Adding Strides

指定,编译和拟合模型,与上面的模型非常相似,但为每个卷积层指定步长为2。 调用你的新模型fashion_model_1。

【5】

from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropoutfashion_model_1 = Sequential()
# Specify the rest of the model# Compile fashion_model_1# Fit fashion_model_1

Make Model Larger

您应该已经注意到fashion_model_1训练得非常快。 这样可以使模型更大。 指定名为fashion_model_2的新模型,该模型与fashion_model_1相同,但以下情况除外:

  1.      在Flatten图层之前添加一个额外的Conv2D图层。 使它类似于你已经拥有的Conv2D图层,除了不在这个新图层中设置步幅长度(我们已经用现有图层缩小了表示)。
  2.      将每个卷积层中的过滤器数量更改为24。

指定fashion_model_2后,编译并拟合它。

【6】

# Your code for fashion_model_2 below

Add Dropout

指定fashion_model_3,它与fashion_model_2相同,只是它在每个卷积层之后立即添加了丢失(因此它增加了3次丢失)。 编译并拟合此模型。 将模型在验证数据上的性能与之前的模型进行比较。

【7】

# Your code for fashion_model_3 below

Congrats

你已经完成了深度学习课程的第1级。 您可以使用工具来创建和调整计算机视觉模型。 选择一个项目并尝试你的技能。
您可能尝试的一些有趣的数据集包括:

  •      书面信件识别
  •      花卉鉴定
  •      猫与狗
  •      10只猴子
  •      从X射线预测骨龄

你学到了很多东西。 在深度学习中还有很多东西需要学习,但你应该对自己的新技能感到满意。

Conclusion

练习结束后,您已完成深度学习课程。 你已经可以做一些很棒的事了,当我们发布它时你就可以开始升级。 2级将扩展您可以做的许多新类型的应用程序。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440249.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【POJ - 2255】Tree Recovery (给定树的先序中序,输出后序)

题干&#xff1a; Input The input will contain one or more test cases. Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique…

Java基础部分快速复习(以前复习的时候放在自己的新浪博客上)

工作后Java基本没有用到&#xff0c;有时候自己也会把基础过一遍&#xff0c;下面的链接是以前重温的时候整理的Java基础最核心部分的知识点和代码示例放在自己的新浪博客上&#xff0c;如果以后有需要&#xff0c;直接从这里进入&#xff0c;可以快速复习&#xff0c;节省时间…

0.Overview——Deep Learning

本文为Kaggle Learn的Deep Learning课程的中文翻译。原链接为&#xff1a;https://www.kaggle.com/learn/deep-learning 1. Intro to Deep Learning and Computer Vision A quick overview of how models work on images 2. Building Models from Convolutions Scale up fr…

SQL基础用法总结(以前复习的时候放在自己的新浪博客上)

工作后主要做移动端和前端这一块&#xff0c;后端的知识都是靠自己学习积累的&#xff0c;下面的链接是以前重温的时候整理的SQL基础最核心部分的知识点和代码示例放在自己的新浪博客上&#xff0c;如果以后有需要&#xff0c;直接从这里进入&#xff0c;可以快速复习&#xff…

【HDU - 4786 】Fibonacci Tree (最小生成树变形,上下界贪心,tricks)

题干&#xff1a; Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do some research on Spanning Tree. So Coach Pang decides to solve the following problem:   Consider a bidirectional graph G with N vertices and M edges. All edg…

网络编程懒人入门(四):快速理解TCP和UDP的差异

转自即时通讯网&#xff1a;http://www.52im.net/ 原作者&#xff1a;MeloDev&#xff0c;本文由即时通讯网重新修订发布&#xff0c;感谢原作者的无私分享。 1、前言 对于即时通讯开发者新手来说&#xff0c;在开始着手编写IM或消息推送系统的代码前&#xff0c;最头疼的问…

【HDU - 5455】Fang Fang(水题,有坑)

题干&#xff1a; Fang Fang says she wants to be remembered. I promise her. We define the sequence FF of strings. F0 ‘‘f",F0 ‘‘f", F1 ‘‘ff",F1 ‘‘ff", F2 ‘‘cff",F2 ‘‘cff", Fn Fn−1 ‘‘f", for n &g…

npm安装与卸载和cordova及ionic项目打包调试等相关命令总结归纳

一、安装和卸载node和npm的命令 1、Mac系统彻底卸载npmsudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}2、因网络问题导致包安装失败的安全解决方法cd到对应项目之下&#xff0c;如果有权限问题前加sudorm -rf node_modules/npm c…

Apollo自动驾驶入门课程第⑤讲 — 感知(下)

目录 1. 卷积神经网络 2. 检测与分类 3. 跟踪 4. 分割 5. Apollo感知 6. 传感器数据比较 7. 感知融合策略 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 8月29日 上一篇文章中&#xff0c;我们发布了无人驾驶技术的 感知篇…

【 HDU - 5459】Jesus Is Here(dp)

题干&#xff1a; Ive sent Fang Fang around 201314 text messages in almost 5 years. Why cant she make sense of what I mean? But Jesus is here!" the priest intoned. Show me your messages." Fine, the first message is s1‘‘c"s1‘‘c" a…

TextRank算法原理和提取关键词的主要过程详解 计算句子相似度 计算句子重要性公式

1、TextRank计算句子相似度和句子重要性的公式 2、TextRank算法提取关键词的过程 3、TextRank建立关键词无向图

Apollo自动驾驶入门课程第⑥讲 — 预测

目录 1. 简介 2. 不同的预测方式 3. 基于车道序列的预测 4. 障碍物状态 5. 预测目标车道 6. 递归神经网络 7. 递归神经网络在目标车道预测的应用 8. 轨迹生成 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a;涛涛CV Apollo开发者社区 9月6日 上一篇文…

使用PDF.js实现前端和手机端网页预览PDF文件(可定制,支持本地文件、Base64编码和远程URL跨域方式)

1.插件下载地址&#xff1a;https://mozilla.github.io/pdf.js/ 下载后解压pdfjs-1.10.88-dist.zip文件后得到&#xff1a; 2.把pdfjs-1.10.88-dist放到项目静态资源中&#xff0c;在自己的页面中通过iframe链接到pdfjs-1.10.88-dist/web/viewer.html文件中。 3.访问自己的页…

【HDU - 5878】I Count Two Three(打表)

题干&#xff1a; I will show you the most popular board game in the Shanghai Ingress Resistance Team. It all started several months ago. We found out the home address of the enlightened agent Icount2three and decided to draw him out. Millions of missile…

移动互联网浩荡十年 有的升腾,有的陨落

原创&#xff1a; 颜西龙 猎云网 &#xff08;ilieyun&#xff09;1周前 中国移动互联网的十年&#xff0c;是波澜壮阔、荡气回肠的十年。本文回溯了这段历史&#xff0c;在这十年间里&#xff0c;有的企业升腾&#xff0c;有的企业陨落。 2011年8月16日&#xff0c;北京798艺术…

原生JS动态计算输入框文本内容的宽度,当内容宽度超过输入框的宽度时可控

需求场景&#xff1a;左边输入框输入内容&#xff0c;右边输入框用placeholder展示&#xff0c;当placeholder的内容宽度超过右边输入框的宽度时&#xff0c;placeholder强行替换为“请选择” 注意事项&#xff1a;1、左右输入框的大小、样式都无关&#xff1b; 2、实际业务中…

【HDU - 5881】Tea(思维,找规律)

题干&#xff1a; Tea is good. Tea is life. Tea is everything. The balance of tea is a journey of pursuing balance of the universe. Alice knows that. Alice wants to teach you the art of pouring tea. Alice has a pot of tea. The exact volume of tea is…

Apollo自动驾驶入门课程第⑦讲 — 规划(上)

目录 1. 规划简介 2. 将地图转为图形 3. 路径查找算法&#xff1a;A* 4. 轨迹生成 5. Fernet坐标系 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 9月13日 上周我们发布了无人驾驶技术的 预测篇&#xff0c;简要介绍了预测的…

JS正则表达式常见场景下的用法总结

&#xff08;一&#xff09;前置知识总结&#xff1a; 1. 正则表达式 /xxxx/[标识] 其中的标识含义 •g &#xff08;全文查找&#xff09; •i &#xff08;忽略大小写&#xff09; •m &#xff08;多行查找&#xff09; 2. 正则表达式创建的两种方式&#xff08;等价&#…

【HDU - 5882】Balanced Game (找规律,思维)

题干&#xff1a; Rock-paper-scissors is a zero-sum hand game usually played between two people, in which each player simultaneously forms one of three shapes with an outstretched hand. These shapes are "rock", "paper", and "scisso…