2019-09-02 23:41:59 Fancyyj 阅读数 94
  • 人工智能-机器学习实战视频课程

    本课程使用Python3讲解,同时适应于Windows、Mac OS X和Linux。本课程是《机器学习系列课程》中的重要部分。这套视频课程包括但不限于Python基础、常用机器学习框架(如scikit-learn、tensorflow、pytorch、caffe、keras等),机器学习核心算法、大量的实战案例、机器学习的数学基础,机器学习在自然语言处理中的应用、机器学习在推荐系统中的应用。

    1692 人正在学习 去看看 李宁

Keras基本概念

Keras —— 简洁高效的深度神经网络工具
Keras 是一个python深度学习框架,可以方便定义和训练几乎所有类型的深度学习模型
Keras 最开始是为研究人员 开发的,目的是为了能够快速实验

Keras 的重要特性

  1. 相同 的代码可以在CPU 和 GPU上无缝的切换运行
  2. 具有友好的api ,便于快速开发深度学习模型的原理
  3. 内置支持卷积网络(用于机器视觉),循环网络(用于序列处理) 以及二者的任意组合
  4. 支持任意架构网络,多输入或者多层输出模型,层共享,模型共享,也就是说,Keras能构建任意深度学习模型
  5. Keras 基于宽松的MIT许可认证办法,在商业项目中可以免费使用,并且所有python版本都兼容

Keras 架构

Keras是一个模型级(model—level)的库,为开发深度学习模块提供了高层次的构建模型。
Keras是依赖于一个专门的,高度优化的张量库来完成运算,这个张量库就是Keras的后端引擎。目前Keras有三个后端实现: Tensorflow,Theano ,和微软认知工具包
在这里插入图片描述

Keras工作流

(1)定义训练数据: 输入张量和目标张量
(2)定义层组成的网络(或者模型),将输入映射到目标
(3)配置学习过程:选择损失函数,优化器和需要监控的指标
(4)调用模型的fit方法在训练数据上进行迭代

定义模型的方法
(1)使用Sequential类(仅用于层的线性堆叠,这是目前最常见的网络构架)
在这里插入图片描述
(2)函数式API 用于层组成的 有向无环图,可以构建任意形式的架构
在这里插入图片描述

Keras的编译和训练

Keras的编译: 编译也就是配置学习过程,可以指定模型使用的优化器和损失函数以及训练过程中想要监控的指标
在这里插入图片描述

Keras的训练
通过fit() 方法将输入数据的numpy数组(和对应的目标数据)传入模型,这个做法和Stick—learn 及其他机器学习库类似
在这里插入图片描述

2018-04-18 08:49:29 weixin_34209851 阅读数 49
  • 人工智能-机器学习实战视频课程

    本课程使用Python3讲解,同时适应于Windows、Mac OS X和Linux。本课程是《机器学习系列课程》中的重要部分。这套视频课程包括但不限于Python基础、常用机器学习框架(如scikit-learn、tensorflow、pytorch、caffe、keras等),机器学习核心算法、大量的实战案例、机器学习的数学基础,机器学习在自然语言处理中的应用、机器学习在推荐系统中的应用。

    1692 人正在学习 去看看 李宁

    在前面的博文中,我们分享了《基于scikit-learn机器学习库的分类预测》,本文将分享Keras机器学习库的分类预测。    

    一旦你在Keras中选择好机器学习模型,就可以用它来预测新的数据实例。初学者经常会有这样的疑问:

如何在Keras中用我自己的模型进行预测?

在本教程中,你将会发现如何在Keras Python库的机器学习模型进行分类和回归预测。文章结构如下:

1.如何构建一个模型,为预测做好准备。

2.如何在Keras库中进行类别和概率预测。

3.如何在Keras库中进行回归预测。

738a4b8df373dc063c96229b6bf9dc519d922994 

一、构建一个模型

在进行预测之前,你必须训练一个最终模型。可以使用k-fold交叉验证或训练/测试数据,对模型进行训练。这样做的目的就是为了评估模型在样本外数据上的表现及其性能,比如新的数据。

你可以在这里了解更多关于如何训练最终模型的信息:

如何训练一个最终的机器学习模型

二、对类别进行预测

分类问题,就是模型学习输入特征与输出特征之间的映射,确定某一个或多个数据实例是否为某一个类标签的问题,比如“是垃圾邮件”和“不是垃圾邮件”。

下面是在Keras中针对一个简单二元分类问题最终模型的示例代码。如何在Keras中开发一个神经网络模型,请看下面这篇文章:

手把手教你在Keras中用Python开发一个神经网络。

8596f220cbd8b8efbfb95de1f40d12b5768a7fad 

模型构建完成以后,需要将模型保存到文件中,如Keras API。保存后,你可以随时加载模型并使用它进行预测。有关这方面的详细内容,请参阅以下文章:

保存并加载你的Keras深度学习模型。

现在,我们希望用最终模型进行两种分类预测:类别预测和概率预测。

1、类别预测

类别预测就是给定最终模型和一个或多个数据实例,使用模型对预测数据实例进行分类。

首先我们并不知道新数据的类别。这就是我们需要这个模型的原因。

Keras库的最终分类模型中,使用predict_classes()函数来预测新数据实例的所属类别。请注意,predict_classes()函数仅适用于Sequential模型,并不适用于使用function API开发的模型。

例如,数组Xnew中有一个或多个数据实例,将这个(这些)实例传递给模型的predict_classes()函数,以便预测数组中每个实例所属的类别。

ccfd75abf0f1aa13128422bf4a0048bbe7820e92 

预测代码如下:

9621a0b9aed36c99406b72056184428d75dc6751 

运行上述代码,对三个新的数据实例的类别进行预测,并将新的数据实例和预测结果打印出来,如下图所示。

40754222dd2253238c57241e2c3668f82c81b2c3 

如果只有一个新的数据实例,则可以直接将放入数组中,并传递给predict_classes()函数,如下图所示。

5a31300cdf372a2273bf6b9b1b9394591a36edac 

运行该实例,并打印该实例数据和预测结果,如下图所示。

133d81945d9c9b3fbc162677e4b00d941c4e9918 

关于类别标签的注意事项

准备好数据后,可能使用过LabelEncoder将图像的类别(例如字符串型)映射为整型。也可以用LabelEncoder中的inverse_transform()函数将整型再转换回字符串型。出于这个原因,在拟合最终模型时,您可能想要在pickle库中保存用于编码输出预测结果的LabelEncoder

2、概率预测

概率预测就是预测每个数据实例所属类别的概率。给定一个或多个新实例,该模型将预测每个数据所属类别的概率,并返回01之间的某一个值。

你可以在scikit-learn中调用predict_proba()函数进行这些实例的类别概率的预测,如下图所示:

e0238a7e7006a2ae2c34f61dc597b6448dd96d9d 

在两种类别(二元)的情况下,输出层通常使用S形激活函数。预测概率用来描述实例数据属于第一类的可能性。

在多种类别的情况下,输出层通常使用softmax激活函数,并返回所属类别的可能性。

下面的代码是对数据实例数组Xnew中的每个实例进行概率预测。

5470ee985ade110d10c4901cb7f799c38735f592 

运行上述代码进行概率预测,并打印出输入数据实例以及每个实例数据属于第一类的概率。

7aae0e40685f683a159391fdc0394eb92069f2d3 

三、如何使用回归模型进行预测

回归属于监督学习,给定输入实例数据,模型学习并将数据数据映射到一个合适的输出量,例如0.10.2等。

一个Keras最终的回归模型如下所示。

6e88121077b5bf77743f30b27ad8611ec3e5ed14 

我们可以在最终模型上调用predict()函数来预测所述类别的概率。和分类一样,predict()函数将一个或多个数据实例的列表或数组作为输入。

下面的例子为在多个类别的新数据实例上进行回归预测。

5bfa4abcf3999c377b1ee29aa95c6100ea7d7d04 

运行上述代码,对多个数据实例进行预测,并将输出和预测结果打印出来,如下图所示。

cc48068a6420e5db85934df3cf5b492cabe38cc0 

将相同的函数用于适当的列表或数组中,就可以对单个数据实例进行预测,举例如下。

2eb1477b3b0751b861a6201186874853c8896b47 

运行上述代码,并打印出数据实例和预测结果,如下图所示。

fbd6e068a41e1540f137572dd098c57d8212f5bd 

拓展阅读

如果您希望深入了解使用Keras进行分类预测,请查看以下更为详细的资源:

1.如何训练一个最终的机器学习模型?

2.保存并加载Keras深度学习模型。

3.手把手教你在Keras中用Python开发一个神经网络。

4.Keras中长期短期记忆模型的5个生命周期。

6.如何在Keras中使用长时间短期记忆模型进行预测。

 

数十款阿里云产品限时折扣中,赶紧点击领劵开始云上实践吧!

以上为译文。

本文由北邮@爱可可-爱生活 老师推荐,阿里云云栖社区组织翻译。

文章原标题《How to Make Predictions with scikit-learn》,译者:Mags,审校:袁虎。

文章为简译,更为详细的内容,请查看原文 

 

2017-07-10 11:48:49 tumi678 阅读数 1933
  • 人工智能-机器学习实战视频课程

    本课程使用Python3讲解,同时适应于Windows、Mac OS X和Linux。本课程是《机器学习系列课程》中的重要部分。这套视频课程包括但不限于Python基础、常用机器学习框架(如scikit-learn、tensorflow、pytorch、caffe、keras等),机器学习核心算法、大量的实战案例、机器学习的数学基础,机器学习在自然语言处理中的应用、机器学习在推荐系统中的应用。

    1692 人正在学习 去看看 李宁

1.首先安装Ubuntu14.04,这是Ubuntu系列最稳定的版本。

安装驱动,有条件使用GPU的,采用GPU加速。

2.安装GPU驱动过程:

ctrl+Alt+F1输入账号密码

sudo stop lightdm

sudo su

sudo apt-get --purge remove nvidia-*

./NVIDIA(tab一下)......--no-opengl-files

sudo start lightdm

3.配置keras

Keras使用了下面的依赖包:

·        numpyscipy

·        pyyaml

·        HDF5, h5py(可选,仅在模型的save/load函数中使用)

·        如果使用CNN的推荐安装cuDNN

当使用TensorFlow为后端时:

·        TensorFlow

当使用Theano作为后端时:

·        Theano

后端翻译自backend,指的是Keras依赖于完成底层的张量运算的软件包。

安装Keras时,请cdKeras的文件夹中,并运行下面的安装命令:

sudopython setup.py install

你也可以使用PyPI来安装Keras

sudopip install keras

Ubuntu初始环境设置

·        安装开发包打开终端输入:

#系统升级

>>>sudo apt update

>>>sudo apt upgrade

#安装python基础开发包

>>>sudo apt install -y python-dev python-pip python-nose gcc g++ git gfortran vim

·        安装运算加速库打开终端输入:

>>>sudo apt install -y libopenblas-dev liblapack-dev libatlas-base-dev

3. CUDA开发环境的搭建(CPU加速跳过)

如果您的仅仅采用cpu加速,可跳过此步骤 -下载CUDA8.0

请采用GPU加速的NVIDA官网步骤进行GPU加速

Keras框架搭建

相关开发包安装,根据你是采用GPU还是采用CPU选择相应命令

终端中输入:

>>> sudo pip install -U --pre pip setuptools wheel

>>> sudo pip install -U --pre numpy scipymatplotlib scikit-learn scikit-image

>>> sudo pip install -U --pre tensorflow-gpu

>>>sudopip install -U --pre theano

# >>> sudo pip install -U --pre tensorflow

>>> sudo pip install -U --pre keras==1.2.2

特别说明:keras版本若不指定,将会默认最高版本,运行有些程序识别不了

 

修改keras后端,在终端输入:

>>>gedit ~/.keras/keras.json

安装完毕后,输入python,然后输入:

>>>import tensorflow

>>>import keras

无错输出即可

配置theano文件,在终端中输入:

>>>gedit ~/.theanorc

若是采用GPU加速的,写入以下

 [global]

openmp=False

device = gpu

floatX = float32

allow_input_downcast=True

[lib]

cnmem = 0.8

[blas]

ldflags= -lopenblas

[nvcc]

fastmath = True

若是采用CPU加速版本,则.theanorc文件配置如下:

[global]

openmp=True

device = cpu

floatX = float32

allow_input_downcast=True

[blas]

ldflags= -lopenblas

之后可以验证keras是否安装成功,在命令行中输入Python命令进入Python变成命令行环境:

>>>import keras

没有报错,并且会打印出关于显卡信息以及cnmem等信息(CPU版本没有),那么keras就安装成功了

加速测试

速度测试

新建一个文件 test.py,内容为:

from theano import function, config,shared, sandbox

import theano.tensor as T

import numpy

import time

vlen = 10 * 30 * 768 # 10 x #cores x #threads per core

iters = 1000

rng = numpy.random.RandomState(22)

x = shared(numpy.asarray(rng.rand(vlen),config.floatX))

f = function([], T.exp(x))

print(f.maker.fgraph.toposort())

t0 = time.time()

for i in xrange(iters):

r = f()

t1 = time.time()

print("Looping %d times took %fseconds" % (iters, t1 - t0))

print("Result is %s" % (r,))

if numpy.any([isinstance(x.op,T.Elemwise) for x in f.maker.fgraph.toposort()]):

print('Used the cpu')

else:

print('Used the gpu')

GTX 970显卡下,输出结果大概是0.21秒,在一百倍运算量下19秒,可以进行对比。理论上,相比

较主频为3.3GHzCPU,加速比应该是75倍,但不同的ssd和内存限制了IO接口传输速度。

Keras mnist数据集测试

下载Keras开发包

git clonehttps://github.com/fchollet/keras.git

cd keras/examples/

python mnist_mlp.py

程序无错进行,至此,keras安装完成。

输入:

>>>keras.__version__

注意:上面每个下划线为两个

Keras安装原作者联系方式:scp173.cool@gmail.com.

4.安装pycharm

5.安装opencv2.4.9

参照:http://www.linuxdiyf.com/linux/28899.html

https://www.douban.com/note/521352400/

1.安装ffmpeg

git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg

cd source_directory

./configure--enable-shared --disable-static

make

sudo makeinstall

检查是否安装成功

ffmpeg

2.安装opencv

安装之前要先装好一堆依赖包,参考官网http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html

安装过程出现问题可以参考部分解决方案

 

注意:必须先安装ffmpeg

官网显示的这一步很重要:

2Enter the<cmake_binary_dir> and type

cmake [<someoptional parameters>] <path to the OpenCVsource directory>

For example

cd ~/opencv

mkdir release

cd release

cmake -D CMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local ..

 

也可参照以下安装opencv3.0的博客:

http://blog.csdn.net/linj_m/article/details/45048905

安装完成后,输入以下查看opencv版本:

pkg-config --modversionopencv

 

将opencv源码包下的example/c拷贝出来,执行./build_all.sh

结果如下:

如果打不开输入sh ./build_all.sh

 

6安装h5py

>>>sudopip install h5py

2019-04-30 15:08:00 weixin_30756499 阅读数 45
  • 人工智能-机器学习实战视频课程

    本课程使用Python3讲解,同时适应于Windows、Mac OS X和Linux。本课程是《机器学习系列课程》中的重要部分。这套视频课程包括但不限于Python基础、常用机器学习框架(如scikit-learn、tensorflow、pytorch、caffe、keras等),机器学习核心算法、大量的实战案例、机器学习的数学基础,机器学习在自然语言处理中的应用、机器学习在推荐系统中的应用。

    1692 人正在学习 去看看 李宁

参考:

编程环境:

  • 操作系统:win7 - CPU

  • anaconda-Python3-jupyter notebook

  • tersonFlow:1.10.0

  • Keras:2.2.4

背景:

  • 视频里宝可梦大师提供的部分参数设置不能得到好的结果,这里记录一下后续调参

1-载入数据报错的问题:

2-测试程序是否正常

  • 打印一下某个矩阵的形状,没有报错一切正常

    1265453-20190430212158490-1922493739.png

3-运行原始代码

  • 按照李宏毅老师视频里讲的全是坑代码运行看看效果

    1265453-20190430213044434-1780956342.png
    1265453-20190430213704014-1071621832.png

4-对神经网络进行调参

改动地方主要为:

  • 激励函数由sigmoid改为relu

  • loss function由mse改为categorical_crossentropy

  • 增加了Dropout,防止过拟合

  • 改动后的代码为

# 选model
model = Sequential()

# 搭建神经网络
# 改动:4点
# 1-中间层units由633改为700
# 2-激活函数由sigmoid改为relu
# 3-原四个Dense,删去一个中间层,只留三个Dense
# 4-在三个Dense的每两个Dense中间加入Dropout
# batch-epochs=100,20时,三Dense好于四Dense
# batch-epochs=1,2时,三Dense好于四Dense
model.add(Dense(input_dim=28*28, units=700, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(units=700, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(units=10, activation='softmax'))

# 自选loss Function等参数
# 改动:1点
# 1-loss function由mse改为categorical_crossentropy
model.compile(loss='mse', optimizer=SGD(lr=0.1), metrics=['accuracy'])

# train模型
# 改动:1点
# 1-参数列表最后加一个validation_split(交叉验证?)
# 2-batch-size=1, epochs=2时,acc变为0.9314(3个Dense),0.9212(4个Dense)
model.fit(x_train, y_train, batch_size=1, epochs=2, validation_split=0.05)

# 测试结果,并打印accuracy
result = model.evaluate(x_test, y_test)
print('\nTest loss:', result[0])
print('\nAccuracy:', result[1])

5-结果分析

  • 貌似相同参数,不同train回合,得到的结果还有细微的差别。。。

    1265453-20190430222838983-23775594.png

总结:

  • 无论如何,总算DL-hello world达成了,好的开始就是成功的一半,再接再厉!

END

转载于:https://www.cnblogs.com/anliux/p/10796101.html

2019-12-07 21:46:45 cfan927 阅读数 21
  • 人工智能-机器学习实战视频课程

    本课程使用Python3讲解,同时适应于Windows、Mac OS X和Linux。本课程是《机器学习系列课程》中的重要部分。这套视频课程包括但不限于Python基础、常用机器学习框架(如scikit-learn、tensorflow、pytorch、caffe、keras等),机器学习核心算法、大量的实战案例、机器学习的数学基础,机器学习在自然语言处理中的应用、机器学习在推荐系统中的应用。

    1692 人正在学习 去看看 李宁

此笔记本(notebook)使用评论文本将影评分为积极(positive)或消极(nagetive)两类。这是一个二元(binary)或者二分类问题,一种重要且应用广泛的机器学习问题。

本教程演示了使用 Tensorflow Hub 和 Keras 进行迁移学习的基本应用。

我们将使用来源于网络电影数据库(Internet Movie Database)的 IMDB 数据集(IMDB dataset),其包含 50,000 条影评文本。从该数据集切割出的 25,000 条评论用作训练,另外 25,000 条用作测试。训练集与测试集是平衡的(balanced),意味着它们包含相等数量的积极和消极评论。

此笔记本(notebook)使用了 tf.keras,它是一个 Tensorflow 中用于构建和训练模型的高级API,此外还使用了 TensorFlow Hub,一个用于迁移学习的库和平台。有关使用 tf.keras 进行文本分类的更高级教程,请参阅 MLCC文本分类指南(MLCC Text Classification Guide)。

from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_datasets as tfds

print("Version: ", tf.__version__)
print("Eager mode: ", tf.executing_eagerly())
print("Hub version: ", hub.__version__)
print("GPU is", "available" if tf.config.experimental.list_physical_devices("GPU") else "NOT AVAILABLE")
Version:  2.0.0
Eager mode:  True
Hub version:  0.6.0
GPU is available

1 下载 IMDB 数据集

IMDB数据集可以在 Tensorflow 数据集处获取。以下代码将 IMDB 数据集下载至您的机器中:

# 将训练集按照 6:4 的比例进行切割,从而最终我们将得到 15,000
# 个训练样本, 10,000 个验证样本以及 25,000 个测试样本
train_validation_split = tfds.Split.TRAIN.subsplit([6, 4])
(train_data, validation_data), test_data = tfds.load(
    name="imdb_reviews", 
    split=(train_validation_split, tfds.Split.TEST),
    as_supervised=True)
Downloading and preparing dataset imdb_reviews (80.23 MiB) to /home/kbuilder/tensorflow_datasets/imdb_reviews/plain_text/0.1.0...

HBox(children=(IntProgress(value=1, bar_style='info', description='Dl Completed...', max=1, style=ProgressStyl…
HBox(children=(IntProgress(value=1, bar_style='info', description='Dl Size...', max=1, style=ProgressStyle(des…

HBox(children=(IntProgress(value=1, bar_style='info', max=1), HTML(value='')))

HBox(children=(IntProgress(value=0, description='Shuffling...', max=10, style=ProgressStyle(description_width=…
WARNING:tensorflow:From /home/kbuilder/.local/lib/python3.6/site-packages/tensorflow_datasets/core/file_format_adapter.py:209: tf_record_iterator (from tensorflow.python.lib.io.tf_record) is deprecated and will be removed in a future version.
Instructions for updating:
Use eager execution and: 
`tf.data.TFRecordDataset(path)`

WARNING:tensorflow:From /home/kbuilder/.local/lib/python3.6/site-packages/tensorflow_datasets/core/file_format_adapter.py:209: tf_record_iterator (from tensorflow.python.lib.io.tf_record) is deprecated and will be removed in a future version.
Instructions for updating:
Use eager execution and: 
`tf.data.TFRecordDataset(path)`

HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…

HBox(children=(IntProgress(value=1, bar_style='info', max=1), HTML(value='')))

HBox(children=(IntProgress(value=0, description='Shuffling...', max=10, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…

HBox(children=(IntProgress(value=1, bar_style='info', max=1), HTML(value='')))

HBox(children=(IntProgress(value=0, description='Shuffling...', max=20, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
HBox(children=(IntProgress(value=1, bar_style='info', description='Reading...', max=1, style=ProgressStyle(des…
HBox(children=(IntProgress(value=0, description='Writing...', max=2500, style=ProgressStyle(description_width=…
Dataset imdb_reviews downloaded and prepared to /home/kbuilder/tensorflow_datasets/imdb_reviews/plain_text/0.1.0. Subsequent calls will reuse this data.

2 探索数据

让我们花一点时间来了解数据的格式。每一个样本都是一个表示电影评论和相应标签的句子。该句子不以任何方式进行预处理。标签是一个值为 0 或 1 的整数,其中 0 代表消极评论,1 代表积极评论。

我们来打印下前十个样本。

train_examples_batch, train_labels_batch = next(iter(train_data.batch(10)))
train_examples_batch
<tf.Tensor: id=219, shape=(10,), dtype=string, numpy=
array([b"As a lifelong fan of Dickens, I have invariably been disappointed by adaptations of his novels.<br /><br />Although his works presented an extremely accurate re-telling of human life at every level in Victorian Britain, throughout them all was a pervasive thread of humour that could be both playful or sarcastic as the narrative dictated. In a way, he was a literary caricaturist and cartoonist. He could be serious and hilarious in the same sentence. He pricked pride, lampooned arrogance, celebrated modesty, and empathised with loneliness and poverty. It may be a clich\xc3\xa9, but he was a people's writer.<br /><br />And it is the comedy that is so often missing from his interpretations. At the time of writing, Oliver Twist is being dramatised in serial form on BBC television. All of the misery and cruelty is their, but non of the humour, irony, and savage lampoonery. The result is just a dark, dismal experience: the story penned by a journalist rather than a novelist. It's not really Dickens at all.<br /><br />'Oliver!', on the other hand, is much closer to the mark. The mockery of officialdom is perfectly interpreted, from the blustering beadle to the drunken magistrate. The classic stand-off between the beadle and Mr Brownlow, in which the law is described as 'a ass, a idiot' couldn't have been better done. Harry Secombe is an ideal choice.<br /><br />But the blinding cruelty is also there, the callous indifference of the state, the cold, hunger, poverty and loneliness are all presented just as surely as The Master would have wished.<br /><br />And then there is crime. Ron Moody is a treasure as the sleazy Jewish fence, whilst Oliver Reid has Bill Sykes to perfection.<br /><br />Perhaps not surprisingly, Lionel Bart - himself a Jew from London's east-end - takes a liberty with Fagin by re-interpreting him as a much more benign fellow than was Dicken's original. In the novel, he was utterly ruthless, sending some of his own boys to the gallows in order to protect himself (though he was also caught and hanged). Whereas in the movie, he is presented as something of a wayward father-figure, a sort of charitable thief rather than a corrupter of children, the latter being a long-standing anti-semitic sentiment. Otherwise, very few liberties are taken with Dickens's original. All of the most memorable elements are included. Just enough menace and violence is retained to ensure narrative fidelity whilst at the same time allowing for children' sensibilities. Nancy is still beaten to death, Bullseye narrowly escapes drowning, and Bill Sykes gets a faithfully graphic come-uppance.<br /><br />Every song is excellent, though they do incline towards schmaltz. Mark Lester mimes his wonderfully. Both his and my favourite scene is the one in which the world comes alive to 'who will buy'. It's schmaltzy, but it's Dickens through and through.<br /><br />I could go on. I could commend the wonderful set-pieces, the contrast of the rich and poor. There is top-quality acting from more British regulars than you could shake a stick at.<br /><br />I ought to give it 10 points, but I'm feeling more like Scrooge today. Soak it up with your Christmas dinner. No original has been better realised.",
       b"Oh yeah! Jenna Jameson did it again! Yeah Baby! This movie rocks. It was one of the 1st movies i saw of her. And i have to say i feel in love with her, she was great in this move.<br /><br />Her performance was outstanding and what i liked the most was the scenery and the wardrobe it was amazing you can tell that they put a lot into the movie the girls cloth were amazing.<br /><br />I hope this comment helps and u can buy the movie, the storyline is awesome is very unique and i'm sure u are going to like it. Jenna amazed us once more and no wonder the movie won so many awards. Her make-up and wardrobe is very very sexy and the girls on girls scene is amazing. specially the one where she looks like an angel. It's a must see and i hope u share my interests",
       b"I saw this film on True Movies (which automatically made me sceptical) but actually - it was good. Why? Not because of the amazing plot twists or breathtaking dialogue (of which there is little) but because actually, despite what people say I thought the film was accurate in it's depiction of teenagers dealing with pregnancy.<br /><br />It's NOT Dawson's Creek, they're not graceful, cool witty characters who breeze through sexuality with effortless knowledge. They're kids and they act like kids would. <br /><br />They're blunt, awkward and annoyingly confused about everything. Yes, this could be by accident and they could just be bad actors but I don't think so. Dermot Mulroney gives (when not trying to be cool) a very believable performance and I loved him for it. Patricia Arquette IS whiny and annoying, but she was pregnant and a teenagers? The combination of the two isn't exactly lavender on your pillow. The plot was VERY predictable and but so what? I believed them, his stress and inability to cope - her brave, yet slightly misguided attempts to bring them closer together. I think the characters, acted by anyone else, WOULD indeed have been annoying and unbelievable but they weren't. It reflects the surreality of the situation they're in, that he's sitting in class and she walks on campus with the baby. I felt angry at her for that, I felt angry at him for being such a child and for blaming her. I felt it all.<br /><br />In the end, I loved it and would recommend it.<br /><br />Watch out for the scene where Dermot Mulroney runs from the disastrous counselling session - career performance.",
       b'This was a wonderfully clever and entertaining movie that I shall never tire of watching many, many times. The casting was magnificent in matching up the young with the older characters. There are those of us out here who really do appreciate good actors and an intelligent story format. As for Judi Dench, she is beautiful and a gift to any kind of production in which she stars. I always make a point to see Judi Dench in all her performances. She is a superb actress and a pleasure to watch as each transformation of her character comes to life. I can only be grateful when I see such an outstanding picture for most of the motion pictures made more recently lack good characters, good scripts and good acting. The movie public needs heroes, not deviant manikins, who lack ingenuity and talent. How wonderful to see old favorites like Leslie Caron, Olympia Dukakis and Cleo Laine. I would like to see this movie win the awards it deserves. Thank you again for a tremendous night of entertainment. I congratulate the writer, director, producer, and all those who did such a fine job.',
       b'I have no idea what the other reviewer is talking about- this was a wonderful movie, and created a sense of the era that feels like time travel. The characters are truly young, Mary is a strong match for Byron, Claire is juvenile and a tad annoying, Polidori is a convincing beaten-down sycophant... all are beautiful, curious, and decadent... not the frightening wrecks they are in Gothic.<br /><br />Gothic works as an independent piece of shock film, and I loved it for different reasons, but this works like a Merchant and Ivory film, and was from my readings the best capture of what the summer must have felt like. Romantic, yes, but completely rekindles my interest in the lives of Shelley and Byron every time I think about the film. One of my all-time favorites.',
       b"This was soul-provoking! I am an Iranian, and living in th 21st century, I didn't know that such big tribes have been living in such conditions at the time of my grandfather!<br /><br />You see that today, or even in 1925, on one side of the world a lady or a baby could have everything served for him or her clean and on-demand, but here 80 years ago, people ventured their life to go to somewhere with more grass. It's really interesting that these Persians bear those difficulties to find pasture for their sheep, but they lose many the sheep on their way.<br /><br />I praise the Americans who accompanied this tribe, they were as tough as Bakhtiari people.",
       b'Just because someone is under the age of 10 does not mean they are stupid. If your child likes this film you\'d better have him/her tested. I am continually amazed at how so many people can be involved in something that turns out so bad. This "film" is a showcase for digital wizardry AND NOTHING ELSE. The writing is horrid. I can\'t remember when I\'ve heard such bad dialogue. The songs are beyond wretched. The acting is sub-par but then the actors were not given much. Who decided to employ Joey Fatone? He cannot sing and he is ugly as sin.<br /><br />The worst thing is the obviousness of it all. It is as if the writers went out of their way to make it all as stupid as possible. Great children\'s movies are wicked, smart and full of wit - films like Shrek and Toy Story in recent years, Willie Wonka and The Witches to mention two of the past. But in the continual dumbing-down of American more are flocking to dreck like Finding Nemo (yes, that\'s right), the recent Charlie & The Chocolate Factory and eye-crossing trash like Red Riding Hood.',
       b"I absolutely LOVED this movie when I was a kid. I cried every time I watched it. It wasn't weird to me. I totally identified with the characters. I would love to see it again (and hope I wont be disappointed!). Pufnstuf rocks!!!! I was really drawn in to the fantasy world. And to me the movie was loooong. I wonder if I ever saw the series and have confused them? The acting I thought was strong. I loved Jack Wilde. He was so dreamy to an 10 year old (when I first saw the movie, not in 1970. I can still remember the characters vividly. The flute was totally believable and I can still 'feel' the evil woods. Witchy poo was scary - I wouldn't want to cross her path.",
       b'A very close and sharp discription of the bubbling and dynamic emotional world of specialy one 18year old guy, that makes his first experiences in his gay love to an other boy, during an vacation with a part of his family.<br /><br />I liked this film because of his extremly clear and surrogated storytelling , with all this "Sound-close-ups" and quiet moments wich had been full of intensive moods.<br /><br />',
       b"This is the most depressing film I have ever seen. I first saw it as a child and even thinking about it now really upsets me. I know it was set in a time when life was hard and I know these people were poor and the crops were vital. Yes, I get all that. What I find hard to take is I can't remember one single light moment in the entire film. Maybe it was true to life, I don't know. I'm quite sure the acting was top notch and the direction and quality of filming etc etc was wonderful and I know that every film can't have a happy ending but as a family film it is dire in my opinion.<br /><br />I wouldn't recommend it to anyone who wants to be entertained by a film. I can't stress enough how this film affected me as a child. I was talking about it recently and all the sad memories came flooding back. I think it would have all but the heartless reaching for the Prozac."],
      dtype=object)>

我们再打印下前十个标签。

train_labels_batch
<tf.Tensor: id=220, shape=(10,), dtype=int64, numpy=array([1, 1, 1, 1, 1, 1, 0, 1, 1, 0])>

3 构建模型

神经网络由堆叠的层来构建,这需要从三个主要方面来进行体系结构决策:

  • 如何表示文本?
  • 模型里有多少层?
  • 每个层里有多少隐层单元(hidden units)?

本示例中,输入数据由句子组成。预测的标签为 0 或 1。

表示文本的一种方式是将句子转换为嵌入向量(embeddings vectors)。我们可以使用一个预先训练好的文本嵌入(text embedding)作为首层,这将具有三个优点:

  • 我们不必担心文本预处理
  • 我们可以从迁移学习中受益
  • 嵌入具有固定长度,更易于处理

针对此示例我们将使用 TensorFlow Hub 中名为 google/tf2-preview/gnews-swivel-20dim/1 的一种预训练文本嵌入(text embedding)模型 。

为了达到本教程的目的还有其他三种预训练模型可供测试:

  • google/tf2-preview/gnews-swivel-20dim-with-oov/1 ——类似google/tf2-preview/gnews-swivel-20dim/1,但 2.5%的词汇转换为未登录词桶(OOV buckets)。如果任务的词汇与模型的词汇没有完全重叠,这将会有所帮助。
  • google/tf2-preview/nnlm-en-dim50/1 ——一个拥有约 1M 词汇量且维度为 50 的更大的模型。
  • google/tf2-preview/nnlm-en-dim128/1 ——拥有约 1M 词汇量且维度为128的更大的模型。

让我们首先创建一个使用 Tensorflow Hub 模型嵌入(embed)语句的Keras层,并在几个输入样本中进行尝试。请注意无论输入文本的长度如何,嵌入(embeddings)输出的形状都是:(num_examples, embedding_dimension)。

embedding = "https://hub.tensorflow.google.cn/google/tf2-preview/gnews-swivel-20dim/1"
hub_layer = hub.KerasLayer(embedding, input_shape=[], 
                           dtype=tf.string, trainable=True)
hub_layer(train_examples_batch[:3])
<tf.Tensor: id=402, shape=(3, 20), dtype=float32, numpy=
array([[ 3.9819887 , -4.4838037 ,  5.177359  , -2.3643482 , -3.2938678 ,
        -3.5364532 , -2.4786978 ,  2.5525482 ,  6.688532  , -2.3076782 ,
        -1.9807833 ,  1.1315885 , -3.0339816 , -0.7604128 , -5.743445  ,
         3.4242578 ,  4.790099  , -4.03061   , -5.992149  , -1.7297493 ],
       [ 3.4232912 , -4.230874  ,  4.1488533 , -0.29553518, -6.802391  ,
        -2.5163853 , -4.4002395 ,  1.905792  ,  4.7512794 , -0.40538004,
        -4.3401685 ,  1.0361497 ,  0.9744097 ,  0.71507156, -6.2657013 ,
         0.16533905,  4.560262  , -1.3106939 , -3.1121316 , -2.1338716 ],
       [ 3.8508697 , -5.003031  ,  4.8700504 , -0.04324996, -5.893603  ,
        -5.2983093 , -4.004676  ,  4.1236343 ,  6.267754  ,  0.11632943,
        -3.5934832 ,  0.8023905 ,  0.56146765,  0.9192484 , -7.3066816 ,
         2.8202746 ,  6.2000837 , -3.5709393 , -4.564525  , -2.305622  ]],
      dtype=float32)>

现在让我们构建完整模型:

model = tf.keras.Sequential()
model.add(hub_layer)
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))

model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
keras_layer (KerasLayer)     (None, 20)                400020    
_________________________________________________________________
dense (Dense)                (None, 16)                336       
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 17        
=================================================================
Total params: 400,373
Trainable params: 400,373
Non-trainable params: 0
_________________________________________________________________

层按顺序堆叠以构建分类器:

  1. 第一层是 Tensorflow Hub 层。这一层使用一个预训练的保存好的模型来将句子映射为嵌入向量(embedding vector)。我们所使用的预训练文本嵌入(embedding)模型(google/tf2-preview/gnews-swivel-20dim/1)将句子切割为符号,嵌入(embed)每个符号然后进行合并。最终得到的维度是:(num_examples, embedding_dimension)。
  2. 该定长输出向量通过一个有 16 个隐层单元的全连接层(Dense)进行管道传输。
  3. 最后一层与单个输出结点紧密相连。使用 Sigmoid 激活函数,其函数值为介于 0 与 1 之间的浮点数,表示概率或置信水平。

让我们编译模型。

3.1 损失函数与优化器

一个模型需要损失函数和优化器来进行训练。由于这是一个二分类问题且模型输出概率值(一个使用 sigmoid 激活函数的单一单元层),我们将使用 binary_crossentropy 损失函数。

这不是损失函数的唯一选择,例如,您可以选择 mean_squared_error 。但是,一般来说 binary_crossentropy 更适合处理概率——它能够度量概率分布之间的“距离”,或者在我们的示例中,指的是度量 ground-truth 分布与预测值之间的“距离”。

稍后,当我们研究回归问题(例如,预测房价)时,我们将介绍如何使用另一种叫做均方误差的损失函数。

现在,配置模型来使用优化器和损失函数:

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

4 训练模型

以 512 个样本的 mini-batch 大小迭代 20 个 epoch 来训练模型。 这是指对 x_train 和 y_train 张量中所有样本的的 20 次迭代。在训练过程中,监测来自验证集的 10,000 个样本上的损失值(loss)和准确率(accuracy):

history = model.fit(train_data.shuffle(10000).batch(512),
                    epochs=20,
                    validation_data=validation_data.batch(512),
                    verbose=1)
Epoch 1/20
30/30 [==============================] - 5s 153ms/step - loss: 0.9062 - accuracy: 0.4985 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
Epoch 2/20
30/30 [==============================] - 4s 117ms/step - loss: 0.7007 - accuracy: 0.5625 - val_loss: 0.6692 - val_accuracy: 0.6029
Epoch 3/20
30/30 [==============================] - 4s 117ms/step - loss: 0.6486 - accuracy: 0.6379 - val_loss: 0.6304 - val_accuracy: 0.6543
Epoch 4/20
30/30 [==============================] - 4s 117ms/step - loss: 0.6113 - accuracy: 0.6866 - val_loss: 0.5943 - val_accuracy: 0.6966
Epoch 5/20
30/30 [==============================] - 3s 114ms/step - loss: 0.5764 - accuracy: 0.7176 - val_loss: 0.5650 - val_accuracy: 0.7201
Epoch 6/20
30/30 [==============================] - 3s 109ms/step - loss: 0.5435 - accuracy: 0.7447 - val_loss: 0.5373 - val_accuracy: 0.7424
Epoch 7/20
30/30 [==============================] - 3s 110ms/step - loss: 0.5132 - accuracy: 0.7723 - val_loss: 0.5080 - val_accuracy: 0.7667
Epoch 8/20
30/30 [==============================] - 3s 110ms/step - loss: 0.4784 - accuracy: 0.7943 - val_loss: 0.4790 - val_accuracy: 0.7833
Epoch 9/20
30/30 [==============================] - 3s 110ms/step - loss: 0.4440 - accuracy: 0.8172 - val_loss: 0.4481 - val_accuracy: 0.8054
Epoch 10/20
30/30 [==============================] - 3s 112ms/step - loss: 0.4122 - accuracy: 0.8362 - val_loss: 0.4204 - val_accuracy: 0.8196
Epoch 11/20
30/30 [==============================] - 3s 110ms/step - loss: 0.3757 - accuracy: 0.8534 - val_loss: 0.3978 - val_accuracy: 0.8290
Epoch 12/20
30/30 [==============================] - 3s 111ms/step - loss: 0.3449 - accuracy: 0.8685 - val_loss: 0.3736 - val_accuracy: 0.8413
Epoch 13/20
30/30 [==============================] - 3s 109ms/step - loss: 0.3188 - accuracy: 0.8798 - val_loss: 0.3570 - val_accuracy: 0.8465
Epoch 14/20
30/30 [==============================] - 3s 110ms/step - loss: 0.2934 - accuracy: 0.8893 - val_loss: 0.3405 - val_accuracy: 0.8549
Epoch 15/20
30/30 [==============================] - 3s 109ms/step - loss: 0.2726 - accuracy: 0.9003 - val_loss: 0.3283 - val_accuracy: 0.8611
Epoch 16/20
30/30 [==============================] - 3s 111ms/step - loss: 0.2530 - accuracy: 0.9079 - val_loss: 0.3173 - val_accuracy: 0.8648
Epoch 17/20
30/30 [==============================] - 3s 113ms/step - loss: 0.2354 - accuracy: 0.9143 - val_loss: 0.3096 - val_accuracy: 0.8679
Epoch 18/20
30/30 [==============================] - 3s 112ms/step - loss: 0.2209 - accuracy: 0.9229 - val_loss: 0.3038 - val_accuracy: 0.8700
Epoch 19/20
30/30 [==============================] - 3s 112ms/step - loss: 0.2037 - accuracy: 0.9287 - val_loss: 0.2990 - val_accuracy: 0.8736
Epoch 20/20
30/30 [==============================] - 3s 109ms/step - loss: 0.1899 - accuracy: 0.9349 - val_loss: 0.2960 - val_accuracy: 0.8751

5 评估模型

我们来看下模型的表现如何。将返回两个值。损失值(loss)(一个表示误差的数字,值越低越好)与准确率(accuracy)。

results = model.evaluate(test_data.batch(512), verbose=2)
for name, value in zip(model.metrics_names, results):
  print("%s: %.3f" % (name, value))
49/49 - 2s - loss: 0.3163 - accuracy: 0.8651
loss: 0.316
accuracy: 0.865

这种十分朴素的方法得到了约 87% 的准确率(accuracy)。若采用更好的方法,模型的准确率应当接近 95%。

没有更多推荐了,返回首页