"Python Keras Combat" Quick start: 30 seconds Keras__python

Source: Internet
Author: User
Tags virtual environment git clone theano keras

First, Keras introduction

Keras is a high-level neural network API written in Python that can be run TensorFlow, CNTK, or Theano as a backend. Keras's development focus is on support for fast experimentation. The key to doing research is to be able to convert your ideas into experimental results with minimal delay.

If you have the following requirements, please select Keras:

-Allows simple and fast prototyping (user-friendly, highly modular, scalable).
-both convolution neural networks and cyclic neural networks are supported, together with a combination of both.
-Seamless running and switching on the CPU and GPU.

Keras compatible python version: Python 2.7-3.6.

second, the rapid start Keras

Keras's core data structure is model, a way of organizing the network layer. The simplest model is the sequential model, which is a stack stacked linearly by the multiple network layers. For more complex structures, you should use the Keras function API, which allows you to construct arbitrary neural network diagrams.

The sequential model looks like this:

From keras.models import sequential

model = sequential ()

You can simply use. Add () to stack the model:

From keras.layers import dense

model.add (dense (units=64, activation= ' Relu ', input_dim=100))
Model.add (Dense (units=10, activation= ' Softmax '))

Once you have finished building the model, you can configure the learning process with. Compile ():

Model.compile (loss= ' categorical_crossentropy ',
              optimizer= ' sgd ',
              metrics=[' accuracy '])

If necessary, you can also configure the optimizer further. A central tenet of keras is to make things fairly simple, while allowing users to have full control when they need it (the ultimate control is the scalability of the source code).

Model.compile (Loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.sgd (lr=0.01, momentum= 0.9, Nesterov=true))

Now, you can iterate over the training data in batches:

# X_train and Y_train are numpy arrays--just like in the Scikit-learn API.
Model.fit (X_train, Y_train, epochs=5, batch_size=32)

Alternatively, you can manually supply the batch data to the model:

Model.train_on_batch (X_batch, Y_batch)

Model performance can be evaluated with just one line of code:

Loss_and_metrics = Model.evaluate (X_test, Y_test, batch_size=128)

or generate predictions for new data:

Classes = Model.predict (X_test, batch_size=128)

Building a question and answer system, an image classification model, a neural Turing, or any other model, is so fast. The idea behind deep learning is simple, so why should it be so painful to realize it?

third, Keras learning and combat
For more in-depth tutorials on Keras, see:

Keras Chinese Official document: https://keras.io/zh/
Books written by Keras authors: Https://github.com/fchollet/deep-learning-with-python-notebooks

iv. installation of Keras

Before you install Keras, install one of the following back-end engines: TensorFlow, Theano, or CNTK. We recommend TensorFlow back end. TensorFlow installation guidelines. Theano installation guidelines. CNTK installation guidelines.

Then you can install the Keras itself. There are two ways to install Keras:

Use PyPI to install Keras (recommended):

sudo pip install Keras

If you use the VIRTUALENV virtual environment, you can avoid sudo using:

Pip Install Keras

Or: Install Keras using Github source code:
First, use Git to clone Keras:

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

Then, the CD to the Keras directory and run the Install command:

CD keras
sudo python setup.py install
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.