8. Convolutional Neural Networks¶
Convolutional neural networks (CNNs) were developed during the last decade of the previous century, with a focus on character recognition tasks. Nowadays, CNNs are a central element in the spectacular success of deep learning methods. The success in for example image classifications have made them a central tool for most machine learning practitioners.
CNNs are very similar to ordinary Neural Networks. They are made up of neurons that have learnable weights and biases. Each neuron receives some inputs, performs a dot product and optionally follows it with a non-linearity. The whole network still expresses a single differentiable score function: from the raw image pixels on one end to class scores at the other. And they still have a loss function (for example Softmax) on the last (fully-connected) layer and all the tips/tricks we developed for learning regular Neural Networks still apply (back propagation, gradient descent etc etc).
What is the difference? CNN architectures make the explicit assumption that the inputs are images, which allows us to encode certain properties into the architecture. These then make the forward function more efficient to implement and vastly reduce the amount of parameters in the network.
Here we provide only a superficial overview, for the more interested, we recommend highly the course IN5400 – Machine Learning for Image Analysis and the slides of CS231.
Another good read is the article here https://arxiv.org/pdf/1603.07285.pdf.
8.1. Neural Networks vs CNNs¶
Neural networks are defined as affine transformations, that is a vector is received as input and is multiplied with a matrix of so-called weights (our unknown paramters) to produce an output (to which a bias vector is usually added before passing the result through a nonlinear activation function). This is applicable to any type of input, be it an image, a sound clip or an unordered collection of features: whatever their dimensionality, their representation can always be flattened into a vector before the transformation.
8.2. Why CNNS for images, sound files, medical images from CT scans etc?¶
However, when we consider images, sound clips and many other similar kinds of data, these data have an intrinsic structure. More formally, they share these important properties:
They are stored as multi-dimensional arrays (think of the pixels of a figure) .
They feature one or more axes for which ordering matters (e.g., width and height axes for an image, time axis for a sound clip).
One axis, called the channel axis, is used to access different views of the data (e.g., the red, green and blue channels of a color image, or the left and right channels of a stereo audio track).
These properties are not exploited when an affine transformation is applied; in fact, all the axes are treated in the same way and the topological information is not taken into account. Still, taking advantage of the implicit structure of the data may prove very handy in solving some tasks, like computer vision and speech recognition, and in these cases it would be best to preserve it. This is where discrete convolutions come into play.
A discrete convolution is a linear transformation that preserves this notion of ordering. It is sparse (only a few input units contribute to a given output unit) and reuses parameters (the same weights are applied to multiple locations in the input).
8.3. Regular NNs don’t scale well to full images¶
As an example, consider an image of size \(32\times 32\times 3\) (32 wide, 32 high, 3 color channels), so a single fully-connected neuron in a first hidden layer of a regular Neural Network would have \(32\times 32\times 3 = 3072\) weights. This amount still seems manageable, but clearly this fully-connected structure does not scale to larger images. For example, an image of more respectable size, say \(200\times 200\times 3\), would lead to neurons that have \(200\times 200\times 3 = 120,000\) weights.
We could have several such neurons, and the parameters would add up quickly! Clearly, this full connectivity is wasteful and the huge number of parameters would quickly lead to possible overfitting.
A regular 3-layer Neural Network.
8.4. 3D volumes of neurons¶
Convolutional Neural Networks take advantage of the fact that the input consists of images and they constrain the architecture in a more sensible way.
In particular, unlike a regular Neural Network, the layers of a CNN have neurons arranged in 3 dimensions: width, height, depth. (Note that the word depth here refers to the third dimension of an activation volume, not to the depth of a full Neural Network, which can refer to the total number of layers in a network.)
To understand it better, the above example of an image with an input volume of activations has dimensions \(32\times 32\times 3\) (width, height, depth respectively).
The neurons in a layer will only be connected to a small region of the layer before it, instead of all of the neurons in a fully-connected manner. Moreover, the final output layer could for this specific image have dimensions \(1\times 1 \times 10\), because by the end of the CNN architecture we will reduce the full image into a single vector of class scores, arranged along the depth dimension.
A CNN arranges its neurons in three dimensions (width, height, depth), as visualized in one of the layers. Every layer of a CNN transforms the 3D input volume to a 3D output volume of neuron activations. In this example, the red input layer holds the image, so its width and height would be the dimensions of the image, and the depth would be 3 (Red, Green, Blue channels).
8.5. Layers used to build CNNs¶
A simple CNN is a sequence of layers, and every layer of a CNN transforms one volume of activations to another through a differentiable function. We use three main types of layers to build CNN architectures: Convolutional Layer, Pooling Layer, and Fully-Connected Layer (exactly as seen in regular Neural Networks). We will stack these layers to form a full CNN architecture.
A simple CNN for image classification could have the architecture:
INPUT (\(32\times 32 \times 3\)) will hold the raw pixel values of the image, in this case an image of width 32, height 32, and with three color channels R,G,B.
CONV (convolutional )layer will compute the output of neurons that are connected to local regions in the input, each computing a dot product between their weights and a small region they are connected to in the input volume. This may result in volume such as \([32\times 32\times 12]\) if we decided to use 12 filters.
RELU layer will apply an elementwise activation function, such as the \(max(0,x)\) thresholding at zero. This leaves the size of the volume unchanged (\([32\times 32\times 12]\)).
POOL (pooling) layer will perform a downsampling operation along the spatial dimensions (width, height), resulting in volume such as \([16\times 16\times 12]\).
FC (i.e. fully-connected) layer will compute the class scores, resulting in volume of size \([1\times 1\times 10]\), where each of the 10 numbers correspond to a class score, such as among the 10 categories of the MNIST images we considered above . As with ordinary Neural Networks and as the name implies, each neuron in this layer will be connected to all the numbers in the previous volume.
8.6. Transforming images¶
CNNs transform the original image layer by layer from the original pixel values to the final class scores.
Observe that some layers contain parameters and other don’t. In particular, the CNN layers perform transformations that are a function of not only the activations in the input volume, but also of the parameters (the weights and biases of the neurons). On the other hand, the RELU/POOL layers will implement a fixed function. The parameters in the CONV/FC layers will be trained with gradient descent so that the class scores that the CNN computes are consistent with the labels in the training set for each image.
8.7. CNNs in brief¶
In summary:
A CNN architecture is in the simplest case a list of Layers that transform the image volume into an output volume (e.g. holding the class scores)
There are a few distinct types of Layers (e.g. CONV/FC/RELU/POOL are by far the most popular)
Each Layer accepts an input 3D volume and transforms it to an output 3D volume through a differentiable function
Each Layer may or may not have parameters (e.g. CONV/FC do, RELU/POOL don’t)
Each Layer may or may not have additional hyperparameters (e.g. CONV/FC/POOL do, RELU doesn’t)
For more material on convolutional networks, we strongly recommend the course IN5400 – Machine Learning for Image Analysis and the slides of CS231 which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). Michael Nielsen’s book is a must read, in particular chapter 6 which deals with CNNs.
8.8. CNNs in more detail, building convolutional neural networks in Tensorflow and Keras¶
As discussed above, CNNs are neural networks built from the assumption that the inputs to the network are 2D images. This is important because the number of features or pixels in images grows very fast with the image size, and an enormous number of weights and biases are needed in order to build an accurate network.
As before, we still have our input, a hidden layer and an output. What’s novel about convolutional networks are the convolutional and pooling layers stacked in pairs between the input and the hidden layer. In addition, the data is no longer represented as a 2D feature matrix, instead each input is a number of 2D matrices, typically 1 for each color dimension (Red, Green, Blue).
8.9. Setting it up¶
It means that to represent the entire dataset of images, we require a 4D matrix or tensor. This tensor has the dimensions:
8.10. The MNIST dataset again¶
The MNIST dataset consists of grayscale images with a pixel size of \(28\times 28\), meaning we require \(28 \times 28 = 724\) weights to each neuron in the first hidden layer.
If we were to analyze images of size \(128\times 128\) we would require \(128 \times 128 = 16384\) weights to each neuron. Even worse if we were dealing with color images, as most images are, we have an image matrix of size \(128\times 128\) for each color dimension (Red, Green, Blue), meaning 3 times the number of weights \(= 49152\) are required for every single neuron in the first hidden layer.
8.11. Strong correlations¶
Images typically have strong local correlations, meaning that a small part of the image varies little from its neighboring regions. If for example we have an image of a blue car, we can roughly assume that a small blue part of the image is surrounded by other blue regions.
Therefore, instead of connecting every single pixel to a neuron in the first hidden layer, as we have previously done with deep neural networks, we can instead connect each neuron to a small part of the image (in all 3 RGB depth dimensions). The size of each small area is fixed, and known as a receptive.
8.12. Layers of a CNN¶
The layers of a convolutional neural network arrange neurons in 3D: width, height and depth.
The input image is typically a square matrix of depth 3.
A convolution is performed on the image which outputs a 3D volume of neurons. The weights to the input are arranged in a number of 2D matrices, known as filters.
Each filter slides along the input image, taking the dot product between each small part of the image and the filter, in all depth dimensions. This is then passed through a non-linear function, typically the Rectified Linear (ReLu) function, which serves as the activation of the neurons in the first convolutional layer. This is further passed through a pooling layer, which reduces the size of the convolutional layer, e.g. by taking the maximum or average across some small regions, and this serves as input to the next convolutional layer.
8.13. Systematic reduction¶
By systematically reducing the size of the input volume, through convolution and pooling, the network should create representations of small parts of the input, and then from them assemble representations of larger areas. The final pooling layer is flattened to serve as input to a hidden layer, such that each neuron in the final pooling layer is connected to every single neuron in the hidden layer. This then serves as input to the output layer, e.g. a softmax output for classification.
8.14. Prerequisites: Collect and pre-process data¶
%matplotlib inline
# import necessary packages
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
# ensure the same random numbers appear every time
np.random.seed(0)
# display images in notebook
%matplotlib inline
plt.rcParams['figure.figsize'] = (12,12)
# download MNIST dataset
digits = datasets.load_digits()
# define inputs and labels
inputs = digits.images
labels = digits.target
# RGB images have a depth of 3
# our images are grayscale so they should have a depth of 1
inputs = inputs[:,:,:,np.newaxis]
print("inputs = (n_inputs, pixel_width, pixel_height, depth) = " + str(inputs.shape))
print("labels = (n_inputs) = " + str(labels.shape))
# choose some random images to display
n_inputs = len(inputs)
indices = np.arange(n_inputs)
random_indices = np.random.choice(indices, size=5)
for i, image in enumerate(digits.images[random_indices]):
plt.subplot(1, 5, i+1)
plt.axis('off')
plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
plt.title("Label: %d" % digits.target[random_indices[i]])
plt.show()
inputs = (n_inputs, pixel_width, pixel_height, depth) = (1797, 8, 8, 1)
labels = (n_inputs) = (1797,)
8.15. Importing Keras and Tensorflow¶
from tensorflow.keras import datasets, layers, models
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Sequential #This allows appending layers to existing models
from tensorflow.keras.layers import Dense #This allows defining the characteristics of a particular layer
from tensorflow.keras import optimizers #This allows using whichever optimiser we want (sgd,adam,RMSprop)
from tensorflow.keras import regularizers #This allows using whichever regularizer we want (l1,l2,l1_l2)
from tensorflow.keras.utils import to_categorical #This allows using categorical cross entropy as the cost function
#from tensorflow.keras import Conv2D
#from tensorflow.keras import MaxPooling2D
#from tensorflow.keras import Flatten
from sklearn.model_selection import train_test_split
# representation of labels
labels = to_categorical(labels)
# split into train and test data
# one-liner from scikit-learn library
train_size = 0.8
test_size = 1 - train_size
X_train, X_test, Y_train, Y_test = train_test_split(inputs, labels, train_size=train_size,
test_size=test_size)
8.16. Running with Keras¶
def create_convolutional_neural_network_keras(input_shape, receptive_field,
n_filters, n_neurons_connected, n_categories,
eta, lmbd):
model = Sequential()
model.add(layers.Conv2D(n_filters, (receptive_field, receptive_field), input_shape=input_shape, padding='same',
activation='relu', kernel_regularizer=regularizers.l2(lmbd)))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(n_neurons_connected, activation='relu', kernel_regularizer=regularizers.l2(lmbd)))
model.add(layers.Dense(n_categories, activation='softmax', kernel_regularizer=regularizers.l2(lmbd)))
sgd = optimizers.SGD(lr=eta)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
return model
epochs = 100
batch_size = 100
input_shape = X_train.shape[1:4]
receptive_field = 3
n_filters = 10
n_neurons_connected = 50
n_categories = 10
eta_vals = np.logspace(-5, 1, 7)
lmbd_vals = np.logspace(-5, 1, 7)
8.17. Final part¶
CNN_keras = np.zeros((len(eta_vals), len(lmbd_vals)), dtype=object)
for i, eta in enumerate(eta_vals):
for j, lmbd in enumerate(lmbd_vals):
CNN = create_convolutional_neural_network_keras(input_shape, receptive_field,
n_filters, n_neurons_connected, n_categories,
eta, lmbd)
CNN.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size, verbose=0)
scores = CNN.evaluate(X_test, Y_test)
CNN_keras[i][j] = CNN
print("Learning rate = ", eta)
print("Lambda = ", lmbd)
print("Test accuracy: %.3f" % scores[1])
print()
1/12 [=>............................] - ETA: 0s - loss: 3.0150 - accuracy: 0.1250
12/12 [==============================] - 0s 958us/step - loss: 2.9441 - accuracy: 0.0722
Learning rate = 1e-05
Lambda = 1e-05
Test accuracy: 0.072
1/12 [=>............................] - ETA: 0s - loss: 3.3024 - accuracy: 0.0312
12/12 [==============================] - 0s 957us/step - loss: 3.0967 - accuracy: 0.1389
Learning rate = 1e-05
Lambda = 0.0001
Test accuracy: 0.139
1/12 [=>............................] - ETA: 0s - loss: 2.9008 - accuracy: 0.2188
12/12 [==============================] - 0s 1ms/step - loss: 3.4843 - accuracy: 0.1028
Learning rate = 1e-05
Lambda = 0.001
Test accuracy: 0.103
1/12 [=>............................] - ETA: 0s - loss: 3.7695 - accuracy: 0.1250
12/12 [==============================] - 0s 968us/step - loss: 3.6647 - accuracy: 0.1417
Learning rate = 1e-05
Lambda = 0.01
Test accuracy: 0.142
1/12 [=>............................] - ETA: 0s - loss: 12.4141 - accuracy: 0.0938
12/12 [==============================] - 0s 960us/step - loss: 12.3084 - accuracy: 0.1028
Learning rate = 1e-05
Lambda = 0.1
Test accuracy: 0.103
1/12 [=>............................] - ETA: 0s - loss: 92.7441 - accuracy: 0.0938
12/12 [==============================] - 0s 959us/step - loss: 91.7224 - accuracy: 0.1306
Learning rate = 1e-05
Lambda = 1.0
Test accuracy: 0.131
1/12 [=>............................] - ETA: 0s - loss: 514.1083 - accuracy: 0.0625
12/12 [==============================] - 0s 958us/step - loss: 513.9800 - accuracy: 0.1111
Learning rate = 1e-05
Lambda = 10.0
Test accuracy: 0.111
8.18. Final visualization¶
# visual representation of grid search
# uses seaborn heatmap, could probably do this in matplotlib
import seaborn as sns
sns.set()
train_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))
test_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))
for i in range(len(eta_vals)):
for j in range(len(lmbd_vals)):
CNN = CNN_keras[i][j]
train_accuracy[i][j] = CNN.evaluate(X_train, Y_train)[1]
test_accuracy[i][j] = CNN.evaluate(X_test, Y_test)[1]
fig, ax = plt.subplots(figsize = (10, 10))
sns.heatmap(train_accuracy, annot=True, ax=ax, cmap="viridis")
ax.set_title("Training Accuracy")
ax.set_ylabel("$\eta$")
ax.set_xlabel("$\lambda$")
plt.show()
fig, ax = plt.subplots(figsize = (10, 10))
sns.heatmap(test_accuracy, annot=True, ax=ax, cmap="viridis")
ax.set_title("Test Accuracy")
ax.set_ylabel("$\eta$")
ax.set_xlabel("$\lambda$")
plt.show()
8.19. The CIFAR01 data set¶
The CIFAR10 dataset contains 60,000 color images in 10 classes, with 6,000 images in each class. The dataset is divided into 50,000 training images and 10,000 testing images. The classes are mutually exclusive and there is no overlap between them.
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
# We import the data set
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
# Normalize pixel values to be between 0 and 1 by dividing by 255.
train_images, test_images = train_images / 255.0, test_images / 255.0
8.20. Verifying the data set¶
To verify that the dataset looks correct, let’s plot the first 25 images from the training set and display the class name below each image.
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
# The CIFAR labels happen to be arrays,
# which is why you need the extra index
plt.xlabel(class_names[train_labels[i][0]])
plt.show()
8.21. Set up the model¶
The 6 lines of code below define the convolutional base using a common pattern: a stack of Conv2D and MaxPooling2D layers.
As input, a CNN takes tensors of shape (image_height, image_width, color_channels), ignoring the batch size. If you are new to these dimensions, color_channels refers to (R,G,B). In this example, you will configure our CNN to process inputs of shape (32, 32, 3), which is the format of CIFAR images. You can do this by passing the argument input_shape to our first layer.
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
# Let's display the architecture of our model so far.
model.summary()
You can see that the output of every Conv2D and MaxPooling2D layer is a 3D tensor of shape (height, width, channels). The width and height dimensions tend to shrink as you go deeper in the network. The number of output channels for each Conv2D layer is controlled by the first argument (e.g., 32 or 64). Typically, as the width and height shrink, you can afford (computationally) to add more output channels in each Conv2D layer.
8.22. Add Dense layers on top¶
To complete our model, you will feed the last output tensor from the convolutional base (of shape (4, 4, 64)) into one or more Dense layers to perform classification. Dense layers take vectors as input (which are 1D), while the current output is a 3D tensor. First, you will flatten (or unroll) the 3D output to 1D, then add one or more Dense layers on top. CIFAR has 10 output classes, so you use a final Dense layer with 10 outputs and a softmax activation.
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
Here's the complete architecture of our model.
model.summary()
As you can see, our (4, 4, 64) outputs were flattened into vectors of shape (1024) before going through two Dense layers.
8.23. Compile and train the model¶
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
8.24. Finally, evaluate the model¶
plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.5, 1])
plt.legend(loc='lower right')
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(test_acc)
8.25. Recurrent neural networks: Overarching view¶
Till now our focus has been, including convolutional neural networks as well, on feedforward neural networks. The output or the activations flow only in one direction, from the input layer to the output layer.
A recurrent neural network (RNN) looks very much like a feedforward neural network, except that it also has connections pointing backward.
RNNs are used to analyze time series data such as stock prices, and tell you when to buy or sell. In autonomous driving systems, they can anticipate car trajectories and help avoid accidents. More generally, they can work on sequences of arbitrary lengths, rather than on fixed-sized inputs like all the nets we have discussed so far. For example, they can take sentences, documents, or audio samples as input, making them extremely useful for natural language processing systems such as automatic translation and speech-to-text.
8.26. Set up of an RNN¶
Text to come.
8.27. A simple example¶
# Start importing packages
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Dense, SimpleRNN, LSTM, GRU
from tensorflow.keras import optimizers
from tensorflow.keras import regularizers
from tensorflow.keras.utils import to_categorical
# convert into dataset matrix
def convertToMatrix(data, step):
X, Y =[], []
for i in range(len(data)-step):
d=i+step
X.append(data[i:d,])
Y.append(data[d,])
return np.array(X), np.array(Y)
step = 4
N = 1000
Tp = 800
t=np.arange(0,N)
x=np.sin(0.02*t)+2*np.random.rand(N)
df = pd.DataFrame(x)
df.head()
plt.plot(df)
plt.show()
values=df.values
train,test = values[0:Tp,:], values[Tp:N,:]
# add step elements into train and test
test = np.append(test,np.repeat(test[-1,],step))
train = np.append(train,np.repeat(train[-1,],step))
trainX,trainY =convertToMatrix(train,step)
testX,testY =convertToMatrix(test,step)
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
model = Sequential()
model.add(SimpleRNN(units=32, input_shape=(1,step), activation="relu"))
model.add(Dense(8, activation="relu"))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='rmsprop')
model.summary()
model.fit(trainX,trainY, epochs=100, batch_size=16, verbose=2)
trainPredict = model.predict(trainX)
testPredict= model.predict(testX)
predicted=np.concatenate((trainPredict,testPredict),axis=0)
trainScore = model.evaluate(trainX, trainY, verbose=0)
print(trainScore)
index = df.index.values
plt.plot(index,df)
plt.plot(index,predicted)
plt.axvline(df.index[Tp], c="r")
plt.show()
8.28. An extrapolation example¶
The following code provides an example of how recurrent neural networks can be used to extrapolate to unknown values of physics data sets. Specifically, the data sets used in this program come from a quantum mechanical many-body calculation of energies as functions of the number of particles.
# For matrices and calculations
import numpy as np
# For machine learning (backend for keras)
import tensorflow as tf
# User-friendly machine learning library
# Front end for TensorFlow
import tensorflow.keras
# Different methods from Keras needed to create an RNN
# This is not necessary but it shortened function calls
# that need to be used in the code.
from tensorflow.keras import datasets, layers, models
from tensorflow.keras.layers import Input
from tensorflow.keras import regularizers
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Dense, SimpleRNN, LSTM, GRU
# For timing the code
from timeit import default_timer as timer
# For plotting
import matplotlib.pyplot as plt
# The data set
datatype='VaryDimension'
X_tot = np.arange(2, 42, 2)
y_tot = np.array([-0.03077640549, -0.08336233266, -0.1446729567, -0.2116753732, -0.2830637392, -0.3581341341, -0.436462435, -0.5177783846,
-0.6019067271, -0.6887363571, -0.7782028952, -0.8702784034, -0.9649652536, -1.062292565, -1.16231451,
-1.265109911, -1.370782966, -1.479465113, -1.591317992, -1.70653767])
8.29. Formatting the Data¶
The way the recurrent neural networks are trained in this program differs from how machine learning algorithms are usually trained. Typically a machine learning algorithm is trained by learning the relationship between the x data and the y data. In this program, the recurrent neural network will be trained to recognize the relationship in a sequence of y values. This is type of data formatting is typically used time series forcasting, but it can also be used in any extrapolation (time series forecasting is just a specific type of extrapolation along the time axis). This method of data formatting does not use the x data and assumes that the y data are evenly spaced.
For a standard machine learning algorithm, the training data has the form of (x,y) so the machine learning algorithm learns to assiciate a y value with a given x value. This is useful when the test data has x values within the same range as the training data. However, for this application, the x values of the test data are outside of the x values of the training data and the traditional method of training a machine learning algorithm does not work as well. For this reason, the recurrent neural network is trained on sequences of y values of the form ((y1, y2), y3), so that the network is concerned with learning the pattern of the y data and not the relation between the x and y data. As long as the pattern of y data outside of the training region stays relatively stable compared to what was inside the training region, this method of training can produce accurate extrapolations to y values far removed from the training data set.
# FORMAT_DATA
def format_data(data, length_of_sequence = 2):
"""
Inputs:
data(a numpy array): the data that will be the inputs to the recurrent neural
network
length_of_sequence (an int): the number of elements in one iteration of the
sequence patter. For a function approximator use length_of_sequence = 2.
Returns:
rnn_input (a 3D numpy array): the input data for the recurrent neural network. Its
dimensions are length of data - length of sequence, length of sequence,
dimnsion of data
rnn_output (a numpy array): the training data for the neural network
Formats data to be used in a recurrent neural network.
"""
X, Y = [], []
for i in range(len(data)-length_of_sequence):
# Get the next length_of_sequence elements
a = data[i:i+length_of_sequence]
# Get the element that immediately follows that
b = data[i+length_of_sequence]
# Reshape so that each data point is contained in its own array
a = np.reshape (a, (len(a), 1))
X.append(a)
Y.append(b)
rnn_input = np.array(X)
rnn_output = np.array(Y)
return rnn_input, rnn_output
# ## Defining the Recurrent Neural Network Using Keras
#
# The following method defines a simple recurrent neural network in keras consisting of one input layer, one hidden layer, and one output layer.
def rnn(length_of_sequences, batch_size = None, stateful = False):
"""
Inputs:
length_of_sequences (an int): the number of y values in "x data". This is determined
when the data is formatted
batch_size (an int): Default value is None. See Keras documentation of SimpleRNN.
stateful (a boolean): Default value is False. See Keras documentation of SimpleRNN.
Returns:
model (a Keras model): The recurrent neural network that is built and compiled by this
method
Builds and compiles a recurrent neural network with one hidden layer and returns the model.
"""
# Number of neurons in the input and output layers
in_out_neurons = 1
# Number of neurons in the hidden layer
hidden_neurons = 200
# Define the input layer
inp = Input(batch_shape=(batch_size,
length_of_sequences,
in_out_neurons))
# Define the hidden layer as a simple RNN layer with a set number of neurons and add it to
# the network immediately after the input layer
rnn = SimpleRNN(hidden_neurons,
return_sequences=False,
stateful = stateful,
name="RNN")(inp)
# Define the output layer as a dense neural network layer (standard neural network layer)
#and add it to the network immediately after the hidden layer.
dens = Dense(in_out_neurons,name="dense")(rnn)
# Create the machine learning model starting with the input layer and ending with the
# output layer
model = Model(inputs=[inp],outputs=[dens])
# Compile the machine learning model using the mean squared error function as the loss
# function and an Adams optimizer.
model.compile(loss="mean_squared_error", optimizer="adam")
return model
8.30. Predicting New Points With A Trained Recurrent Neural Network¶
def test_rnn (x1, y_test, plot_min, plot_max):
"""
Inputs:
x1 (a list or numpy array): The complete x component of the data set
y_test (a list or numpy array): The complete y component of the data set
plot_min (an int or float): the smallest x value used in the training data
plot_max (an int or float): the largest x valye used in the training data
Returns:
None.
Uses a trained recurrent neural network model to predict future points in the
series. Computes the MSE of the predicted data set from the true data set, saves
the predicted data set to a csv file, and plots the predicted and true data sets w
while also displaying the data range used for training.
"""
# Add the training data as the first dim points in the predicted data array as these
# are known values.
y_pred = y_test[:dim].tolist()
# Generate the first input to the trained recurrent neural network using the last two
# points of the training data. Based on how the network was trained this means that it
# will predict the first point in the data set after the training data. All of the
# brackets are necessary for Tensorflow.
next_input = np.array([[[y_test[dim-2]], [y_test[dim-1]]]])
# Save the very last point in the training data set. This will be used later.
last = [y_test[dim-1]]
# Iterate until the complete data set is created.
for i in range (dim, len(y_test)):
# Predict the next point in the data set using the previous two points.
next = model.predict(next_input)
# Append just the number of the predicted data set
y_pred.append(next[0][0])
# Create the input that will be used to predict the next data point in the data set.
next_input = np.array([[last, next[0]]], dtype=np.float64)
last = next
# Print the mean squared error between the known data set and the predicted data set.
print('MSE: ', np.square(np.subtract(y_test, y_pred)).mean())
# Save the predicted data set as a csv file for later use
name = datatype + 'Predicted'+str(dim)+'.csv'
np.savetxt(name, y_pred, delimiter=',')
# Plot the known data set and the predicted data set. The red box represents the region that was used
# for the training data.
fig, ax = plt.subplots()
ax.plot(x1, y_test, label="true", linewidth=3)
ax.plot(x1, y_pred, 'g-.',label="predicted", linewidth=4)
ax.legend()
# Created a red region to represent the points used in the training data.
ax.axvspan(plot_min, plot_max, alpha=0.25, color='red')
plt.show()
# Check to make sure the data set is complete
assert len(X_tot) == len(y_tot)
# This is the number of points that will be used in as the training data
dim=12
# Separate the training data from the whole data set
X_train = X_tot[:dim]
y_train = y_tot[:dim]
# Generate the training data for the RNN, using a sequence of 2
rnn_input, rnn_training = format_data(y_train, 2)
# Create a recurrent neural network in Keras and produce a summary of the
# machine learning model
model = rnn(length_of_sequences = rnn_input.shape[1])
model.summary()
# Start the timer. Want to time training+testing
start = timer()
# Fit the model using the training data genenerated above using 150 training iterations and a 5%
# validation split. Setting verbose to True prints information about each training iteration.
hist = model.fit(rnn_input, rnn_training, batch_size=None, epochs=150,
verbose=True,validation_split=0.05)
for label in ["loss","val_loss"]:
plt.plot(hist.history[label],label=label)
plt.ylabel("loss")
plt.xlabel("epoch")
plt.title("The final validation loss: {}".format(hist.history["val_loss"][-1]))
plt.legend()
plt.show()
# Use the trained neural network to predict more points of the data set
test_rnn(X_tot, y_tot, X_tot[0], X_tot[dim-1])
# Stop the timer and calculate the total time needed.
end = timer()
print('Time: ', end-start)
8.31. Other Things to Try¶
Changing the size of the recurrent neural network and its parameters can drastically change the results you get from the model. The below code takes the simple recurrent neural network from above and adds a second hidden layer, changes the number of neurons in the hidden layer, and explicitly declares the activation function of the hidden layers to be a sigmoid function. The loss function and optimizer can also be changed but are kept the same as the above network. These parameters can be tuned to provide the optimal result from the network. For some ideas on how to improve the performance of a recurrent neural network.
def rnn_2layers(length_of_sequences, batch_size = None, stateful = False):
"""
Inputs:
length_of_sequences (an int): the number of y values in "x data". This is determined
when the data is formatted
batch_size (an int): Default value is None. See Keras documentation of SimpleRNN.
stateful (a boolean): Default value is False. See Keras documentation of SimpleRNN.
Returns:
model (a Keras model): The recurrent neural network that is built and compiled by this
method
Builds and compiles a recurrent neural network with two hidden layers and returns the model.
"""
# Number of neurons in the input and output layers
in_out_neurons = 1
# Number of neurons in the hidden layer, increased from the first network
hidden_neurons = 500
# Define the input layer
inp = Input(batch_shape=(batch_size,
length_of_sequences,
in_out_neurons))
# Create two hidden layers instead of one hidden layer. Explicitly set the activation
# function to be the sigmoid function (the default value is hyperbolic tangent)
rnn1 = SimpleRNN(hidden_neurons,
return_sequences=True, # This needs to be True if another hidden layer is to follow
stateful = stateful, activation = 'sigmoid',
name="RNN1")(inp)
rnn2 = SimpleRNN(hidden_neurons,
return_sequences=False, activation = 'sigmoid',
stateful = stateful,
name="RNN2")(rnn1)
# Define the output layer as a dense neural network layer (standard neural network layer)
#and add it to the network immediately after the hidden layer.
dens = Dense(in_out_neurons,name="dense")(rnn2)
# Create the machine learning model starting with the input layer and ending with the
# output layer
model = Model(inputs=[inp],outputs=[dens])
# Compile the machine learning model using the mean squared error function as the loss
# function and an Adams optimizer.
model.compile(loss="mean_squared_error", optimizer="adam")
return model
# Check to make sure the data set is complete
assert len(X_tot) == len(y_tot)
# This is the number of points that will be used in as the training data
dim=12
# Separate the training data from the whole data set
X_train = X_tot[:dim]
y_train = y_tot[:dim]
# Generate the training data for the RNN, using a sequence of 2
rnn_input, rnn_training = format_data(y_train, 2)
# Create a recurrent neural network in Keras and produce a summary of the
# machine learning model
model = rnn_2layers(length_of_sequences = 2)
model.summary()
# Start the timer. Want to time training+testing
start = timer()
# Fit the model using the training data genenerated above using 150 training iterations and a 5%
# validation split. Setting verbose to True prints information about each training iteration.
hist = model.fit(rnn_input, rnn_training, batch_size=None, epochs=150,
verbose=True,validation_split=0.05)
# This section plots the training loss and the validation loss as a function of training iteration.
# This is not required for analyzing the couple cluster data but can help determine if the network is
# being overtrained.
for label in ["loss","val_loss"]:
plt.plot(hist.history[label],label=label)
plt.ylabel("loss")
plt.xlabel("epoch")
plt.title("The final validation loss: {}".format(hist.history["val_loss"][-1]))
plt.legend()
plt.show()
# Use the trained neural network to predict more points of the data set
test_rnn(X_tot, y_tot, X_tot[0], X_tot[dim-1])
# Stop the timer and calculate the total time needed.
end = timer()
print('Time: ', end-start)
8.32. Other Types of Recurrent Neural Networks¶
Besides a simple recurrent neural network layer, there are two other commonly used types of recurrent neural network layers: Long Short Term Memory (LSTM) and Gated Recurrent Unit (GRU). For a short introduction to these layers see https://medium.com/mindboard/lstm-vs-gru-experimental-comparison-955820c21e8b and https://medium.com/mindboard/lstm-vs-gru-experimental-comparison-955820c21e8b.
The first network created below is similar to the previous network, but it replaces the SimpleRNN layers with LSTM layers. The second network below has two hidden layers made up of GRUs, which are preceeded by two dense (feeddorward) neural network layers. These dense layers “preprocess” the data before it reaches the recurrent layers. This architecture has been shown to improve the performance of recurrent neural networks (see the link above and also https://arxiv.org/pdf/1807.02857.pdf.
def lstm_2layers(length_of_sequences, batch_size = None, stateful = False):
"""
Inputs:
length_of_sequences (an int): the number of y values in "x data". This is determined
when the data is formatted
batch_size (an int): Default value is None. See Keras documentation of SimpleRNN.
stateful (a boolean): Default value is False. See Keras documentation of SimpleRNN.
Returns:
model (a Keras model): The recurrent neural network that is built and compiled by this
method
Builds and compiles a recurrent neural network with two LSTM hidden layers and returns the model.
"""
# Number of neurons on the input/output layer and the number of neurons in the hidden layer
in_out_neurons = 1
hidden_neurons = 250
# Input Layer
inp = Input(batch_shape=(batch_size,
length_of_sequences,
in_out_neurons))
# Hidden layers (in this case they are LSTM layers instead if SimpleRNN layers)
rnn= LSTM(hidden_neurons,
return_sequences=True,
stateful = stateful,
name="RNN", use_bias=True, activation='tanh')(inp)
rnn1 = LSTM(hidden_neurons,
return_sequences=False,
stateful = stateful,
name="RNN1", use_bias=True, activation='tanh')(rnn)
# Output layer
dens = Dense(in_out_neurons,name="dense")(rnn1)
# Define the midel
model = Model(inputs=[inp],outputs=[dens])
# Compile the model
model.compile(loss='mean_squared_error', optimizer='adam')
# Return the model
return model
def dnn2_gru2(length_of_sequences, batch_size = None, stateful = False):
"""
Inputs:
length_of_sequences (an int): the number of y values in "x data". This is determined
when the data is formatted
batch_size (an int): Default value is None. See Keras documentation of SimpleRNN.
stateful (a boolean): Default value is False. See Keras documentation of SimpleRNN.
Returns:
model (a Keras model): The recurrent neural network that is built and compiled by this
method
Builds and compiles a recurrent neural network with four hidden layers (two dense followed by
two GRU layers) and returns the model.
"""
# Number of neurons on the input/output layers and hidden layers
in_out_neurons = 1
hidden_neurons = 250
# Input layer
inp = Input(batch_shape=(batch_size,
length_of_sequences,
in_out_neurons))
# Hidden Dense (feedforward) layers
dnn = Dense(hidden_neurons/2, activation='relu', name='dnn')(inp)
dnn1 = Dense(hidden_neurons/2, activation='relu', name='dnn1')(dnn)
# Hidden GRU layers
rnn1 = GRU(hidden_neurons,
return_sequences=True,
stateful = stateful,
name="RNN1", use_bias=True)(dnn1)
rnn = GRU(hidden_neurons,
return_sequences=False,
stateful = stateful,
name="RNN", use_bias=True)(rnn1)
# Output layer
dens = Dense(in_out_neurons,name="dense")(rnn)
# Define the model
model = Model(inputs=[inp],outputs=[dens])
# Compile the mdoel
model.compile(loss='mean_squared_error', optimizer='adam')
# Return the model
return model
# Check to make sure the data set is complete
assert len(X_tot) == len(y_tot)
# This is the number of points that will be used in as the training data
dim=12
# Separate the training data from the whole data set
X_train = X_tot[:dim]
y_train = y_tot[:dim]
# Generate the training data for the RNN, using a sequence of 2
rnn_input, rnn_training = format_data(y_train, 2)
# Create a recurrent neural network in Keras and produce a summary of the
# machine learning model
# Change the method name to reflect which network you want to use
model = dnn2_gru2(length_of_sequences = 2)
model.summary()
# Start the timer. Want to time training+testing
start = timer()
# Fit the model using the training data genenerated above using 150 training iterations and a 5%
# validation split. Setting verbose to True prints information about each training iteration.
hist = model.fit(rnn_input, rnn_training, batch_size=None, epochs=150,
verbose=True,validation_split=0.05)
# This section plots the training loss and the validation loss as a function of training iteration.
# This is not required for analyzing the couple cluster data but can help determine if the network is
# being overtrained.
for label in ["loss","val_loss"]:
plt.plot(hist.history[label],label=label)
plt.ylabel("loss")
plt.xlabel("epoch")
plt.title("The final validation loss: {}".format(hist.history["val_loss"][-1]))
plt.legend()
plt.show()
# Use the trained neural network to predict more points of the data set
test_rnn(X_tot, y_tot, X_tot[0], X_tot[dim-1])
# Stop the timer and calculate the total time needed.
end = timer()
print('Time: ', end-start)
# ### Training Recurrent Neural Networks in the Standard Way (i.e. learning the relationship between the X and Y data)
#
# Finally, comparing the performace of a recurrent neural network using the standard data formatting to the performance of the network with time sequence data formatting shows the benefit of this type of data formatting with extrapolation.
# Check to make sure the data set is complete
assert len(X_tot) == len(y_tot)
# This is the number of points that will be used in as the training data
dim=12
# Separate the training data from the whole data set
X_train = X_tot[:dim]
y_train = y_tot[:dim]
# Reshape the data for Keras specifications
X_train = X_train.reshape((dim, 1))
y_train = y_train.reshape((dim, 1))
# Create a recurrent neural network in Keras and produce a summary of the
# machine learning model
# Set the sequence length to 1 for regular data formatting
model = rnn(length_of_sequences = 1)
model.summary()
# Start the timer. Want to time training+testing
start = timer()
# Fit the model using the training data genenerated above using 150 training iterations and a 5%
# validation split. Setting verbose to True prints information about each training iteration.
hist = model.fit(X_train, y_train, batch_size=None, epochs=150,
verbose=True,validation_split=0.05)
# This section plots the training loss and the validation loss as a function of training iteration.
# This is not required for analyzing the couple cluster data but can help determine if the network is
# being overtrained.
for label in ["loss","val_loss"]:
plt.plot(hist.history[label],label=label)
plt.ylabel("loss")
plt.xlabel("epoch")
plt.title("The final validation loss: {}".format(hist.history["val_loss"][-1]))
plt.legend()
plt.show()
# Use the trained neural network to predict the remaining data points
X_pred = X_tot[dim:]
X_pred = X_pred.reshape((len(X_pred), 1))
y_model = model.predict(X_pred)
y_pred = np.concatenate((y_tot[:dim], y_model.flatten()))
# Plot the known data set and the predicted data set. The red box represents the region that was used
# for the training data.
fig, ax = plt.subplots()
ax.plot(X_tot, y_tot, label="true", linewidth=3)
ax.plot(X_tot, y_pred, 'g-.',label="predicted", linewidth=4)
ax.legend()
# Created a red region to represent the points used in the training data.
ax.axvspan(X_tot[0], X_tot[dim], alpha=0.25, color='red')
plt.show()
# Stop the timer and calculate the total time needed.
end = timer()
print('Time: ', end-start)


Event Bar
Enter a number and see it rounded!
Another Round!
Clear Results
Results come here to cool off!


Kevin J Marley
Democrat
Brewer, Denver ChopHouse & Brewery (over 10 years)
High School, 1 year at Colorado Mt College Leadier , 1 year CU Boulder
80238
Some door to door canvassing, some online texting, attended 2017 Democratic Party "reconstruction" meeting, have attended most of the last 10 years Democratic Party Caucuses (local level), was on a committee to hire a principal at Sandoval Elementary in Denver in 2009?
Block Capt. for past 2?years, canvassed for the Clinton Campaign 2016, donated Sanders Campaign 2016, donated Warren Campaign 2020, some online campaign work (unpaid) Warren campaign, 2020, a few small out of state congressional primary and seat donations in Montana Arizona ? and Tennessee. (less than $200... all democrats)
Colorado/ Denver Democratic Party.
I believe strongly in non partisan voting districts. I am strongly committed to ending gerrymandering and increasing the power of individual votes locally and nationally. I believe that citizens only participate in government when they think their participation matters and government can not work properly unless citizens participate in electing and overseeing the government.
Acknowledging my personal biases and being honest and open about exploring how deep they run and how they affect my judgement as I engage with others who have concerns that I may not share or have a prejudicial attitude toward. Listening to what others have to say and making an honest and rigorous attempt to understand what it is they are saying while keeping a firm grasp on the goal of having the overall needs each locale represented fairly by the sub sequentially elected government.
I am Brewer by trade in a small brewery where equipment, production and personnel problems arise on a regular basis. It is necessary in my trade to gather as many facts as possible before drawing conclusions and when drawing conclusions in is necessary to carefully examine whether or not those conclusions are valid and to what degree they rely on assumptions and how firm are the bases for those assumptions. (BTW this form is asking me for a racial/ ethnic identification and giving the category "Caucasian" for those of European origin. The word implies that those claiming to be of that racial group are descended from the survivors of the Ark of Noah which according to the Bible washed up in the Caucasus Mountains after, according to the story, every other human had perished. I find this category the epitome of racism as the implication is that only "Caucasians" are human)
Male


The InterruptPortMM class is designed to send an .NET event to a subscriber when the state (high or low) of a GPIO pin on the Raspberry Pi changes. The RPICSIO library usually provides classes for both SYSFS and Memory Mapped access however the InterruptPort functionality is only available in MemoryMapped mode.
The InterruptPortMM class automatically configures the GPIO as an input in exactly the same way as a normal InputPort. Note that you will need to configure pull-up and pull-down resistors on the port in exactly the same way as the InputPort classes.
The example code below uses a GPIO value of 4 which is exposed as pin 7 on the header. In general, on the Raspberry Pi 2, these GPIOs are available for input or output by default. However it is possible to adjust the pinmux with the device tree to use header pins for alternate devices. If you are messing about with the Device Tree you will probably know what you have configured specific pins to use.
The GPIOs themselves can be either outputs or inputs and it is possible to configure internal pull-up or pull-down resistors on an input. The act of creating an RPICSIO InterruptPort class on a GPIO will configure it as an input. However, you will need to configure the pull-up or pull-down resistors as required. The InterruptPort class has function calls for this purpose.
Always be aware that the Raspbian Linux running on the Raspberry Pi is not a real-time operating system. This means, for example, although you might set up an InterruptPort object to trigger on a the state change of a GPIO, the actual timing of some of the events may vary (and there may be lengthy gaps) as the process is pre-emptively swapped in and out by the kernel.
Be aware of the RPI max input voltage (3.3v) and pullup/pulldown resistors. Be really careful about the voltage you provide here. In general it is usually a good idea to run the input through some sort of buffer to clamp the max input at the RPI maximum.
This is not a true real-time interrupt. It has a lot more in common with a C language poll() call. Please see the comments on the InteruptPortMM documentation for notes on its method of operation.
IMPORTANT NOTE: Before creating any RPICSIO Port class you will need to ensure that the RPICSIO Library knows which type of Raspberry Pi you are dealing with. This is done by executing a call (only once is necessary) like the one below somewhere in your code before you create any port.
RPICSIOConfig.Instance.RPIType = RPITypeEnum.RPITYPE_RPI2;
The code below illustrates how to use the InterruptPortMM class to monitor the state of a GPIO pin and send an event to a remote method (which can be in an entirely different class). The example interrupt handler simply outputs a summary of the event to the console.
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// Creates and monitors an interrupt port. /// </summary> /// /// NOTE: /// Be aware of the RPi max input voltage and pullup/pulldown resistors. /// Interrupt or not it the pin is still an input. Be really careful about /// the voltage you provide here. In general it is usually a good idea to /// run the input through some sort of buffer to cap the max input at the /// RPi maximum. /// /// NOTE: /// this is not a true interrupt. It has a lot more in common with a C /// language poll() call. /// /// STANDARD NOTE: /// Be aware that you have to tell RPICSIO what type of Pi you are using. This /// is done by issuing the call below somewhere before you open any port. It /// only needs to be done once (replace the '2' with a 3 or 0 as appropriate). /// /// RPICSIOConfig.Instance.RPIType = RPITypeEnum.RPITYPE_RPI2; /// /// SIMPLE Test Harness /// connect a wire to GPIO_4 (pin 7) /// /// Once you have done that the issue a call like /// SimpleInterruptPortMM(GpioEnum.GPIO_4, InterruptMode.InterruptRisingEdgeTransient); /// /// then ground the wire by touching it to pin 39. This should generate a /// output on the screen indicating the ground has happened. The events appear /// in a call to the IpPortGPIO_OnInterrupt code below. Note, usually a lot of /// events will be triggered because such a simple test system has no debouncing. /// /// <param name="eventInterruptModeIn">The interrupt mode</param> /// <param name="gpioIn">The gpio this port represents. </param> /// <history> /// 01 Dec 16 Cynic - Originally written /// </history> public void SimpleInterruptPortMM(GpioEnum gpioIn, InterruptMode eventInterruptModeIn) { if (gpioIn == GpioEnum.GPIO_NONE) { throw new Exception ("Bad interrupt port GpioEnum.GPIO_NONE"); } // create an interrupt port. InterruptPortMM ipPortGPIO = new InterruptPortMM(gpioIn, GPIOPullUpDownModeEnum.PULLUPDOWN_UP, eventInterruptModeIn); // Hook up an event handler to the OnInterrupt event // the IpPortGPIO_OnInterrupt function will receive the incoming event data ipPortGPIO.OnInterrupt += new InterruptEventHandlerMM(IpPortGPIO_OnInterrupt); // we will not get any data if it is not enabled ipPortGPIO.EnableInterrupt(); // run until we get key press on the console while (Console.KeyAvailable == false) { // sleep for a second Thread.Sleep(1000); } // this just stops all transmissions. The interrupt can be enabled again // NOTE: disabling the interrupt does not release the event resources ipPortGPIO.DisableInterrupt(); // close the port ipPortGPIO.ClosePort(); // usually a good idea to dispose() ipPortGPIO.Dispose(); // we never can use this object again after it is disposed. To use it // again we would have to re-create it ipPortGPIO = null; }The above code subscribes to the InterruptPort's OnInterrupt event and any events will appear in the named
IpPortGPIO_OnInterrupt
handler. Example code for the IpPortGPIO_OnInterrupt
handler is given below. You can
route more than one InterruptPort to the same handler - however to do anything useful you will need to
use the information passed in during the event to figure out which event was triggered.
Note that the code below executes in a different thread than the one in which the InterruptPortMM was created. You should not do any lengthy processing in the interrupt handler thread as no other interrupts can be processed until the current interrupt is cleared.
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// An InterruptPort interrupt event handler. This is where the data from /// the interrupt arrives. Note the EventData object contains more /// detailed information than the passed in arguments - including a /// reference to the InterruptPort object which generated the event. /// /// NOTE: /// Be aware that this handler method which accepts the data from /// an InterruptPort event is executing in the InterruptPort's event /// thread - not the main() thread. You have to be careful what you do /// in here to avoid the many issues associated with multi-threaded /// applications. /// /// NOTE: Be Quick. It is not advisable to take to long to process the /// incoming event data - you cannot receive another interrupt while /// processing the current one and the fact that a lost interrupt /// happened will not be recorded /// /// NOTE: /// this is not a true interrupt. It has a lot more in common with a C /// language poll() call. /// /// </summary> /// <param name="evGpio">The gpio the event is configured on</param> /// <param name="evState">The event state (1 or 0)</param> /// <param name="evTime">The event time</param> /// <param name="evData">The event data structure</param> /// <history> /// 01 Dec 16 Cynic - Originally written /// </history> public void IpPortGPIO_OnInterrupt(GpioEnum evGpio, bool evState, DateTime evTime, EventData evData) { // here we process and take action on the incoming data // in this particular example we just write it out to the // console if (evData != null) { // this function could receive data from multiple // interrupt ports. To decide what action to take we could // look at the user specified evCode or the GPIO number // down in the evData object. // just output some diagnostics Console.WriteLine (evData.ToString ()); // if we have to call ClearInterrupt() on the port // we can do it this way if (evData.PortObject != null) { evData.PortObject.ClearInterrupt(); } } }In the
SimpleInterruptPortMM
call, the gpioID
and eventInterruptModeIn
are passed in when the function was called.
These values are members of the
GpioEnum
and InterruptMode
enumerations.
The called would be made via a line which looks like:
SimpleInterruptPortMM(GpioEnum.GPIO_4, InterruptMode.InterruptRisingEdgeTransient);


Profs are handled and selected by the Head Prof. They will be looked at much stricter in terms of activity and type variety, so they tend to be versatile.


PhaseOne at McMaster attempts to support a wide array of different initiatives all aimed towards making technology easier for students to use and understand. We run weekly code nights aiming towards students who want to collaborate on building software. We also run workshops that aims to improving software literacy, along with speakers to engage and immerse the students within the software engineering community. Our goal here is to eliminate all of the barriers that students perceive about the current technology movement.
A new workshop held every month!
Get help from experienced coders!
Use our recommended tools!


Nova Film was started in 2015 by Joshua and Amanda Maiorino. They have a passion to importizlie your special day in a theatrical wedding video style.
A beatiful couple gets married. This video was a two camera shoot by Josh and Amanda.
This was the perfect wedding located in a small town in Connecicut. This video was a two camera shoot by Josh and Amanda.
A perfect July Wedding for a perfect couple. This wedding took place on the Historical Yale Campus.


Drag and Drop - IT fields and jobs
Let's try, drag and drop the cards under the correct column.
-
DEVELOPMENT
- Data Scientist
- Project Manager
-
BUSINESS & ADMIN
- Mobile App Programmer
- Data Center Operator
-
BIG DATA ANALYSIS
- Network Infrastructure
- Analyst Programmer
-
OPERATION
- Data Warehousing
- Business Analyst
Your result:


class: center, middle # Serving connected data with GraphQL .logo[![graph](images/graphql_logo.png)] ## Mike Williamson --- class: center, middle .eighty[![facebook projects](images/fb_projects.png)] ## Facebook's scale: _reductio ad absurdum_ for technology .quote[_reductio ad absurdum_: Disproof of a proposition by showing that it leads to absurd or untenable conclusions. ] --- class: center, middle # Not so long ago... .seventy[![3 tier](images/three_tier.png)] --- class: center, middle # In front of the server .seventy[![the_situation](images/the_situation.png)] --- class: center, middle # Behind the server #### APIs for everything * Other people's APIs * Microservices ```bash curl http://www.omdbapi.com/?t=Game%20of%20Thrones&Season=1&Episode=2 {"Title":"Game of Thrones","Year":"2011–","Rated":"TV-MA","Released":"17 Apr 2011","Runtime":"56 min","Genre":"Adventure, Drama, Fantasy","Director":"N/A","Writer":"David Benioff, D.B. Weiss","Actors":"Peter Dinklage, Lena Headey, Emilia Clarke, Kit Harington","Plot":"Nine noble families fight for control over the mythical lands of Westeros. Meanwhile, a forgotten race hell-bent on destruction returns after being dormant for thousands of years.","Language":"English","Country":"USA, UK","Awards":"Won 1 Golden Globe. Another 224 wins & 393 nominations.","Poster":"https://images-na.ssl-images-amazon.com/images/M/MV5BMjM5OTQ1MTY5Nl5BMl5BanBnXkFtZTgwMjM3NzMxODE@._V1_SX300.jpg","Metascore":"N/A","imdbRating":"9.5","imdbVotes":"1,113,297","imdbID":"tt0944947","Type":"series","totalSeasons":"8","Response":"True"} ``` #### So many databases * Relational|Graph|Key/Value|Document * Sometimes combining data from multiple databases * Polyglot persistance ```sql FOR customer IN customers FILTER customer.country == 'Canada' RETURN customer ``` ```sql SELECT * FROM customers WHERE country='Canada'; ``` --- class: center, middle # No graceful way forward ### Server side * API sprawl: Lots of endpoints that return very little (specific to scenario/feature) * Overfetching: Few endpoints that return a lot (let the client decide what they need) * Responses keep growing: Only add to responses, but never remove * API sprawl v2: API versioning (/v1/users/1.json) * if/else, if/else: Input validation sprinkled throughout your code ### Client side * When the client needs different data, the server needs to be changed * Client code dealing with multiple requests for related resources * Client code reformatting server responses * Ignoring parts of server response --- class: center, middle # Solving those problems * Ask for things you need * Ask for parts of those things * Asking for related things --- class: center, middle # Solving those problems * Ask for things you need: selection? * Ask for parts of those things: projection? * Asking for related things: joins? ### This is starting to sound like we need a query language... --- class: center, middle # GraphQL ### A query language that lives outside the database --- class: center, middle .onehundred[![request](images/graphql_request_response.png)] --- class: center, middle .onehundred[![request](images/graphql_request_response_schema.png)] --- class: center, middle ```javascript const schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: () => ({ hello: { type: GraphQLString, resolve: () => { return 'world' } } }) }) }) let query = ` { hello } ` ``` --- class: center, middle ```javascript // two inputs graphql(schema, query) // one output { data: { hello: 'world' } } ``` --- class: center, middle # Adjusting our schema to take arguments ```javascript const schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: () => ({ hello: { type: GraphQLString, resolve: () => { return 'world' } } }) }) }) ``` --- class: center, middle # Adjusting our schema to take arguments ```javascript const schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: () => ({ hello: { type: GraphQLString, resolve: (root) => { return 'world' } } }) }) }) ``` --- class: center, middle # Adjusting our schema to take arguments ```javascript const schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: () => ({ hello: { type: GraphQLString, resolve: (root, args) => { return 'world' } } }) }) }) ``` --- class: center, middle # Adjusting our schema to take arguments ```javascript const schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: () => ({ hello: { args: { }, type: GraphQLString, resolve: (root, args) => { return 'world' } } }) }) }) ``` --- class: center, middle # Adjusting our schema to take arguments ```javascript const schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: () => ({ hello: { args: { adjective: {} }, type: GraphQLString, resolve: (root, args) => { return 'world' } } }) }) }) ``` --- class: center, middle # Adjusting our schema to take arguments ```javascript const schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: () => ({ hello: { args: { adjective: { type: GraphQLString } }, type: GraphQLString, resolve: (root, args) => { return 'world' } } }) }) }) ``` --- class: center, middle # Adjusting our schema to take arguments ```javascript const schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: () => ({ hello: { args: { adjective: { type: GraphQLString } }, type: GraphQLString, resolve: (root, args) => { return args.adjective + ' world' } } }) }) }) ``` --- class: center, middle ```javascript graphql(schema, ` { hello(adjective: "beautiful") }`) // the output { data: { hello: 'beautiful world' } } ``` --- class: center, middle # Talking to a graph database .onehundred[![knows graph](images/arango_plus_graphql.png)] --- class: center, middle .onehundred[![knows graph](images/example_graphs.png)] --- class: center, middle .onehundred[![knows graph](images/knows_graph.png)] --- class: center, middle ```javascript async function getPersonByName (name) { let query = aql` FOR person IN persons FILTER person.name == ${ name } LIMIT 1 RETURN person ` let results = await db.query(query) return results.next() } ``` --- class: center, middle ```javascript let Person = new GraphQLObjectType({ name: 'Person', fields: () => ({ name: { type: GraphQLString } }) }) ``` --- class: center, middle ```javascript let schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: () => ({ person: { args: { name: { type: new GraphQLNonNull(GraphQLString) } }, type: Person, resolve: (root, args) => { return getPersonByName(args.name) }, } }) }) }) ``` --- class: center, middle ```javascript let query = ` { person(name: "Eve") { name } } ` graphql(schema, query) // the output { data: { name: 'Eve' } } ``` --- class: center, middle # Getting the rest of the graph ```javascript async function getFriends (id) { let query = aql` FOR vertex IN OUTBOUND ${id} knows RETURN vertex ` let results = await db.query(query) return await results.all() } ``` --- class: center, middle # Getting the rest of the graph ```javascript let Person = new GraphQLObjectType({ name: 'Person', fields: () => ({ name: { type: GraphQLString } }) }) ``` --- class: center, middle # Getting the rest of the graph ```javascript let Person = new GraphQLObjectType({ name: 'Person', fields: () => ({ name: { type: GraphQLString }, friends: { type: new GraphQLList(Person), resolve(root) { return getFriends(root._id) } } }) }) ``` --- class: center, middle ```javascript let query = ` { person(name: "Eve") { name friends { name } } } ` graphql(schema, query) ``` --- class: center, middle ```javascript { "data": { "person": { "name": "Eve", "friends": [ { "name": "Bob" }, { "name": "Alice" } ] } } } ``` --- class: center, middle ```javascript let query = ` { person(name: "Eve") { name friends { name friends { name } } } } ` graphql(schema, query) ``` --- class: center, middle .output[```javascript { "data": { "person": { "name": "Eve", "friends": [ { "name": "Bob", "friends": [ { "name": "Charlie" }, { "name": "Dave" } ] }, { "name": "Alice", "friends": [ { "name": "Bob" } ] } ] } } } ```] --- class: center, middle # Serving your schema with Express.js --- class: center, middle ```javascript import express from 'express' import graphqlHTTP from 'express-graphql' const app = express() app.use('/graphql', graphqlHTTP({ schema: schema })) app.listen(3000) // http://localhost:3000/graphql is up and running! ``` --- class: center, middle # GraphQL * Presents single unified _object graph_ * It just runs resolve functions and cares only that they produce what you said * Gives only what you ask for * Single request * Implementations in most languages ### What's awesome... * Cuts coupling between client and server * Schema can act as a contract for frontend/backend teams to build against * A perfect compliment to schemaless databases like ArangoDB * Security: Inputs are suddenly a formal language with a proper parser --- class: center, middle # Explore: * graphql.org * graphql.org/swapi-graphql # Questions? * Github: @sleepycat * Twitter: @dexterchief * mike@korora.ca


Nested Forms
- need writers to handle the
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| <select name='mixtape[mixtape_song_attributes][][song_id]'>
<% Song.all.each do |song| %>
<option value='<%= song.id %>'> <%= song.name %></option>
<% end %>
</select>
<%= select_tag('mixtape[mixtape_songs_attributes][][song_id]',
options_for_select(Song.all.collect{|s| [s.name, s.id]})) %>
#need a song in memory
@mixtape.mixtape_songs.build
<%= f.fields_for :mixtape_songs do |mixtape_song_builder| %>
<%= mixtape_song_builder.select :song_id, songs_for_select %>
<% end %>
#songs_for_select logic in a helper
|
- add a scope to your form with fields for
Pass in local variables and change that local variable’s name in the context of the new partial
when rendering a partial -> how do I pass in the f. variable.1
| <%= render 'songs/fields', :f => :song_form %>
|
f.object_name -> gives you the path of what f is
- This was valuable with specifying a :name => [“#{f.object_name}”][genre_name][]
settings represents the edit form for a user
instead of throwing it away-> label depreciated and put into a folder, since you might need that code later
HAML is whitespace sensitive, it’s very strict
Adding search to our application
Implementing Search1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #in routes
resources :songs do
collection do
get :search
end
end

Search Songs

<%= form_tag(search_songs_path, :method => :get) do %>
<%= text_field_tag :query %>
<%= submit_tag :search %>
<% end %>
#in songs controller -> fuzzy search
def search
#@songs = Song.find_all_by_name(params[:query])
#should put this in the model
@songs = Song.where("name LIKE ?", "%#{query}%")
render :action => 'index'
end
|


Home
Products
Account
Basket
Logout
Log in:
New here? Register
here
Back to
Homepage
Enter your email:
Enter your password:
Log in


ABOUT ME
Section for about me
Work
Section for about work
Education
Interests
Section for about interests


Lampard challenges young Chelsea players to step up
by Pulse News Agency International by AFPFrank Lampard has challenged his young Chelsea players to "make a mark" ahead of their crucial Champions League match against Lille on Tuesday after a series of poor results took the shine off their season.
Chelsea must beat the French side at Stamford Bridge to ensure safe passage to the knockout stages, with manager Lampard expecting far sharper focus than his players showed during the 3-1 weekend defeat to Everton.
He admitted Chelsea's recent poor form -- just one win in five games -- was his first big challenge as Blues boss, and wants his players to embrace the pressure of this week's must-win clash.
"It is a test for us and it was always going to come," Lampard said during his pre-match press conference on Monday.
"The way we are, the nature of the Premier League, this was never going to be a plain sailing, invincible season from our point of view. Teams need this test.
"Big games like this, they are crucial for the players. You can be defined as an individual and a group by success."
Lampard, who won the Champions League with Chelsea in 2012, said he had been fortunate to have enjoyed many big nights as a player.
"So these are big opportunities now for the team, particularly the young boys when it's quite new to them, to make a mark," he said. "So I think it's good, I like the pressure of this game being knockout.
"If we're going to do anything good here, we're going to have to have lots of these nights."
Lampard said his side's poor run had not yet altered his view on January recruitment plans after the club's FIFA transfer ban had been lifted.
"I wouldn't say my mind is set on anything to be changed," said Lampard, whose side are fourth in the Premier League despite their wobble.
"But you do learn from your players in tough times, not just in terms of what they do on the pitch but how their personality is.
"That was the disappointing thing at Everton. It wasn't just that we were slightly off -- I didn't like that there were individual contact moments across the game that we lost. Because that's a personality thing as much as anything else."
Antonio Rudiger is fit for just his second appearance of the season following groin trouble, with the Germany centre-back likely to step into the Blues' defence.
Lampard believes Rudiger can make an instant impact, with the 26-year-old's return proving timely as Fikayo Tomori misses out due to a minor hip issue.
"He's a good defender, it's good to have him back and he's training really well," said Lampard, of Rudiger.
"He has got a lot of his fitness work done since the injury's healed.
"People maybe overlooked the fact that we were missing a number of big players at the start of the season. Rudi is just back, so let's see what he can add to us now."
jw/iwd


| Source |
|
Link
|
{{semanticLinkTypes[record.linkType].title}}
|
Target |
|
|
|---|
There are no links in this table. You can add links by clicking the button above.