人工智能与Python –深度学习

本文探讨了深度学习与机器学习的区别,强调深度学习在数据处理、机器依赖、特征提取和执行效率上的优势。同时介绍了卷积神经网络(CNN)的基本原理和结构,包括CNN在图像处理中的应用。此外,还提到了使用Python和Keras进行深度学习的实践,如构建线性回归器和图像分类器。
摘要由CSDN通过智能技术生成

人工智能与Python –深度学习 (AI with Python – Deep Learning)

Artificial Neural Network (ANN) it is an efficient computing system, whose central theme is borrowed from the analogy of biological neural networks. Neural networks are one type of model for machine learning. In the mid-1980s and early 1990s, much important architectural advancements were made in neural networks. In this chapter, you will learn more about Deep Learning, an approach of AI.

人工神经网络(ANN)是一个高效的计算系统,其中心主题是从生物神经网络的类比中借鉴而来的。 神经网络是机器学习的一种模型。 在1980年代中期和1990年代初期,神经网络在架构上取得了重要的进步。 在本章中,您将了解有关深度学习(一种人工智能方法)的更多信息。

Deep learning emerged from a decade’s explosive computational growth as a serious contender in the field. Thus, deep learning is a particular kind of machine learning whose algorithms are inspired by the structure and function of human brain.

深度学习来自十年的爆炸性计算增长,成为该领域的重要竞争者。 因此,深度学习是一种特殊的机器学习,其算法受人脑的结构和功能的启发。

机器学习与深度学习 (Machine Learning v/s Deep Learning)

Deep learning is the most powerful machine learning technique these days. It is so powerful because they learn the best way to represent the problem while learning how to solve the problem. A comparison of Deep learning and Machine learning is given below −

如今,深度学习是最强大的机器学习技术。 它之所以强大,是因为他们在学习如何解决问题的同时,学习了代表问题的最佳方法。 深度学习和机器学习的比较如下-

数据依赖 (Data Dependency)

The first point of difference is based upon the performance of DL and ML when the scale of data increases. When the data is large, deep learning algorithms perform very well.

当数据规模增加时,第一点差异是基于DL和ML的性能。 当数据很大时,深度学习算法会表现出色。

机器依赖性 (Machine Dependency)

Deep learning algorithms need high-end machines to work perfectly. On the other hand, machine learning algorithms can work on low-end machines too.

深度学习算法需要高端机器才能完美运行。 另一方面,机器学习算法也可以在低端机器上工作。

特征提取 (Feature Extraction)

Deep learning algorithms can extract high level features and try to learn from the same too. On the other hand, an expert is required to identify most of the features extracted by machine learning.

深度学习算法可以提取高级功能,并尝试从中学习。 另一方面,需要专家来识别机器学习提取的大多数特征。

执行时间 (Time of Execution)

Execution time depends upon the numerous parameters used in an algorithm. Deep learning has more parameters than machine learning algorithms. Hence, the execution time of DL algorithms, specially the training time, is much more than ML algorithms. But the testing time of DL algorithms is less than ML algorithms.

执行时间取决于算法中使用的众多参数。 深度学习比机器学习算法具有更多的参数。 因此,DL算法的执行时间,特别是训练时间,比ML算法要多得多。 但是DL算法的测试时间少于ML算法。

解决问题的方法 (Approach to Problem Solving)

Deep learning solves the problem end-to-end while machine learning uses the traditional way of solving the problem i.e. by breaking down it into parts.

深度学习端到端地解决了问题,而机器学习则使用了传统的解决问题的方法,即将问题分解为多个部分。

卷积神经网络(CNN) (Convolutional Neural Network (CNN))

Convolutional neural networks are the same as ordinary neural networks because they are also made up of neurons that have learnable weights and biases. Ordinary neural networks ignore the structure of input data and all the data is converted into 1-D array before feeding it into the network. This process suits the regular data, however if the data contains images, the process may be cumbersome.

卷积神经网络与普通神经网络相同,因为它们也由具有可学习的权重和偏差的神经元组成。 普通的神经网络会忽略输入数据的结构,所有数据在输入到网络之前都将转换为一维数组。 此过程适合常规数据,但是,如果数据包含图像,则该过程可能很麻烦。

CNN solves this problem easily. It takes the 2D structure of the images into account when they process them, which allows them to extract the properties specific to images. In this way, the main goal of CNNs is to go from the raw image data in the input layer to the correct class in the output layer. The only difference between an ordinary NNs and CNNs is in the treatment of input data and in the type of layers.

CNN可以轻松解决此问题。 在处理图像时会考虑图像的2D结构,这使它们可以提取特定于图像的属性。 这样,CNN的主要目标是从输入层中的原始图像数据转到输出层中的正确类别。 普通NN和CNN之间的唯一区别在于输入数据的处理方式和层的类型。

CNN的体系结构概述 (Architecture Overview of CNNs)

Architecturally, the ordinary neural networks receive an input and transform it through a series of hidden layer. Every layer is connected to the other layer with the help of neurons. The main disadvantage of ordinary neural networks is that they do not scale well to full images.

在结构上,普通的神经网络接收输入,并通过一系列隐藏层对其进行转换。 每一层都在神经元的帮助下与另一层相连。 普通神经网络的主要缺点是它们不能很好地缩放到完整图像。

The architecture of CNNs have neurons arranged in 3 dimensions called width, height and depth. Each neuron in the current layer is connected to a small patch of the output from the previous layer. It is similar to overlaying a 𝑵×𝑵 filter on the input image. It uses M filters to be sure about getting all the details. These M filters are feature extractors which extract features like edges, corners, etc.

CNN的结构具有排列为3个维度的神经元,分别称为宽度,高度和深度。 当前层中的每个神经元都连接到前一层输出的一小块。 这类似于在输入图像上叠加𝑵×𝑵滤镜。 它使用M个过滤器来确保获取所有详细信息。 这些M过滤器是特征提取器,可提取诸如边,角等特征。

用于构造CNN的层 (Layers used to construct CNNs)

Following layers are used to construct CNNs −

以下几层用于构建CNN-

  • Input Layer − It takes the raw image data as it is.

    输入层 -直接获取原始图像数据。

  • Convolutional Layer − This layer is the core building block of CNNs that does most of the computations. This layer computes the convolutions between the neurons and the various patches in the input.

    卷积层 - 卷积层是进行大多数计算的CNN的核心构建块。 该层计算输入中神经元与各种面片之间的卷积。

  • Rectified Linear Unit Layer − It applies an activation function to the output of the previous layer. It adds non-linearity to the network so that it can generalize well to any type of function.

    整流的线性单位层 -将激活函数应用于上一层的输出。 它为网络增加了非线性,因此可以很好地推广到任何类型的功能。

  • Pooling Layer − Pooling helps us to keep only the important parts as we progress in the network. Pooling layer operates independently on every depth slice of the input and resizes it spatially. It uses the MAX function.

    池化层 -池化有助于我们在网络发展过程中仅保留重要部分。 池化层在输入的每个深度切片上独立运行,并在空间上调整其大小。 它使用MAX函数。

  • Fully Connected layer/Output layer − This layer computes the output scores in the last layer. The resulting output is of the size 𝟏×𝟏×𝑳 , where L is the number training dataset classes.

    完全连接层/输出层 -此层计算最后一层的输出分数。 结果输出的大小为𝟏×𝟏×𝑳 ,其中L是数字训练数据集类。

安装有用的Python软件包 (Installing Useful Python Packages)

You can use Keras, which is an high level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK or Theno. It is compatible with Python 2.7-3.6. You can learn more about it from https://keras.io/.

您可以使用Keras (这是高级神经网络API),它使用Python编写,并且能够在TensorFlow,CNTK或Theno之上运行。 它与Python 2.7-3.6兼容。 您可以从https://keras.io/了解更多信息。

Use the following commands to install keras −

使用以下命令安装keras-


pip install keras

On conda environment, you can use the following command −

conda环境中,可以使用以下命令-


conda install –c conda-forge keras

使用ANN构建线性回归器 (Building Linear Regressor using ANN)

In this section, you will learn how to build a linear regressor using artificial neural networks. You can use KerasRegressor to achieve this. In this example, we are using the Boston house price dataset with 13 numerical for properties in Boston. The Python code for the same is shown here −

在本节中,您将学习如何使用人工神经网络构建线性回归器。 您可以使用KerasRegressor来实现。 在此示例中,我们将波士顿房价数据集与13个数值一起用于波士顿的房地产。 此处的Python代码如下所示-

Import all the required packages as shown −

导入所有必需的软件包,如下所示:


import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold

Now, load our dataset which is saved in local directory.

现在,加载保存在本地目录中的数据集。


dataframe = pandas.read_csv("/Usrrs/admin/data.csv", delim_whitespace = True, header = None)
dataset = dataframe.values

Now, divide the data into input and output variables i.e. X and Y −

现在,将数据分为输入和输出变量,即X和Y-


X = dataset[:,0:13]
Y = dataset[:,13]

Since we use baseline neural networks, define the model −

由于我们使用基线神经网络,因此定义模型-


def baseline_model():

Now, create the model as follows −

现在,创建模型,如下所示:


model_regressor = Sequential()
model_regressor.add(Dense(13, input_dim = 13, kernel_initializer = 'normal', 
   activation = 'relu'))
model_regressor.add(Dense(1, kernel_initializer = 'normal'))

Next, compile the model −

接下来,编译模型-


model_regressor.compile(loss='mean_squared_error', optimizer='adam')
return model_regressor

Now, fix the random seed for reproducibility as follows −

现在,按照以下步骤修复随机种子的可再现性-


seed = 7
numpy.random.seed(seed)

The Keras wrapper object for use in scikit-learn as a regression estimator is called KerasRegressor. In this section, we shall evaluate this model with standardize data set.

scikit-learn中用作回归估计量的Keras包装器对象称为KerasRegressor 。 在本节中,我们将使用标准化数据集评估该模型。


estimator = KerasRegressor(build_fn = baseline_model, nb_epoch = 100, batch_size = 5, verbose = 0)
kfold = KFold(n_splits = 10, random_state = seed)
baseline_result = cross_val_score(estimator, X, Y, cv = kfold)
print("Baseline: %.2f (%.2f) MSE" % (Baseline_result.mean(),Baseline_result.std()))

The output of the code shown above would be the estimate of the model’s performance on the problem for unseen data. It will be the mean squared error, including the average and standard deviation across all 10 folds of the cross validation evaluation.

上面显示的代码输出将是针对看不见数据的问题的模型性能的估计。 这将是均方误差,包括交叉验证评估的所有10倍的平均值和标准偏差。

图像分类器:深度学习的应用 (Image Classifier: An Application of Deep Learning)

Convolutional Neural Networks (CNNs) solve an image classification problem, that is to which class the input image belongs to. You can use Keras deep learning library. Note that we are using the training and testing data set of images of cats and dogs from following link https://www.kaggle.com/c/dogs-vs-cats/data.

卷积神经网络(CNN)解决了图像分类问题,即输入图像属于哪个类别。 您可以使用Keras深度学习库。 请注意,我们正在使用来自以下链接https://www.kaggle.com/c/dogs-vs-cats/data的猫和狗图像的训练和测试数据集。

Import the important keras libraries and packages as shown −

导入重要的keras库和包,如下所示-

The following package called sequential will initialize the neural networks as sequential network.

以下称为顺序的程序包会将神经网络初始化为顺序网络。


from keras.models import Sequential

The following package called Conv2D is used to perform the convolution operation, the first step of CNN.

以下名为Conv2D的程序包用于执行卷积操作,这是CNN的第一步。


from keras.layers import Conv2D

The following package called MaxPoling2D is used to perform the pooling operation, the second step of CNN.

以下名为MaxPoling2D的程序包用于执行合并操作,这是CNN的第二步。


from keras.layers import MaxPooling2D

The following package called Flatten is the process of converting all the resultant 2D arrays into a single long continuous linear vector.

以下称为Flatten的程序包是将所有所得2D数组转换为单个长连续线性向量的过程。


from keras.layers import Flatten

The following package called Dense is used to perform the full connection of the neural network, the fourth step of CNN.

以下名为Dense的程序包用于执行神经网络的完整连接,这是CNN的第四步。


from keras.layers import Dense

Now, create an object of the sequential class.

现在,创建顺序类的对象。


S_classifier = Sequential()

Now, next step is coding the convolution part.

现在,下一步是对卷积部分进行编码。


S_classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))

Here relu is the rectifier function.

这里relu是整流器功能。

Now, the next step of CNN is the pooling operation on the resultant feature maps after convolution part.

现在,CNN的下一步是对卷积后的结果特征图进行合并操作。


S-classifier.add(MaxPooling2D(pool_size = (2, 2)))

Now, convert all the pooled images into a continuous vector by using flattering −

现在,通过使用奉承将所有合并的图像转换为连续向量-


S_classifier.add(Flatten())

Next, create a fully connected layer.

接下来,创建一个完全连接的层。


S_classifier.add(Dense(units = 128, activation = 'relu'))

Here, 128 is the number of hidden units. It is a common practice to define the number of hidden units as the power of 2.

此处,隐藏单元数为128。 通常的做法是将隐藏单位的数量定义为2的幂。

Now, initialize the output layer as follows −

现在,如下初始化输出层:


S_classifier.add(Dense(units = 1, activation = 'sigmoid'))

Now, compile the CNN, we have built −

现在,编译CNN,我们已经建立了-


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

Here optimizer parameter is to choose the stochastic gradient descent algorithm, loss parameter is to choose the loss function and metrics parameter is to choose the performance metric.

这里的优化器参数是选择随机梯度下降算法,损失参数是选择损失函数,度量参数是选择性能度量。

Now, perform image augmentations and then fit the images to the neural networks −

现在,执行图像增强,然后将图像拟合到神经网络-


train_datagen = ImageDataGenerator(rescale = 1./255,shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = 
   train_datagen.flow_from_directory(”/Users/admin/training_set”,target_size = 
      (64, 64),batch_size = 32,class_mode = 'binary')

test_set = 
   test_datagen.flow_from_directory('test_set',target_size = 
      (64, 64),batch_size = 32,class_mode = 'binary')

Now, fit the data to the model we have created −

现在,将数据拟合到我们创建的模型中-


classifier.fit_generator(training_set,steps_per_epoch = 8000,epochs = 
25,validation_data = test_set,validation_steps = 2000)

Here steps_per_epoch have the number of training images.

在这里,steps_per_epoch具有训练图像的数量。

Now as the model has been trained, we can use it for prediction as follows −

现在已经对模型进行了训练,我们可以将其用于预测,如下所示:


from keras.preprocessing import image

test_image = image.load_img('dataset/single_prediction/cat_or_dog_1.jpg', 
target_size = (64, 64))

test_image = image.img_to_array(test_image)

test_image = np.expand_dims(test_image, axis = 0)

result = classifier.predict(test_image)

training_set.class_indices

if result[0][0] == 1:
prediction = 'dog'

else:
   prediction = 'cat'

翻译自: https://www.tutorialspoint.com/artificial_intelligence_with_python/artificial_intelligence_with_python_deep_learning.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值