Learning
Learning probabilistic models. (Chapters 20)
- class probabilistic_learning.CountingProbDist(observations=None, default=0)[source]
Bases:
objectA probability distribution formed by observing and counting examples. If p is an instance of this class and o is an observed value, then there are 3 main operations: p.add(o) increments the count for observation o by 1. p.sample() returns a random element from the distribution. p[o] returns the probability for o (as in a regular ProbDist).
- probabilistic_learning.NaiveBayesLearner(dataset, continuous=True, simple=False)[source]
Return a naive Bayes classifier for
dataset, dispatching to the simple, continuous (Gaussian), or discrete variant according tosimpleandcontinuous.
- probabilistic_learning.NaiveBayesSimple(distribution)[source]
A simple naive bayes classifier that takes as input a dictionary of CountingProbDist objects and classifies items according to these distributions. The input dictionary is in the following form:
(ClassName, ClassProb): CountingProbDist
- probabilistic_learning.NaiveBayesDiscrete(dataset)[source]
Just count how many times each value of each input attribute occurs, conditional on the target value. Count the different target values too.
- probabilistic_learning.NaiveBayesContinuous(dataset)[source]
Count how many times each target value occurs. Also, find the means and deviations of input attribute values for each target value.
Deep learning. (Chapters 20)
- class deep_learning4e.Node(weights=None, value=None)[source]
Bases:
objectA single unit of a layer in a neural network :param weights: weights between parent nodes and current node :param value: value of current node
- class deep_learning4e.Layer(size)[source]
Bases:
objectA layer in a neural network based on a computational graph. :param size: number of units in the current layer
- class deep_learning4e.Activation[source]
Bases:
objectAbstract base class for neural-network activation functions.
Subclasses implement
functionand itsderivative; calling an instance applies the activation to its input.
- class deep_learning4e.Sigmoid[source]
Bases:
ActivationLogistic sigmoid activation,
1 / (1 + e**-x).
- class deep_learning4e.ReLU[source]
Bases:
ActivationRectified Linear Unit activation,
max(0, x).
- class deep_learning4e.ELU(alpha=0.01)[source]
Bases:
ActivationExponential Linear Unit activation, with scale
alphafor non-positive inputs.
- class deep_learning4e.LeakyReLU(alpha=0.01)[source]
Bases:
ActivationLeaky ReLU activation, with small slope
alphafor negative inputs.
- class deep_learning4e.Tanh[source]
Bases:
ActivationHyperbolic tangent activation.
- class deep_learning4e.SoftMax[source]
Bases:
ActivationSoftmax activation that normalises a vector into a probability distribution.
- class deep_learning4e.SoftPlus[source]
Bases:
ActivationSoftPlus activation,
log(1 + e**x)(a smooth approximation of ReLU).
- class deep_learning4e.Linear[source]
Bases:
ActivationIdentity (linear) activation that returns its input unchanged.
- class deep_learning4e.InputLayer(size=3)[source]
Bases:
Layer1D input layer. Layer size is the same as input vector size.
- class deep_learning4e.DenseLayer(in_size=3, out_size=3, activation=<class 'deep_learning4e.Sigmoid'>)[source]
Bases:
Layer1D dense layer in a neural network. :param in_size: (int) input vector size :param out_size: (int) output vector size :param activation: (Activation object) activation function
- class deep_learning4e.ConvLayer1D(size=3, kernel_size=3)[source]
Bases:
Layer1D convolution layer of in neural network. :param kernel_size: convolution kernel size
- class deep_learning4e.MaxPoolingLayer1D(size=3, kernel_size=3)[source]
Bases:
Layer1D max pooling layer in a neural network. :param kernel_size: max pooling area size
- class deep_learning4e.BatchNormalizationLayer(size, eps=0.001)[source]
Bases:
LayerBatch normalization layer.
- deep_learning4e.init_examples(examples, idx_i, idx_t, o_units)[source]
Init examples from dataset.examples.
- deep_learning4e.stochastic_gradient_descent(dataset, net, loss, epochs=1000, l_rate=0.01, batch_size=1, verbose=False)[source]
Gradient descent algorithm to update the learnable parameters of a network. :return: the updated network
- deep_learning4e.adam(dataset, net, loss, epochs=1000, rho=(0.9, 0.999), delta=1e-08, l_rate=0.001, batch_size=1, verbose=False)[source]
[Figure 19.6] Adam optimizer to update the learnable parameters of a network. Required parameters are similar to gradient descent. :return the updated network
- deep_learning4e.BackPropagation(inputs, targets, theta, net, loss)[source]
The back-propagation algorithm for multilayer networks in only one epoch, to calculate gradients of theta. :param inputs: a batch of inputs in an array. Each input is an iterable object :param targets: a batch of targets in an array. Each target is an iterable object :param theta: parameters to be updated :param net: a list of predefined layer objects representing their linear sequence :param loss: a predefined loss function taking array of inputs and targets :return: gradients of theta, loss of the input batch
- class deep_learning4e.NeuralNetworkLearner(dataset, hidden_layer_sizes, l_rate=0.01, epochs=1000, batch_size=10, optimizer=<function stochastic_gradient_descent>, loss=<function mean_squared_error_loss>, verbose=False, plot=False)[source]
Bases:
objectSimple dense multilayer neural network. :param hidden_layer_sizes: size of hidden layers in the form of a list
- class deep_learning4e.PerceptronLearner(dataset, l_rate=0.01, epochs=1000, batch_size=10, optimizer=<function stochastic_gradient_descent>, loss=<function mean_squared_error_loss>, verbose=False, plot=False)[source]
Bases:
objectSimple perceptron neural network.
- deep_learning4e.keras_dataset_loader(dataset, max_length=500)[source]
Helper function to load keras datasets. :param dataset: keras data set type :param max_length: max length of each input sequence
- deep_learning4e.SimpleRNNLearner(train_data, val_data, epochs=2, verbose=False)[source]
RNN example for text sentimental analysis.
- Parameters:
train_data – a tuple of (training data, targets) Training data: ndarray taking training examples, while each example is coded by embedding Targets: ndarray taking targets of each example. Each target is mapped to an integer
val_data – a tuple of (validation data, targets)
epochs – number of epochs
verbose – verbosity mode
- Returns:
a keras model
- deep_learning4e.AutoencoderLearner(inputs, encoding_size, epochs=200, verbose=False)[source]
Simple example of linear auto encoder learning producing the input itself. :param inputs: a batch of input data in np.ndarray type :param encoding_size: int, the size of encoding layer :param epochs: number of epochs :param verbose: verbosity mode :return: a keras model
Reinforcement Learning (Chapter 21)
- class reinforcement_learning.PassiveDUEAgent(pi, mdp)[source]
Bases:
objectPassive (non-learning) agent that uses direct utility estimation on a given MDP and policy:
import sys from mdp import sequential_decision_environment north = (0, 1) south = (0,-1) west = (-1, 0) east = (1, 0) policy = {(0, 2): east, (1, 2): east, (2, 2): east, (3, 2): None, (0, 1): north, (2, 1): north, (3, 1): None, (0, 0): north, (1, 0): west, (2, 0): west, (3, 0): west,} agent = PassiveDUEAgent(policy, sequential_decision_environment) for i in range(200): run_single_trial(agent,sequential_decision_environment) agent.estimate_U() agent.U[(0, 0)] > 0.2 True
- class reinforcement_learning.PassiveADPAgent(pi, mdp)[source]
Bases:
object[Figure 21.2] Passive (non-learning) agent that uses adaptive dynamic programming on a given MDP and policy:
import sys from mdp import sequential_decision_environment north = (0, 1) south = (0,-1) west = (-1, 0) east = (1, 0) policy = {(0, 2): east, (1, 2): east, (2, 2): east, (3, 2): None, (0, 1): north, (2, 1): north, (3, 1): None, (0, 0): north, (1, 0): west, (2, 0): west, (3, 0): west,} agent = PassiveADPAgent(policy, sequential_decision_environment) for i in range(100): run_single_trial(agent,sequential_decision_environment) agent.U[(0, 0)] > 0.2 True agent.U[(0, 1)] > 0.2 True
- class reinforcement_learning.PassiveTDAgent(pi, mdp, alpha=None)[source]
Bases:
object[Figure 21.4] The abstract class for a Passive (non-learning) agent that uses temporal differences to learn utility estimates. Override update_state method to convert percept to state and reward. The mdp being provided should be an instance of a subclass of the MDP Class:
import sys from mdp import sequential_decision_environment north = (0, 1) south = (0,-1) west = (-1, 0) east = (1, 0) policy = {(0, 2): east, (1, 2): east, (2, 2): east, (3, 2): None, (0, 1): north, (2, 1): north, (3, 1): None, (0, 0): north, (1, 0): west, (2, 0): west, (3, 0): west,} agent = PassiveTDAgent(policy, sequential_decision_environment, alpha=lambda n: 60./(59+n)) for i in range(200): run_single_trial(agent,sequential_decision_environment) agent.U[(0, 0)] > 0.2 True agent.U[(0, 1)] > 0.2 True
- class reinforcement_learning.QLearningAgent(mdp, Ne, Rplus, alpha=None)[source]
Bases:
object[Figure 21.8] An exploratory Q-learning agent. It avoids having to learn the transition model because the Q-value of a state can be related directly to those of its neighbors:
import sys from mdp import sequential_decision_environment north = (0, 1) south = (0,-1) west = (-1, 0) east = (1, 0) policy = {(0, 2): east, (1, 2): east, (2, 2): east, (3, 2): None, (0, 1): north, (2, 1): north, (3, 1): None, (0, 0): north, (1, 0): west, (2, 0): west, (3, 0): west,} q_agent = QLearningAgent(sequential_decision_environment, Ne=5, Rplus=2, alpha=lambda n: 60./(59+n)) for i in range(200): run_single_trial(q_agent,sequential_decision_environment) q_agent.Q[((0, 1), (0, 1))] >= -0.5 True q_agent.Q[((1, 0), (0, -1))] <= 0.5 True
- class reinforcement_learning.SARSALearningAgent(mdp, Ne, Rplus, alpha=None)[source]
Bases:
QLearningAgent[Section 21.3] An on-policy temporal-difference control agent (SARSA: State-Action-Reward- State-Action). It is identical to the Q-learning agent except for the update rule: instead of bootstrapping on the maximum Q-value over next actions, SARSA bootstraps on the Q-value of the action a1 that its exploration policy will actually take in the next state. Being on-policy, SARSA learns the value of the policy it is following, exploration included, rather than that of the greedy policy.
- reinforcement_learning.run_single_trial(agent_program, mdp)[source]
Execute trial for given agent_program and mdp. mdp should be an instance of subclass of mdp.MDP
Reinforcement Learning (Chapter 21)
- class reinforcement_learning4e.PassiveDUEAgent(pi, mdp)[source]
Bases:
objectPassive (non-learning) agent that uses direct utility estimation on a given MDP and policy:
import sys from mdp import sequential_decision_environment north = (0, 1) south = (0,-1) west = (-1, 0) east = (1, 0) policy = {(0, 2): east, (1, 2): east, (2, 2): east, (3, 2): None, (0, 1): north, (2, 1): north, (3, 1): None, (0, 0): north, (1, 0): west, (2, 0): west, (3, 0): west,} agent = PassiveDUEAgent(policy, sequential_decision_environment) for i in range(200): run_single_trial(agent,sequential_decision_environment) agent.estimate_U() agent.U[(0, 0)] > 0.2 True
- class reinforcement_learning4e.PassiveADPAgent(pi, mdp)[source]
Bases:
object[Figure 21.2] Passive (non-learning) agent that uses adaptive dynamic programming on a given MDP and policy:
import sys from mdp import sequential_decision_environment north = (0, 1) south = (0,-1) west = (-1, 0) east = (1, 0) policy = {(0, 2): east, (1, 2): east, (2, 2): east, (3, 2): None, (0, 1): north, (2, 1): north, (3, 1): None, (0, 0): north, (1, 0): west, (2, 0): west, (3, 0): west,} agent = PassiveADPAgent(policy, sequential_decision_environment) for i in range(100): run_single_trial(agent,sequential_decision_environment) agent.U[(0, 0)] > 0.2 True agent.U[(0, 1)] > 0.2 True
- class reinforcement_learning4e.PassiveTDAgent(pi, mdp, alpha=None)[source]
Bases:
object[Figure 21.4] The abstract class for a Passive (non-learning) agent that uses temporal differences to learn utility estimates. Override update_state method to convert percept to state and reward. The mdp being provided should be an instance of a subclass of the MDP Class:
import sys from mdp import sequential_decision_environment north = (0, 1) south = (0,-1) west = (-1, 0) east = (1, 0) policy = {(0, 2): east, (1, 2): east, (2, 2): east, (3, 2): None, (0, 1): north, (2, 1): north, (3, 1): None, (0, 0): north, (1, 0): west, (2, 0): west, (3, 0): west,} agent = PassiveTDAgent(policy, sequential_decision_environment, alpha=lambda n: 60./(59+n)) for i in range(200): run_single_trial(agent,sequential_decision_environment) agent.U[(0, 0)] > 0.2 True agent.U[(0, 1)] > 0.2 True
- class reinforcement_learning4e.QLearningAgent(mdp, Ne, Rplus, alpha=None)[source]
Bases:
object[Figure 21.8] An exploratory Q-learning agent. It avoids having to learn the transition model because the Q-value of a state can be related directly to those of its neighbors:
import sys from mdp import sequential_decision_environment north = (0, 1) south = (0,-1) west = (-1, 0) east = (1, 0) policy = {(0, 2): east, (1, 2): east, (2, 2): east, (3, 2): None, (0, 1): north, (2, 1): north, (3, 1): None, (0, 0): north, (1, 0): west, (2, 0): west, (3, 0): west,} q_agent = QLearningAgent(sequential_decision_environment, Ne=5, Rplus=2, alpha=lambda n: 60./(59+n)) for i in range(200): run_single_trial(q_agent,sequential_decision_environment) q_agent.Q[((0, 1), (0, 1))] >= -0.5 True q_agent.Q[((1, 0), (0, -1))] <= 0.5 True
- class reinforcement_learning4e.SARSALearningAgent(mdp, Ne, Rplus, alpha=None)[source]
Bases:
QLearningAgent[Section 22.3] An on-policy temporal-difference control agent (SARSA: State-Action-Reward- State-Action). It is identical to the Q-learning agent except for the update rule: instead of bootstrapping on the maximum Q-value over next actions, SARSA bootstraps on the Q-value of the action a1 that its exploration policy will actually take in the next state. Being on-policy, SARSA learns the value of the policy it is following, exploration included, rather than that of the greedy policy.
- reinforcement_learning4e.run_single_trial(agent_program, mdp)[source]
Execute trial for given agent_program and mdp. mdp should be an instance of subclass of mdp.MDP
Perception (Chapter 24)
- perception4e.array_normalization(array, range_min, range_max)[source]
Normalize an array in the range of (range_min, range_max)
- perception4e.gradient_edge_detector(image)[source]
Image edge detection by calculating gradients in the image :param image: numpy ndarray or an iterable object :return: numpy ndarray, representing a gray scale image
- perception4e.gaussian_derivative_edge_detector(image)[source]
Image edge detector using derivative of gaussian kernels
- perception4e.gen_gray_scale_picture(size, level=3)[source]
Generate a picture with different gray scale levels
- Parameters:
size – size of generated picture
level – the number of level of gray scales in the picture, range (0, 255) are equally divided by number of levels
:return image in numpy ndarray type
- perception4e.probability_contour_detection(image, discs, threshold=0)[source]
Detect edges/contours by applying a set of discs to an image :param image: an image in type of numpy ndarray :param discs: a set of discs/filters to apply to pixels of image :param threshold: threshold to tell whether the pixel at (x, y) is on an edge :return image showing edges in numpy ndarray type
- perception4e.group_contour_detection(image, cluster_num=2)[source]
Detecting contours in an image with k-means clustering :param image: an image in numpy ndarray type :param cluster_num: number of clusters in k-means
- perception4e.generate_edge_weight(image, v1, v2)[source]
Find edge weight between two vertices in an image :param image: image in numpy ndarray type :param v1, v2: verticles in the image in form of (x index, y index)
- class perception4e.Graph(image)[source]
Bases:
objectGraph in adjacent matrix to represent an image
- perception4e.gen_discs(init_scale, scales=1)[source]
Generate a collection of disc pairs by splitting an round discs with different angles :param init_scale: the initial size of each half discs :param scales: scale number of each type of half discs, the scale size will be doubled each time :return: the collection of generated discs: [discs of scale1, discs of scale2…]
- perception4e.simple_convnet(size=3, num_classes=10)[source]
Simple convolutional network for digit recognition :param size: number of convolution layers :param num_classes: number of output classes :return a convolution network in keras model type
- perception4e.selective_search(image)[source]
Selective search for object detection :param image: str, the path of image or image in ndarray type with 3 channels :return list of bounding boxes, each element is in form of [x_min, y_min, x_max, y_max]
- perception4e.pool_rois(feature_map, rois, pooled_height, pooled_width)[source]
Applies ROI pooling for a single image and various ROIs :param feature_map: ndarray, in shape of (width, height, channel) :param rois: list of roi :param pooled_height: height of pooled area :param pooled_width: width of pooled area :return list of pooled features
- perception4e.pool_roi(feature_map, roi, pooled_height, pooled_width)[source]
Applies a single ROI pooling to a single image :param feature_map: ndarray, in shape of (width, height, channel) :param roi: region of interest, in form of [x_min_ratio, y_min_ratio, x_max_ratio, y_max_ratio] :return feature of pooling output, in shape of (pooled_width, pooled_height)