legonet package¶
Submodules¶
legonet.activations module¶
This module contains activation functions used for layers in neural network.
-
legonet.activations.get(name)¶ Returns activation according to name.
Parameters: name – name of activation function. Returns: the activation according to name.
-
legonet.activations.identity(x)¶ Returns the input tensor without any change.
Parameters: x – Input tensor. Returns: The input tensor.
-
legonet.activations.relu(x)¶ Computes the rectified linear of x.
Parameters: x – Input tensor. Returns: A Tensor of same shape as x.
-
legonet.activations.sigmoid(x)¶ Computes the sigmoid of x.
Parameters: x – Input tensor. Returns: A Tensor of same shape as x.
-
legonet.activations.softmax(x)¶ Computes the softmax of x.
Parameters: x – Input tensor. Returns: A Tensor of same shape as x.
-
legonet.activations.tanh(x)¶ Computes the hyperbolic tangent of x.
Parameters: x – Input tensor. Returns: A Tensor of same shape as x.
legonet.initializers module¶
This module contains initializers used for initializing parameters in neural network layers.
-
legonet.initializers.get(name)¶ Returns an array filler according to name.
Parameters: name – the name of initializer. Returns: the initializer corresponding to name.
legonet.layers module¶
This module contains all kinds of layers which are used in a neural network model. These layers are all derived from an abstract base class Layer, which itself is derived from the abstract class Piece. These layers can either be added to container Piece`(including `NeuralNetwork) or be called directly in a functional style.
-
class
legonet.layers.Convolution(filter_shape, n_output_channels, activation_fn='relu', strides=None, padding='SAME', weight_init=None, bias_init=None, weight_regularizer=None, bias_regularizer=None, use_cudnn_on_gpu=True, has_bias=True, name=None, trainable=True)¶ Bases:
legonet.layers.LayerConvolution layer for 2D arrays.
Convolution layer is the core layer in Convolutional Neural Network. It performs a 2D convolution operation using a 2D filter (a.k.a convolution kernel) and adds a bias array (usually denoted as b) to the input Tensor, possibly followed by an activation function (often ReLU in latest research).
-
__call__(flow)¶ Applies this layer to the input Tensor and returns the output Tensor.
Parameters: flow – The input Tensor. Returns: Output of this layer.
-
-
class
legonet.layers.Embedding(input_shape, init_values, name=None, trainable=True)¶ Bases:
legonet.layers.LayerEmbedding layer uses the underlying mechanism provided by TensorFlow to parallel lookups on a list of tensors.
It is very useful for tasks where the input of (subsequent) model is just a list of tensors chosen from a predefined table (e.g. word vectors in NLP tasks), and is especially useful when you want to fine-tune this table during the training process by enabling error message being propagated to tensors in this table.
-
__call__(flow=None)¶ Constructs the layer in Tensorflow graph.
Parameters: flow – This argument is ignored. (Default value = None) Returns: Output of this layer.
-
-
class
legonet.layers.FullyConnected(n_output_units, activation_fn=None, weight_init=None, bias_init=None, weight_regularizer=None, bias_regularizer=None, has_bias=True, name=None, trainable=True)¶ Bases:
legonet.layers.LayerA simple fully connected layer.
Fully connected layer (a.k.a. dense layer) performs an affine transformation to the input Tensor using a weight matrix (usually denoted as W) and a bias vector (usually denoted as b), possibly followed by an activation function, which is necessary except that FullyConnected is used as the output layer (i.e. the last layer in the model).
-
__call__(flow)¶ Applies this FullyConnected to the input Tensor and returns the output Tensor.
Parameters: flow – The input Tensor. Returns: Output of this layer.
-
-
class
legonet.layers.Input(input_shape, input_dtype=tf.float32, name=None)¶ Bases:
legonet.layers.LayerInput layer takes the input Tensor and pass it to the subsequent layers.
This layer performs no transformation to the input and is used only for the purpose of placeholder.
-
__call__(flow=None)¶ Constructs the layer in Tensorflow graph.
Parameters: flow – This argument is ignored. (Default value = None) Returns: Output of this layer.
-
-
class
legonet.layers.Layer(name=None, trainable=True)¶ Bases:
legonet.pieces.PieceAbstract base class for all kinds of layers.
This class is an abstract base class and is intended to be used only as an interface for its derived classes. So, do not directly use it in neural network.
-
trainable¶ Indicates whether the parameters of this layer will be updated during training.
-
params¶ List of all parameters in this Layer. The list will be empty before the Layer being called for the
-
first time.
-
__call__(flow)¶ Constructs the Layer in Tensorflow graph.
This method is intended to be implemented in derived classes.
Parameters: flow – The input Tensor. Returns: Output of this layer.
-
-
class
legonet.layers.Pooling(pool_shape=None, strides=None, mode='max', padding='VALID', name=None)¶ Bases:
legonet.layers.LayerPooling layer for 2D arrays.
Pooling layer is one of the most important components in a convolutional neural network. It is used for reducing the size of the input tensor, so as to lower the argument number and to reduce redundant information. The process is to generate one scalar from every fix-sized area (called pool) in the input Tensor and combine all scalars to a new Tensor as the output of pooling layer. There are two choices for the pooling strategy: max pooling and average pooling.
-
__call__(flow)¶ Applies pooling layer to the input Tensor and returns the output Tensor.
Parameters: flow – The input Tensor. Returns: Output of this layer.
-
legonet.models module¶
This module contains different models, which are the interface for training and using neural networks.
-
class
legonet.models.NeuralNetwork(optimizer, log_dir=None, output_fn='softmax', loss_fn='sparse_softmax_cross_entropy', target_dtype=tf.int64, base_plate=None, graph=None, session=None)¶ Bases:
objectThe basic neural network model.
-
__del__()¶ Destructor of NeuralNetwork.
-
add(piece)¶ Adds a Piece to the base plate of this NeuralNetwork.
Parameters: piece – a Piece instance. Returns: None
-
build()¶ Constructs the whole neural network in TensorFlow graph.
-
fit(x, y, n_epochs=5, batch_size=32, checkpoint_dir=None, randomized=True, freq_log=100, freq_checkpoint=10000, loss_decay=0.0)¶ Trains this model using x and y.
Parameters: - x – Input array.
- y – Targets array, should be consistent with target_dtype.
- n_epochs – Number of epochs to iterate. (Default value = 5)
- batch_size – Size of mini-batch. (Default value = 32)
- checkpoint_dir – Path to the folder to store checkpoint files. (Default value = None)
- randomized – Indicates whether mini-batches will be selected randomly. (Default value = True)
- freq_log – The frequency of logging. (Default value = 100)
- freq_checkpoint – The frequency of saving parameters to checkpoint files. (Default value = 10000)
- loss_decay – The decay rate used for displaying exponential moving average of loss. (Default value = 0.0)
Returns: None
-
load_checkpoint(path=None)¶ Loads checkpoint from a file or directory.
Parameters: path – Path of folder containing checkpoint files. (Default value = None) Returns: None
-
predict(x)¶ Outputs result given input.
Parameters: x – Input array. Returns: Predicted result.
-
legonet.objectives module¶
This module contains various kinds of objective functions used as loss functions for neural networks.
-
legonet.objectives.get(name)¶ Returns the objective function according to name.
Parameters: name – The name of the loss function. Returns: The loss function corresponding to name.
-
legonet.objectives.mean_square(output, targets)¶ Mean square error.
Parameters: - output – The output Tensor of model.
- targets – A Tensor of the same size as output.
Returns: A scalar indicating the mean-square error.
-
legonet.objectives.sigmoid_cross_entropy(logits, targets)¶ Cross-entropy error for sigmoid output layer.
Parameters: - logits – The Tensor passed to logistic function.
- targets – A Tensor of the same shape as logits. Must be either a value in the range [0, 1] or -1. If -1, the
- loss term will not be added to the result. (corresponding) –
Returns: A scalar indicating the summed cross-entropy error of all input samples with target not being -1.
-
legonet.objectives.softmax_cross_entropy(logits, targets)¶ Cross-entropy error for softmax output layer.
Parameters: - logits – Unscaled log probabilities.
- targets – Each row must be either a valid probability distribution or all zeros. If all zeros, the
- loss term will not be added to the result. (corresponding) –
Returns: A scalar indicating the cross-entropy error.
-
legonet.objectives.sparse_softmax_cross_entropy(logits, labels)¶ Cross-entropy error for softmax output layer with one-hot target.
Parameters: - logits – Unscaled log probabilities.
- labels – Each element must be a index within range [0, n_classes - 1] or -1. If -1, the corresponding loss term
- not be added to the result. (will) –
Returns: A scalar indicating the cross-entropy error.
legonet.optimizers module¶
This module contains various kinds of optimizers used for updating parameters in neural networks.
legonet.regularizers module¶
This module contains various kinds of regularizers which can be applied to layers in neural networks as a strategy for preventing overfit.