深度学习 keras_Keras深度学习教程

本文介绍了Keras,一个高级神经网络API,适用于Theano、TensorFlow和CNTK。Keras因其用户友好、易于学习和使用的特点而备受青睐。文中详细讲解了Keras的安装、使用,以及如何构建密集连接网络、顺序模型、共享视觉模型和视觉问答模型,并提供了训练神经网络的例子。
摘要由CSDN通过智能技术生成

深度学习 keras

什么是Keras? (What is Keras?)

Keras is a high-level neural networks API. It is written in Python and can run on top of Theano, TensorFlow or CNTK. It was developed with the idea of:

Keras是高级神经网络API。 它是用Python编写的,可以在Theano,TensorFlow或CNTK之上运行。 它的开发思想是:

Being able to go from idea to result with the least possible delay is key to doing good research.
能够以尽可能少的延迟将想法付诸实践是进行良好研究的关键。

Keras is a user-friendly, extensible and modular library which makes prototyping easy and fast. It supports convolutional networks, recurrent networks and even the combination of both.

Keras是一个用户友好,可扩展的模块化库,可轻松快速地制作原型。 它支持卷积网络,循环网络,甚至两者的组合。

Initial development of Keras was a part of the research of project ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating System).

Keras的最初开发是ONEIROS(开放式神经电子智能机器人操作系统)项目研究的一部分。

为什么选择Keras? (Why Keras?)

There are countless deep-learning frameworks available today, but there are some of the areas in which Keras proved better than other alternatives.

当今有无数的深度学习框架可用,但是Keras在某些领域被证明比其他选择要好。

Keras focuses on minimal user action requirement when common use cases are concerned also if the user makes an error, clear and actionable feedback is provided. This makes keras easy to learn and use.

当涉及到常见用例时,如果用户犯了错误,提供了清晰且可行的反馈,Keras专注于最小化用户操作要求。 这使喀拉拉邦易于学习和使用

When you want to put your Keras models to use into some application, you need to deploy it on other platforms which is comparatively easy if you are using keras. It also supports multiple backends and also allows portability across backends i.e. you can train using one backend and load it with another.

当您想将Keras模型用于某些应用程序时,您需要将其部署在其他平台上,如果您使用的是keras,这相对容易。 它还支持多个后端,并且还允许跨后端进行可移植性,即,您可以使用一个后端进行培训,然后将其加载到另一个后端。

It has got a strong back with built-in multiple GPU support, it also supports distributed training.

凭借内置的多个GPU支持获得了强大的支持,它还支持分布式培训。

Keras教程 (Keras Tutorial)

安装Keras (Installing Keras)

We need to install one of the backend engines before we actually get to installing Keras. Let’s go and install any of TensorFlow or Theano or CNTK modules.

在实际安装Keras之前,我们需要安装一个后端引擎。 我们去安装任何TensorFlowTheanoCNTK模块。

Now, we are ready to install keras. We can either use pip installation or clone the repository from git. To install using pip, open the terminal and run the following command:

现在,我们准备安装keras。 我们可以使用pip安装或从git克隆存储库。 要使用pip进行安装,请打开终端并运行以下命令:

pip install keras

In case pip installation doesn’t work or you want another method, you can clone the git repository using

如果无法安装pip或需要其他方法,则可以使用以下命令克隆git存储库

git clone https://github.com/keras-team/keras.git

Once cloned, move to the cloned directory and run:

克隆后,移至克隆目录并运行:

sudo python setup.py install

使用Keras (Using Keras)

To use Keras in any of your python scripts we simply need to import it using:

要在您的任何python脚本中使用Keras,我们只需要使用以下命令导入即可:

import keras

密集连接的网络 (Densely Connected Network)

A Sequential model is probably a better choice to create such network, but we are just getting started so it’s a better choice to start with something really simple:

顺序模型可能是创建此类网络的更好选择,但是我们才刚刚开始,因此从一个非常简单的东西开始是一个更好的选择:

from keras.layers import Input, Dense
from keras.models import Model
# This returns a tensor
inputs = Input(shape=(784,))
 
# a layer instance is callable on a tensor, and returns a tensor
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)
# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

Now that you have seen how to create a simple Densely Connected Network model you can train it with your training data and may use it in your deep learning module.

既然您已经了解了如何创建简单的密集连接网络模型,则可以将其与训练数据一起训练,并可以在深度学习模块中使用它。

顺序模型 (Sequential Model)

Model is core data structure of Keras. The simplest type of model is a linear stack of layers, we call it Sequential Model. Let’s put our hands in code and try to build one:

模型是Keras的核心数据结构。 模型的最简单类型是层的线性堆栈,我们称之为顺序模型。 让我们动手编写代码,尝试构建一个:

# import required modules
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
# Create a model
model= Sequential()
# Stack Layers
model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))
# Configure learning
model.compile(loss='categorical_crossentropy', optimizer='sgd',metrics=['accuracy'])
# Create Numpy arrays with random values, use your training or test data here
x_train = np.random.random((64,100))
y_train = np.random.random((64,10))
x_test = np.random.random((64,100))
y_test = np.random.random((64,10))
# Train using numpy arrays
model.fit(x_train, y_train, epochs=5, batch_size=32)
# evaluate on existing data
loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)
# Generate predictions on new data
classes = model.predict(x_test, batch_size=128)

Let’s run the program to see the results:

keras tutorial, keras deep learning tutorial

让我们运行程序以查看结果:

Let’s try a few more models and how to create them like, Residual Connection on a Convolution Layer:

让我们尝试其他一些模型,以及如何在卷积层上创建“残留连接”,例如:

from keras.layers import Conv2D, Input

# input tensor for a 3-channel 256x256 image
x = Input(shape=(256, 256, 3))
# 3x3 conv with 3 output channels (same as input channels)
y = Conv2D(3, (3, 3), padding='same')(x)
# this returns x + y.
z = keras.layers.add([x, y])

共享视觉模型 (Shared Vision Model)

Shared Vision Model helps to classify whether two MNIST digits are the same digit or different digits by reusing the same image-processing module on two inputs. Let’s create one as shown below.

共享视觉模型通过在两个输入上重用相同的图像处理模块,有助于对两个MNIST数字是相同数字还是不同数字进行分类。 让我们创建一个如下所示。

from keras.layers import Conv2D, MaxPooling2D, Input, Dense, Flatten
from keras.models import Model
import keras
# First, define the vision modules
digit_input = Input(shape=(27, 27, 1))
x = Conv2D(64, (3, 3))(digit_input)
x = Conv2D(64, (3, 3))(x)
x = MaxPooling2D((2, 2))(x)
out = Flatten()(x)
vision_model = Model(digit_input, out)
# Then define the tell-digits-apart model
digit_a = Input(shape=(27, 27, 1))
digit_b = Input(shape=(27, 27, 1))
# The vision model will be shared, weights and all
out_a = vision_model(digit_a)
out_b = vision_model(digit_b)
concatenated = keras.layers.concatenate([out_a, out_b])
out = Dense(1, activation='sigmoid')(concatenated)
classification_model = Model([digit_a, digit_b], out)

视觉问答模型 (Visual Question Answering Model)

Let’s create a model which can choose the correct one-word answer to a natural-language question about a picture.

让我们创建一个模型,该模型可以为有关图片的自然语言问题选择正确的单字答案。

It can be done by encoding the question and image into two separate vectors, concatenating both of them and training on top a logistic regression over some vocabulary of potential answers. Let’s try the model:

可以通过将问题和图像编码为两个单独的向量,并将它们连接在一起,并在一些潜在答案的词汇表上进行逻辑回归来训练来完成。 让我们尝试一下模型:

from keras.layers import Conv2D, MaxPooling2D, Flatten
from keras.layers import Input, LSTM, Embedding, Dense
from keras.models import Model, Sequential
import keras
 
# First, let's define a vision model using a Sequential model.
# This model will encode an image into a vector.
vision_model = Sequential()
vision_model.add(Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(224, 224, 3)))
vision_model.add(Conv2D(64, (3, 3), activation='relu'))
vision_model.add(MaxPooling2D((2, 2)))
vision_model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
vision_model.add(Conv2D(128, (3, 3), activation='relu'))
vision_model.add(MaxPooling2D((2, 2)))
vision_model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
vision_model.add(Conv2D(256, (3, 3), activation='relu'))
vision_model.add(Conv2D(256, (3, 3), activation='relu'))
vision_model.add(MaxPooling2D((2, 2)))
vision_model.add(Flatten())
 
# Now let's get a tensor with the output of our vision model:
image_input = Input(shape=(224, 224, 3))
encoded_image = vision_model(image_input)
 
# Next, let's define a language model to encode the question into a vector.
# Each question will be at most 100 word long,
# and we will index words as integers from 1 to 9999.
question_input = Input(shape=(100,), dtype='int32')
embedded_question = Embedding(input_dim=10000, output_dim=256, input_length=100)(question_input)
encoded_question = LSTM(256)(embedded_question)
 
# Let's concatenate the question vector and the image vector:
merged = keras.layers.concatenate([encoded_question, encoded_image])
 
# And let's train a logistic regression over 1000 words on top:
output = Dense(1000, activation='softmax')(merged)
 
# This is our final model:
vqa_model = Model(inputs=[image_input, question_input], outputs=output)
 
# The next stage would be training this model on actual data.

If you want to learn more about Visual Question Answering (VQA), check out this beginner’s guide to VQA.

如果您想了解有关视觉问答(VQA)的更多信息,请查阅此VQA初学者指南

训练神经网络 (Training Neural Network)

Now that we have seen how to build different models using Keras, let’s put things together and work on a complete example. The following example trains a Neural Network on MNIST data set:

既然我们已经了解了如何使用Keras构建不同的模型,那么让我们将它们放在一起并研究一个完整的示例。 以下示例在MNIST数据集上训练神经网络:

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
 
batch_size = 128
num_classes = 10
epochs = 20
 
# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
 
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
 
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
 
model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))
 
model.summary()
# Compile model
model.compile(loss='categorical_crossentropy',
              optimizer=RMSprop(),
              metrics=['accuracy'])
 
history = model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
# Print the results
print('Test loss:', score[0])
print('Test accuracy:', score[1])

Let’s run this example and wait for results:

keras example, keras neural network tutorial

The output shows only the final part, it might take a few minutes for the program to finish execution depending on machine

让我们运行此示例并等待结果:

输出仅显示最后一部分,根据机器的不同,程序可能需要几分钟才能完成执行

结论 (Conclusion)

In this tutorial, we discovered that Keras is a powerful framework and makes it easy for the user to create prototypes and that too very quickly. We have also seen how different models can be created using keras. These models can be used for feature extraction, fine-tuning and prediction. We have also seen how to train a neural network using keras.

在本教程中,我们发现Keras是一个功能强大的框架,它使用户易于创建原型,而且开发起来太快了。 我们还看到了如何使用keras创建不同的模型。 这些模型可用于特征提取,微调和预测。 我们还看到了如何使用keras训练神经网络。

Keras has grown popular with other frameworks and it is one of the most popular frameworks on Kaggle.

Keras在其他框架中变得越来越流行,它是Kaggle上最受欢迎的框架之一。

翻译自: https://www.journaldev.com/18314/keras-deep-learning-tutorial

深度学习 keras

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值