使用Keras / Tensorflow对数值的特征进行分类

Some problems are really complex (and maybe impossible!) to solve only with classical imperative programming. This short and simple project will explain, how easy and effortless these problems could be solved with machine learning methods, in particular with deep learning and a neural network.

有些问题真的很复杂(也许不可能!),仅使用经典命令式编程即可解决。 这个简短而简单的项目将说明,使用机器学习方法(尤其是深度学习和神经网络)可以轻松轻松地解决这些问题。

Let’s assume, you get a series of numerical values and these values are representing a curve (or the fragment) one of these three basic geometric shape types:

假设您获得了一系列数值,这些值代表了这三种基本几何形状类型之一的曲线(或片段):

You may think: “Oh that’s pretty easy: The upper left one is not a circle, it’s a parabola!”

您可能会想:“哦,这很容易:左上角不是圆,而是抛物线!”

But in most cases, you won’t get a beautiful plot, you will rather get a bunch of x/y values as a vector or a tensor which have to be processed very fast.

但是在大多数情况下,您不会得到漂亮的图,您宁愿获得一堆x / y值作为向量或张量,这些值必须非常快地处理。

[-8.75110346699612,-0.9645611495889332,-11.2332236496635,-0.42621292318119675,-17.053561559907408,4.09121179786454,-26.212117197727828,12.587713013548267,-38.70889056312478,25.063290723870004,-54.54388165609825,41.517944928829735,-73.71709047664822,61.95167562842744,-96.22851702477477,86.36448282266319,-122.07816130047782,114.75636651153694,-151.26602330375735,147.12732669504862]

This is a parabola, too! ;)

这也是抛物线! ;)

Ok, let’s take a look at the “classical approach”. Identifying a straight line is not difficult: Calculate the slope at two different sample points. If the values are equal, it’s a line. But how to distinguish a parabola from a circle? Or a parabola from another unknown exponential function? I don’t know…

好的,让我们看一下“经典方法”。 识别一条直线并不困难:计算两个不同采样点的斜率。 如果值相等,则为一行。 但是如何区分抛物线和圆呢? 还是来自另一个未知指数函数的抛物线? 我不知道…

With the help of a simple neural network (NN), this challenge becomes way easier! The NN don’t need any plot image to “learn” about the characteristics of a mathematical function. It just needs training data. LOTS of training data!

借助简单的神经网络(NN),这一挑战变得更加轻松! NN不需要任何绘图图像即可“学习”数学函数的特征。 它只需要训练数据。 很多培训数据!

(All source code used in this article is available in my github repo: https://github.com/jschw/Keras-Timeseries-Classification)

(本文中使用的所有源代码都可以在我的github仓库中找到: https : //github.com/jschw/Keras-Timeseries-Classification )

创建用于训练和测试的数据集 (Creating a dataset for training and testing)

Let’s start with creating some random sample values. For example, the function to create a parabola looks like this:

让我们从创建一些随机样本值开始。 例如,创建抛物线的函数如下所示:

Basically, the following mathematical equations are written down as functions with optional parameters which can be loaded with random values.

基本上,以下数学方程式作为具有可选参数的函数写下来,这些参数可以加载随机值。

Parabola:

抛物线:

y = a*x^2 + b*x + c

Circle:

圈:

x = r * cos(theta) + Center_x,y = r * sin(theta) + Center_y

Line:

线:

y = a*x + b

To get more variants, all curves can be rotated by a given angle parameter.

为了获得更多的变体,可以将所有曲线旋转给定的角度参数。

A few parabola samples can be easily created:

可以很容易地创建一些抛物线样本:

Image for post
Three random parabola samples
三个随机抛物线样本

After creating a lot of samples (1000 each class), the data is being packed and stored as a CSV file. Ten samples of each class are stored in a separate file for testing the NN after training with some new (unknown) data. Each line contains a classlabel. This is important for the training, because it’s supervised learning.

创建大量示例(每个类1000个)后,将数据打包并存储为CSV文件。 每个类别的十个样本存储在一个单独的文件中,用于在训练了一些新的(未知)数据后测试NN。 每行包含一个classlabel。 这对于培训很重要,因为它是监督学习。

Image for post
Preview of the csv file content
CSV文件内容预览

数据预处理和神经网络训练 (Data preprocessing and training the NN)

The next step is, to read in this CSV file and put all the data to a dataframe:

下一步是读取此CSV文件并将所有数据放入数据框:

Preprocess the data:

预处理数据:

  • Split the data into values (X) and labels (Y)

    将数据分为值(X)和标签(Y)
  • Encode the label strings (one-hot-encoding is used)

    编码标签字符串(使用单热编码)

Now we can build the model and train it.

现在我们可以构建模型并对其进行训练。

Image for post
The NN architecure visualized with Netron
用Netron可视化NN架构

With the Netron viewer (https://github.com/lutzroeder/netron), we can take a look into the neural network model architecture.

使用Netron查看器( https://github.com/lutzroeder/netron ),我们可以了解神经网络模型的体系结构。

As you can see, the structure isn’t very complex, only five fully connected Dense layers alternating with a Dropout layer are used. The capacity of the NN is enough for this task (14.371 trainable parameters).

如您所见,结构不是很复杂,仅使用五个完全连接的Dense层和一个Dropout层交替使用。 NN的容量足以完成此任务(14.371个可训练参数)。

The input layer expects the shape (1,20) what’s equal to 10 sample points (each point includes 2 values, x and y).

输入层期望形状(1,20)等于10个采样点(每个点包括2个值x和y)。

Output is a vector with three values, each for one classes confidence value.

输出是一个具有三个值的向量,每个值代表一类置信度值。

After the model was trained for 40 epochs, it reaches an impressive accuracy of nearly 98%! That’s a very good accuracy, but keep in mind, real-world values (e.g. measured by a sensor) won’t be that clean and accuracy will drop.

将模型训练了40个纪元后,它达到了近98%的惊人准确性! 这是一个非常好的精度,但是请记住,现实世界的值(例如,由传感器测量的)不会干净,精度会下降。

You probably want to test your trained model with some samples of the test dataset or other values. No problem, just load and encode the test dataset as shown above. Then choose a random or specific sample:

您可能想用一些测试数据集样本或其他值来测试您训练的模型。 没问题,只需加载并编码测试数据集即可,如上所示。 然后选择一个随机样本或特定样本:

x_data and y_data are the separated x and y values of the curve in order to plot the curve.

x_datay_data是曲线的分开的x和y值,以便绘制曲线。

The values in predict_arr can be fed directly into the model:

可以将predict_arr中的值直接输入模型中:

If everything works flawlessly, you will get an output like this:

如果一切正常,您将获得如下输出:

Image for post
Example output for a circle sample
圆样本的示例输出

The NN has predicted correctly a circle with a confidence of 99.65 % in 0.035 ms. Processing times may vary on different systems (depends on CPU power or if a GPU is installed).

NN在0.035毫秒内以99.65%的置信度正确预测了一个圆。 处理时间可能因不同系统而异(取决于CPU能力或是否安装了GPU)。

结论 (Conclusion)

This simple implementation of a neural network points out how new technologies like machine learning (and in particular deep learning) can extend common methods of data processing.

神经网络的这种简单实现指出了诸如机器学习(特别是深度学习)之类的新技术如何扩展数据处理的通用方法。

You may think, to classify a parabola or circle isn’t transferable on real-world problems. But YES, it is, of course! E.g. for recognising a specific movement or to identify a sensor value pattern which can indicate an imminent failure.

您可能会认为,对抛物线或圆进行分类在实际问题上是不可转移的。 但是,是的,当然! 例如,用于识别特定运动或识别可以指示即将发生故障的传感器值模式。

Usage of this deep learning model in an IoT device alongside an industrial PLC environment is shown in one of my other articles: Jetson Nano Communication with PLC

我的另一篇文章介绍了这种深度学习模型在IoT设备和工业PLC环境中的使用情况: Jetson Nano与PLC的通信

翻译自: https://medium.com/analytics-vidhya/classifiy-the-characteristics-of-numerical-values-with-keras-tensorflow-d5f6bf0e29f6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值