Getting started with Keras Sequential model Japanese translation

Motivation

I had the opportunity to write Keras, so I translated it into Japanese. Getting started with the Keras Sequential model The Sequential model is made by stacking layers. You can create a Sequential by giving an instance of the layer to the constructor of the Sequential. The layer given the Dense, ʻActivation` below (By the way, Dense is a so-called Full connected Neural Network).

from Keras.models import Sequential
model = Sequential([
	Dense(32, input_dim=784),
	Activation('relu'),
	Dense(10),
	Activation('softmax'),
])

You can also add layers using the .add () method.

model = Sequential()
model.add(Dense(32, input_dim=784))
model.add(Activation('relu'))

Specifying the input shape The model must know what comes in the number of dimensions of the input. Therefore, it is necessary to give the information of the input dimension to the first layer of the Sequential model (the other layers can automatically infer the number of dimensions). There are three ways to do this.

  1. Pass it to the input_shape argument. Pass it as a tuple. If you pass it as None instead of an integer, it will be interpreted as any positive number. Batch size is not included.
  2. Pass it as a batch_input_shape argument. This includes the batch size. It is a useful technique when you want to fix the batch size.
  3. Two-dimensional layers such as Dense can specify the number of dimensions with the input_dim argument. For three-dimensional layers, specify the input_length argument in addition to the input_dim argument.

Here's what I put these into my code:

	model = Sequential()
	model.add(Dense(32, input_shape=(784,)))
	model = Sequential()
	model.add(Dense(32, batch_input_shape=(None, 784)))
	# note that batch dimension is "None" here,(Although the batch size is None)
	# so the model will be able to process batches of any size(Now you can learn any batch size)
	model = Sequential()
	model.add(Dense(32, input_dim=784))

And the following three codes have the same meaning.

	model = Sequential()
	model.add(LSTM(32, input_shape=(10, 64)))
	model = Sequential()
	model.add(LSTM(32, batch_input_shape=(None, 10, 64)))
	model = Sequential()
	model.add(LSTM(32, input_length=10, input_dim=64))

The Merge layer It is possible to integrate several instances of Sequential so that one output is output. The output Sequential can be layered on a new Sequential. The following is an example and a conceptual diagram of layers.

from keras.layers import Merge

left_branch = Sequential()
left_branch.add(Dense(32, input_dim=784))

right_branch = Sequential()
right_branch.add(Dense(32, input_dim=784))

merged = Merge([left_branch, right_branch], mode='concat')

final_model = Sequential()
final_model.add(merged)
final_model.add(Dense(10, activation='softmax'))

two_branches_sequential_model.png

Merge has several modes, and you can describe how to merge two tensors.

Arbitrary calculation formula can be described in mode argument using lambda expression etc.

merged = Merge([left_branch, right_branch], mode=lambda x, y: x - y)

If you want to build a complex model that cannot be represented by Sequential and Merge, you need to refer to the function API.

Compilation It is necessary to set the learning process before training the model. It is done by the compile method. The compile method takes the following three arguments.

# for a multi-class classification problem
model.compile(optimizer='rmsprop',
			  loss='categorical_crossentropy',
			  metrics=['accuracy'])

# for a binary classification problem
model.compile(optimizer='rmsprop',
			  loss='binary_crossentropy',
			  metrics=['accuracy'])

# for a mean squared error regression problem
model.compile(optimizer='rmsprop',
			  loss='mse')

Training In Keras, training is done with Numpy array inputs and labels. The fit function is often used in the learning phase.

# for a single-input model with 2 classes(binary):(Two-class classification)

model = Sequential()
model.add(Dense(1, input_dim=784, activation='softmax'))
model.compile(optimizer='rmsprop',
			  loss='binary_crossentropy',
			  metrics=['accuracy'])

# generate dummy data(Create appropriate input data)
import numpy as np
data = np.random.random((1000, 784))
labels = np.random.randint(2, size=(1000, 1))

# train the model, interating on the data in batches(Learn with batch size 32)
# of 32 samples
model.fit(data, labels, nb_epoch=10, batch_size=32)
# for a multi-input model with 10 classes:(10 classification)

left_branch = Sequential()
left_branch.add(Dense(32, input_dim=784))

right_branch = Sequential()
right_branch.add(Dense(32, input_dim=784))

merged = Merge([left_branch, right_branch], mode='concat')

model = Sequential()
model.add(merged)
model.add(Dense(10, activation='softmax'))

model.compile(optimizer='rmsprop',
			  loss='categorical_crossentropy',
			  metrics=['accuracy'])

# generate dummy data(Creating appropriate input data)
import numpy as np
from keras.utils.np_utils import to_categorical
data_1 = np.random.random((1000, 784))
data_2 = np.random.random((1000, 784))

# these are integers between 0 and 9(Generate integer values from 0 to 9)
labels = np.random.randint(10, size=(1000, 1))
# we convert the labels to a binary matrix of size (1000, 10)(For use in multi-class classification tasks 1-Convert to hot vector)
# for use with categorical_crossentropy
labels = to_categorical(labels, 10)

# train the model
#note that we are passing a list of Numpy arrays as training data
# since the model has 2 inputs(Take two inputs as arguments)
model.fit([data_1, data_2], labels, nb_epoch=10, batch_size=32)

Examples There are some implementation examples. Check out the original Keras.

Recommended Posts

Getting started with Keras Sequential model Japanese translation
Getting started: 30 seconds to Keras Japanese translation
Getting started with Android!
1.1 Getting Started with Python
Getting Started with Golang 2
[Translation] Getting Started with Rust for Python Programmers
Getting started with apache2
Getting Started with Golang 1
Getting Started with Python
Getting Started with Django 1
Getting Started with Optimization
Getting Started with Golang 3
Getting Started with Numpy
Getting started with Spark
Getting Started with Python
Getting Started with Pydantic
Getting Started with Golang 4
Getting Started with Jython
Getting Started with Django 2
Translate Getting Started With TensorFlow
Getting Started with Python Functions
Getting Started with Go Assembly
Getting Started with PKI with Golang ―― 4
Getting Started with Python Django (1)
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Getting Started with Python Django (6)
Getting Started with Django with PyCharm
Python3 | Getting Started with numpy
Getting Started with Python Django (5)
Getting Started with Python responder v2
Getting Started with Git (1) History Storage
Getting started with Sphinx. Generate docstring with Sphinx
Getting Started with Python Web Applications
Getting Started with Python for PHPer-Classes
Getting Started with Sparse Matrix with scipy.sparse
Getting Started with Julia for Pythonista
Getting Started with Python Basics of Python
Getting Started with Cisco Spark REST-API
Getting started with USD on Windows
Getting Started with Python Genetic Algorithms
Getting started with Python 3.8 on Windows
Getting Started with Python for PHPer-Functions
Getting Started with CPU Steal Time
Getting Started with python3 # 1 Learn Basic Knowledge
Getting Started with Flask with Azure Web Apps
Getting Started with Python Web Scraping Practice
Getting Started with Python for PHPer-Super Basics
Getting Started with Python Web Scraping Practice
Getting started with Dynamo from Python boto
Getting Started with Lisp for Pythonista: Supplement
Getting Started with Heroku, Deploying Flask App
Getting Started with TDD with Cyber-dojo at MobPro
MongoDB Basics: Getting Started with CRUD in JAVA
Getting Started with Drawing with matplotlib: Writing Simple Functions
Exposing the DCGAN model for Cifar 10 with keras
Django Getting Started Part 2 with eclipse Plugin (PyDev)
Getting started with AWS IoT easily in Python
Getting Started with Python's ast Module (Using NodeVisitor)
Materials to read when getting started with Python
Settings for getting started with MongoDB in python