[PYTHON] Keras starting from nothing 1st

Click here for Keras lottery starting from nothing [http://qiita.com/Ishotihadus/items/6ecf5684c2cbaaa6a5ef)

What is nothing

Keras starts without knowing Keras, even neural networks, or even Python. It is assumed that only Python3 is installed (this depends on the environment, so please google).

First of all, install it.

$ sudo pip3 install tensorflow keras

Then start Python.

$ python3

I feel that the interactive type is easier to do.

Make an array

Arrays are handled by NumPy. Because it's not late.

import numpy as np
array = np.array([0.1,0.2,0.3])

An array was created. The first line loads the library, so it's always needed (that's right).

You can also make two dimensions. The row direction (horizontal direction) in the matrix is the inner array. If below

\begin{pmatrix}0.1 & 0.2 & 0.3 \\ 0.4 & 0.6 & 0.8\end{pmatrix}

Represents.

array = np.array([[0.1,0.2,0.3], [0.4,0.6,0.8]])

The zero matrix, the matrix with all elements 1 and the identity matrix are as follows. You can indicate the number of elements in the array with (a, b, c, ...). Tuple for Python.

array = np.zeros((2,3))
array = np.ones((3,1))
array = np.identity(5) #size is(5,5)

Let's make a random matrix.

array = np.random.rand(3,2) #Uniform distribution from 0 to less than 1
array = np.random.randn(5,4) #Normal distribution with mean 0 and variance 1
array = np.random.randint(0, 5, size=(2,3)) #Discrete uniform distribution from 0 to less than 5

Practice so far.

Create a dataset

I want to learn, so I'll just create a dataset.

data = np.random.rand(250,5)
labels = (np.sum(data, axis=1) > 2.5) * 1

The data on the first line is just 250 5-dimensional data. The label on the second line is an array that takes a five-dimensional sum for each of the 250 data, and is 1 if it is greater than 2.5 (or 0 otherwise).

Using this, if you input an appropriate 5-dimensional array, let's make a neural network that tells us 1 when the sum is greater than 2.5.

Format the dataset

There are two classes of output, so I really want it to be one-hot. In other words, for one (5D) input, the output is 2D, and if the sum of the inputs is 2.5 or less, it will be [1,0], and if it is greater than 2.5, it will be [0,1]. That's why.

To do that, use the Keras feature. Logs are spit out when importing, but you shouldn't have to worry about it.

from keras.utils import np_utils
labels = np_utils.to_categorical(labels)

This makes labels 250 two-dimensional arrays.

Make a neural network

Sequential is a simple model that just stacks layers. It feels like inputting the output of the previous layer as it is. All the edges are drawn between the nodes (of course it can be pulled out, but omitted). This time, let's consider it as one intermediate layer (input layer, hidden layer, output layer).

from keras.models import Sequential
from keras.layers import Dense, Activation
model = Sequential()

Next, insert the input layer-hidden layer. I use a layer called Dense for the time being. Twenty arrows appear from each of the five nodes, indicating that there are 20 nodes in the next layer. The number of nodes must match the number of dimensions of the input data (of course).

model.add(Dense(20, input_dim=5))

Next, let's put in the activation function. Five numerical values are input for each of the 20 nodes, and the weighted sum (+ bias) of the five numerical values is appropriately taken and the function is applied at the end. The function at that time is the activation function. This time is a ramp function.

model.add(Activation('relu'))

I think these two are the same even if they are summarized by Dense (20, input_dim = 5, activation ='relu').

Next is the hidden layer-the output layer. Values come from each of the 20 nodes (after the activation function has been applied). The output is two-dimensional and applies a weighted sum and softmax function to the 20 values that come to the node. It seems that the softmax function is good for categorization.

model.add(Dense(2, activation='softmax'))

It looks like the figure below (I made 10 because it is troublesome to write 20 nodes). It's different from the general neural network diagram, but I arranged it for explanation.

Actually, both Dense and Activation represent layers, but I felt that it would be easier to understand if I imagined something to connect.

名称未設定.001.jpeg

compile

Compile before learning.

The first is the optimization function, the second is the loss function, and the third is a list of metrics. For the time being, use something that is common.

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

Learning

nb_epoch is the number of times. validation_split is the percentage of data used for validation (the entire data is not used for learning, only 80% is used for learning, and the remaining 20% is used for evaluation).

model.fit(data, labels, nb_epoch=300, validation_split=0.2)

Did you learn?

Forecasting unknown data

Try to see if it actually works.

test = np.random.rand(200, 5)
predict = np.argmax(model.predict(test), axis=1)
real = (np.sum(test, axis=1) > 2.5) * 1
sum(predict == real) / 200.0

It can be identified with an accuracy of about 98%. Well, it's such a simple condition. By the way, if you make a judgment without turning learning, it will be about 50%. Well, the probability of hitting it properly is 50%.

Summary of this program

Layers are stacked at the same time when the model is defined.

import numpy as np
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation

data = np.random.rand(250,5)
labels = np_utils.to_categorical((np.sum(data, axis=1) > 2.5) * 1)
model = Sequential([Dense(20, input_dim=5), Activation('relu'), Dense(2, activation='softmax')])
model.compile('rmsprop', 'categorical_crossentropy', metrics=['accuracy'])
model.fit(data, labels, nb_epoch=300, validation_split=0.2)

test = np.random.rand(200, 5)
predict = np.argmax(model.predict(test), axis=1)
real = (np.sum(test, axis=1) > 2.5) * 1
print(sum(predict == real) / 200.0)

Recommended Posts

Keras starting from nothing 1st
Keras starting from nothing
Keras 5th starting from nothing
Keras 4th starting from nothing
Keras starting from nothing 2nd
Keras starting from nothing 3rd
Django starting from scratch (part: 2)
ChIP-seq analysis starting from zero
(Almost) troubleshooting techniques from nothing
[Introduction] From installing kibana to starting
Code wars kata starting from zero