Original link:tecdat.cn/?p=18149
Original source:Tuo End number according to the tribe public number
Driverless cars date back to 1989. Neural networks have been around for a long time, so what has sparked the boom in AI and deep learning in recent years? [1 SEC] Part of the answer lies in Moore’s Law and dramatic improvements in hardware and computing power. We can do more with less now. As the name suggests, the concept of neural networks is inspired by the networks of neurons in our own brains. Neurons are very long cells, and each cell has projections called dendrites that separately receive and transmit electrochemical signals from surrounding neurons. As a result, our brain cells form flexible and powerful communication networks, an assembly-line-like distribution process that supports complex cognitive abilities such as playing music and drawing.
Video: CNN (Convolutional Neural Network) model and R language implementation
Neural network structure
Neural networks usually consist of one input layer, one or more hidden layers, and one output layer. The input layer consists of P predictive variables or input units/nodes. Needless to say, it is usually best to standardize variables. These input units can be connected to one or more hide units in the first hide layer. The hidden layer that is fully connected to the previous layer is called the dense layer. In the figure, both hidden layers are dense.
Computational prediction of the output layer
The output layer calculates predictions, where the number of units is determined by the specific problem. In general, a dichotomous problem requires one output unit, while a multi-class problem with k categories will require k corresponding output units. The former can simply be calculated directly using an S-shaped function, while the latter usually requires a Softmax transform to add up all values in all K output units to 1, so it can be treated as a probability. No classification prediction is required.
The weight
Each arrow shown in the figure passes the input associated with the weight. Each weight is essentially one of many coefficient estimates that help calculate the regression in the node to which the corresponding arrow points. These are unknown parameters that must be adjusted by the model using an optimization process to minimize the loss function. Before training, ownership weights are initialized with random values.
Optimization and loss functions
Before training, we need to do two things: measure goodness of fit, which compares predictions and known labels of all training observations; The second is the optimization method to calculate gradient descent, which is essentially to adjust the ownership reestimate value at the same time to improve the direction of goodness of fit. For each method, we have a loss function and an optimizer. There are many types of loss functions, all aimed at quantifying prediction errors, such as using cross entropy. Popular stochastic optimization methods such as Adam.
Convolutional neural network
Convolutional neural network is a special type of neural network, which can be well used for image processing and is based on the above principles. The “convolution” in the name is attributed to the square squares of pixels in the image processed through the filter. As a result, the model can capture key visual cues mathematically. For example, a bird’s beak can be highly distinctive among animals. In the example described below, the convolutional neural network may process beak-like structures along a series of transformation chains involving convolution, pooling, and flattening, and finally, see the associated neurons firing, ideally predicting that the bird’s probability is the greatest of the competing classes.
Images can be represented as numerical matrices based on color intensity. Monochromatic images are processed using 2D convolution layers, while color images require 3D convolution layers, we use the former.
The kernel (also known as a filter) convolves square blocks of pixels into scalars in subsequent convolutional layers, scanning the image from top to bottom.
Throughout the process, the core performs element-by-element multiplication and sums all the products into a value that is passed to the subsequent convolution layer.
The kernel moves one pixel at a time. This is the step size of the sliding window that the kernel uses for convolution, adjusted step by step. Larger step sizes imply finer, smaller convolution features.
Pooling is the sampling from the convolution layer, which can present the main features in a lower dimension, so as to prevent excessive fitting and merging to reduce computing requirements. The two main types of pooling are average pooling and maximum pooling. Given a kernel and a step size, the merge is equivalent to convolution, but take the average or maximum of each frame.
Flattening as the name implies, flattening simply transforms the last convolutional layer into a one-dimensional neural network layer. It lays the foundation for practical forecasting.
R language implementation
CNN (convolutional neural network) models are very useful when we use them to train multidimensional types of data, such as images. We can also implement CNN model for regression data analysis. We previously used Python for CNN model regression, and in this video we implement the same approach in R. We use a one-dimensional convolution function to apply the CNN model. We need the Keras R interface to use the Keras neural network API in R. If it is not available in the development environment, you need to install it first. This tutorial covers:
- To prepare data
- Define and fit the model
- Predict and visualize results
- The source code
Let’s start with the libraries we need to load this tutorial.
library(keras)
library(caret)
Copy the code
To prepare
Data In this tutorial, we use the Boston housing data set as target regression data. First, we’ll load the data set and divide it into training and test sets.
set.seed(123)
boston = MASS::Boston
indexes = createDataPartition(boston$medv, p = .85, list = F)
Copy the code
train = boston[indexes,]
test = boston[-indexes,]
Copy the code
Next, we separate the X input and Y output portions of the training and test data and convert them to matrix type. As you may know, “MEDV” is the y data output of the Boston housing data set, which is the last column. The remaining columns are x input data. Check dimensions.
dim(xtrain)
[1] 432 13
Copy the code
dim(ytrain)
[1] 432 1
Copy the code
Next, we will redefine the shape of the X input data by adding another dimension.
dim(xtrain)
[1] 432 13 1
Copy the code
dim(xtest)
[1] 74 13 1
Copy the code
Here, we can extract the input dimension of the Keras model.
print(in_dim)
[1] 13 1
Copy the code
Define and fit the model
We define the Keras model and add a one-dimensional convolution layer. The input shape changes to (13,1) as defined above. We added Flatten and Dense layers and compiled them using the “Adam” optimizer.
model %>% summary() ________________________________________________________________________ Layer (type) Output Shape Param # ======================================================================== conv1d_2 (Conv1D) (None, 12, 64) 192 ________________________________________________________________________ flatten_2 (Flatten) (None, 768) 0 ________________________________________________________________________ dense_3 (Dense) (None, 32) 24608 ________________________________________________________________________ dense_4 (Dense) (None, 1) 33 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Total params: 24833 Trainable params: 24833 Non - trainable params: 0 ________________________________________________________________________Copy the code
Next, we will use training data to fit the model.
Print (scores) loss of 24.20518Copy the code
Predict and visualize results
Now we can use the model of training to predict the test data.
predict(xtest)
Copy the code
We will check the accuracy of the forecast with RMSE metrics.
Cat ("RMSE:", RMSE(ytest, ypred)) RMSE: 4.935908Copy the code
Finally, we’ll visualize the results in the chart to check for errors.
x_axes = seq(1:length(ypred))
lines(x_axes, ypred, col = "red", type = "l", lwd = 2)
legend("topl
Copy the code
In this tutorial, we briefly learned how to use the Keras CNN model in R to fit and predict regression data.
Most welcome insight
1. Improved Nelson-Siegel model fitting yield curve analysis with r language using neural network
2. R language to achieve fitting neural network prediction and result visualization
3. Python uses genetic algorithm-neural network-fuzzy logic control algorithm for lottery analysis
4. Python for NLP: Classification using Keras’s multi-label text LSTM neural network
5. Use R language to realize the neural network to predict the stock example
6.R language deep learning image classification based on Keras small data set
7. An example of SEQ2SEQ model for NLP uses Keras for neural machine translation
8. Deep learning model analysis of sugar based on grid search algorithm optimization in Python
9. Matlab uses Bayesian optimization for deep learning