noxer.nn module
This script contains a number of interfaces and implementations for Supervised learning with PyTorch.
""" This script contains a number of interfaces and implementations for Supervised learning with PyTorch. """ import math from abc import abstractmethod import numpy as np from sklearn.base import ClassifierMixin, RegressorMixin, BaseEstimator, TransformerMixin from sklearn.preprocessing import LabelEncoder from sklearn.utils import check_X_y import torch import torch.utils.data from torch.autograd import Variable import torch.nn as nn import torch.optim as optim class PTLBase(BaseEstimator): """ A base class for learning algorithms with pytorch. Parameters ---------- epochs: int > 0 Number of epochs to train neural network for. batch_size: int > 0 Size of subsample of dataset to use to approximate the gradient in stochatic gradient descent procedure. alpha: float > 0 Learning rate. Tunes the amount of update done after processing of single batch size. beta1: float 0.0 < x < 1.0 Beta 1 parameter of Adam stochastic gradient descent algorithm. beta2: float 0.0 < x < 1.0 Beta 2 parameter of Adam stochastic gradient descent algorithm. """ def __init__(self, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999): self.epochs = epochs self.batch_size = batch_size self.alpha = alpha self.beta1 = beta1 self.beta2 = beta2 self.net = None @abstractmethod def make_architecture(self, X, y): """ Should return nn.Module instance, which represents architecture of the neural network. Parameters ---------- X: iterable of size n_samples Representation of dataset. y: iterable of size n_samples Representation of output Return ------ net: an instance of a neural network to be trained. """ pass def fit(self, X, y, criterion): """ Trains a neural network on provided data. Parameters ---------- X: iterable of size n_samples Representation of dataset. y: iterable of size n_samples Representation of output criterion: callable with 2 arguments, possibly a nn._Loss instance. Cost function to minimize. Return ------ self """ check_X_y(X, y, allow_nd=True, dtype=None) self.net = self.make_architecture(X, y) optimizer = optim.Adam(self.net.parameters(), lr=self.alpha, betas=(self.beta1, self.beta2)) data = torch.utils.data.TensorDataset( torch.FloatTensor(X), torch.LongTensor(y) ) # this creates mixed batches trainloader = torch.utils.data.DataLoader( data, batch_size=self.batch_size, shuffle=True ) for epoch in range(self.epochs): # loop over the dataset multiple times for i, data in enumerate(trainloader, 0): # get the inputs inputs, labels = data # wrap them in Variable inputs, labels = Variable(inputs), Variable(labels) # zero the parameter gradients optimizer.zero_grad() # forward + backward + optimize outputs = self.net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() return self def predict(self, X): """ Make estimation with trained neural network. Parameters ---------- X: iterable of size n_samples Representation of inputs. Should be consistent with inputs in the training dataset. Return ------ X: iterable of size n_samples Representation of estimated outputs. """ if self.net is None: raise RuntimeError("The model is not fit. Did you forget to call the fit method on a dataset?") X = Variable(torch.FloatTensor(X), volatile=True) yp = self.net(X).data.numpy() return yp class PTLClassifierBase(PTLBase, ClassifierMixin): """ A base class for learning classifiers with pytorch. Parameters ---------- See parent classes for corresponding parameters. """ def __init__(self, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999): super(PTLClassifierBase, self).__init__( epochs, batch_size, alpha, beta1, beta2 ) self.label_encoder = None def fit(self, X, y): """ Trains a classifier on provided data. Parameters ---------- X: iterable of size n_samples Representation of dataset. y: iterable of size n_samples Representation of classes Return ------ self """ # encode outputs self.label_encoder = LabelEncoder() y = self.label_encoder.fit_transform(y) criterion = nn.CrossEntropyLoss() super(PTLClassifierBase, self).fit(X, y, criterion) return self def predict(self, X): """ Estimate output classes. Parameters ---------- X: iterable of size n_samples Representation of inputs to classify. Return ------ y: iterable of size n_samples Representation of classes """ yp = super(PTLClassifierBase, self).predict(X) yp = np.argmax(yp, axis=1) yp = self.label_encoder.inverse_transform(yp) return yp class FFNNClassificationNN(nn.Module): """ Simple fully connected feed forward NN. Parameters ---------- xsz: int > 0 Size of input vector ysz: int > 0 Size of output vector n_layers: int > 0 Number of layers in the neural network n_neurons: int > 0 Number of neurons in every layer """ def __init__(self, xsz, ysz, n_neurons, n_layers, dropout=None): super(FFNNClassificationNN, self).__init__() hsz = int(xsz) ysz = int(ysz) n_neurons = int(n_neurons) n_layers = int(n_layers) if dropout is not None: dropout = float(dropout) layers = [] for i in range(n_layers): layers.append(nn.Linear(hsz, n_neurons)) layers.append(nn.LeakyReLU()) if dropout is not None: if dropout > 0.03: layers.append(nn.Dropout(p=dropout)) hsz = n_neurons layers.append(nn.Linear(hsz, ysz)) layers.append(nn.Softmax(dim=-1)) self.fc = nn.ModuleList(layers) def forward(self, x): for l in self.fc: x = l(x) return x class MLPClassifier(PTLClassifierBase): """ Estimator with Feed Forward Neural Network. Parameters ---------- For any parameters not listed, see PTLClassifierBase. n_layers: int > 0 Number of layers in the NN n_neurons: int > 0 Number of neurons in every layer """ def __init__(self, dropout=None, n_layers=1, n_neurons=32, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999): super(MLPClassifier, self).__init__( epochs, batch_size, alpha, beta1, beta2 ) self.n_neurons = n_neurons self.n_layers = n_layers self.dropout = dropout def make_architecture(self, X, y): """ See PTLBase.make_architecture for explanations. """ net = FFNNClassificationNN( X.shape[-1], len(set(y)), self.n_neurons, self.n_layers, dropout=self.dropout ) return net class CNN1DClassificationNN(nn.Module): """ Simple fully connected feed forward NN. Parameters ---------- xsz: int > 0 Size of input vector ysz: int > 0 Size of output vector n_layers: int > 0 Number of layers in the neural network n_neurons: int > 0 Number of neurons in every layer """ def __init__(self, xsz, ysz, n_neurons=64, n_layers=1, kernel_size=3, dropout=None): super(CNN1DClassificationNN, self).__init__() ssz = int(xsz[0]) hsz = int(xsz[1]) ysz = int(ysz) n_neurons = int(n_neurons) n_layers = int(n_layers) kernel_size = int(kernel_size) if dropout is not None: dropout = float(dropout) layers = [] for i in range(n_layers): layers.append(nn.Conv1d(hsz, n_neurons, kernel_size=kernel_size, padding=1)) layers.append(nn.LeakyReLU()) if dropout is not None: if dropout > 0.03: layers.append(nn.Dropout(p=dropout)) hsz = n_neurons # here avoid empty sequence essz = ssz / 2.0 if essz < 1.0: break layers.append(nn.MaxPool1d(2, ceil_mode=True)) ssz = math.ceil(essz) self.seq = nn.ModuleList(layers) # calculate flatten hsz = hsz * ssz self.ffnn = FFNNClassificationNN(hsz, ysz, n_neurons=n_neurons, n_layers=1, dropout=dropout) def forward(self, x): # reshape input to (batch size, channels, seq length) x = x.transpose(1, 2) for l in self.seq: x = l(x) # flatten the data x = x.view(x.size(0), -1) x = self.ffnn(x) return x class CNN1DClassifier(PTLClassifierBase): """ Estimator with one dimensional convolutional neural network. Parameters ---------- For any parameters not listed, see PTLClassifierBase. n_layers: int > 0 Number of layers in the NN n_neurons: int > 0 Number of neurons in every layer """ def __init__(self, kernel_size=3, dropout=None, n_layers=1, n_neurons=32, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999): super(CNN1DClassifier, self).__init__( epochs, batch_size, alpha, beta1, beta2 ) self.n_neurons = n_neurons self.n_layers = n_layers self.kernel_size = kernel_size self.dropout = dropout def make_architecture(self, X, y): """ See PTLBase.make_architecture for explanations. """ net = CNN1DClassificationNN( X.shape[1:], len(set(y)), self.n_neurons, self.n_layers, self.kernel_size, dropout=self.dropout ) return net class GRUClassification(nn.Module): """ Recurent neural network module. Maps sequence to vector output. Parameters ---------- xsz: int > 0 Size of input vector ysz: int > 0 Size of output vector n_layers: int > 0 Number of layers in the neural network n_neurons: int > 0 Number of neurons in every layer """ def __init__(self, xsz, ysz, n_neurons=64, n_layers=1, dropout=None): super(GRUClassification, self).__init__() ssz = int(xsz[0]) hsz = int(xsz[1]) ysz = int(ysz) n_neurons = int(n_neurons) n_layers = int(n_layers) if dropout is not None: dropout = float(dropout) else: dropout = 0.0 self.rnn = nn.GRU(hsz, n_neurons, n_layers, dropout=dropout) # calculate flatten hsz = n_neurons self.ffnn = FFNNClassificationNN(hsz, ysz, n_neurons=n_neurons, n_layers=1) def forward(self, x): # swap to (seq_len, batch, input_size) x = x.transpose(0, 1) _, x = self.rnn(x) # flatten the data x = x[0, :, :] x = self.ffnn(x) return x class GRUClassifier(PTLClassifierBase): """ Estimator with one dimensional convolutional neural network. Parameters ---------- For any parameters not listed, see PTLClassifierBase. n_layers: int > 0 Number of layers in the NN n_neurons: int > 0 Number of neurons in every layer """ def __init__(self, n_layers=1, n_neurons=32, dropout=None, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999): super(GRUClassifier, self).__init__( epochs, batch_size, alpha, beta1, beta2 ) self.n_neurons = n_neurons self.n_layers = n_layers self.dropout = dropout def make_architecture(self, X, y): """ See PTLBase.make_architecture for explanations. """ net = GRUClassification( X.shape[1:], len(set(y)), self.n_neurons, self.n_layers, dropout=self.dropout ) return net def test_dnn_v_dnn(datafnc): from sklearn.neural_network import MLPClassifier from sklearn.model_selection import train_test_split from sklearn.model_selection import GridSearchCV from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from searchgrid import set_grid, build_param_grid X, y = datafnc() X_train, X_test, y_train, y_test = train_test_split(X, y) estimator = make_pipeline( StandardScaler(), set_grid( PMLPClassifier(), epochs=[2 ** i for i in range(1, 8)], n_layers=list(range(1, 4)), n_neurons=[2 ** i for i in range(1, 8)], alpha=[1e-4, 1e-3, 1e-2] ) ) model = GridSearchCV( estimator=estimator, param_grid=build_param_grid(estimator), verbose=1000, cv=3, n_jobs=2 ) mlp = GridSearchCV( estimator=make_pipeline( StandardScaler(), MLPClassifier(), ), param_grid={ 'mlpclassifier__max_iter': [2 ** i for i in range(1, 8)] }, verbose=1000, cv=3 ) model.fit(X_train, y_train) mlp.fit(X_train, y_train) print(datafnc.__name__) print(model.score(X_test, y_test)) print(mlp.score(X_test, y_test)) def test_rnn(datafnc): from sklearn.neural_network import MLPClassifier from sklearn.model_selection import train_test_split from sklearn.model_selection import GridSearchCV from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from searchgrid import set_grid, build_param_grid from noxer.sequences import PadSubsequence X, y = datafnc() X_train, X_test, y_train, y_test = train_test_split(X, y) estimator = make_pipeline( set_grid( GRUClassifier(n_neurons=64, n_layers=1, epochs=100), alpha=[1e-4, 1e-3, 1e-2] ) ) estimator = make_pipeline( set_grid( CNN1DClassifier(epochs=64), alpha=[0.01], n_layers=[1], n_neurons=[32], dropout=[0.2, 0.3, 0.4] ) ) model = GridSearchCV( estimator=estimator, param_grid=build_param_grid(estimator), verbose=1000, cv=3, n_jobs=1 ) model.fit(X_train, y_train) print(datafnc.__name__) print(model.score(X_test, y_test)) if __name__ == '__main__': import numpy as np from sklearn.datasets import load_digits def rnd_data(): X = np.random.randn(2048, 30 * 20) y = X[:, 0] > 0.0 return X, y def rnn_data(): X = np.random.randn(2500, 30, 60) y = X[:, 0, 0] > 0.0 return X, y #test_dnn_v_dnn(rnd_data) #test_dnn_v_dnn(lambda : load_digits(return_X_y=True)) test_rnn(rnn_data)
Functions
def test_dnn_v_dnn(
datafnc)
def test_dnn_v_dnn(datafnc): from sklearn.neural_network import MLPClassifier from sklearn.model_selection import train_test_split from sklearn.model_selection import GridSearchCV from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from searchgrid import set_grid, build_param_grid X, y = datafnc() X_train, X_test, y_train, y_test = train_test_split(X, y) estimator = make_pipeline( StandardScaler(), set_grid( PMLPClassifier(), epochs=[2 ** i for i in range(1, 8)], n_layers=list(range(1, 4)), n_neurons=[2 ** i for i in range(1, 8)], alpha=[1e-4, 1e-3, 1e-2] ) ) model = GridSearchCV( estimator=estimator, param_grid=build_param_grid(estimator), verbose=1000, cv=3, n_jobs=2 ) mlp = GridSearchCV( estimator=make_pipeline( StandardScaler(), MLPClassifier(), ), param_grid={ 'mlpclassifier__max_iter': [2 ** i for i in range(1, 8)] }, verbose=1000, cv=3 ) model.fit(X_train, y_train) mlp.fit(X_train, y_train) print(datafnc.__name__) print(model.score(X_test, y_test)) print(mlp.score(X_test, y_test))
def test_rnn(
datafnc)
def test_rnn(datafnc): from sklearn.neural_network import MLPClassifier from sklearn.model_selection import train_test_split from sklearn.model_selection import GridSearchCV from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from searchgrid import set_grid, build_param_grid from noxer.sequences import PadSubsequence X, y = datafnc() X_train, X_test, y_train, y_test = train_test_split(X, y) estimator = make_pipeline( set_grid( GRUClassifier(n_neurons=64, n_layers=1, epochs=100), alpha=[1e-4, 1e-3, 1e-2] ) ) estimator = make_pipeline( set_grid( CNN1DClassifier(epochs=64), alpha=[0.01], n_layers=[1], n_neurons=[32], dropout=[0.2, 0.3, 0.4] ) ) model = GridSearchCV( estimator=estimator, param_grid=build_param_grid(estimator), verbose=1000, cv=3, n_jobs=1 ) model.fit(X_train, y_train) print(datafnc.__name__) print(model.score(X_test, y_test))
Classes
class CNN1DClassificationNN
Simple fully connected feed forward NN.
Parameters
xsz: int > 0 Size of input vector
ysz: int > 0 Size of output vector
n_layers: int > 0 Number of layers in the neural network
n_neurons: int > 0 Number of neurons in every layer
class CNN1DClassificationNN(nn.Module): """ Simple fully connected feed forward NN. Parameters ---------- xsz: int > 0 Size of input vector ysz: int > 0 Size of output vector n_layers: int > 0 Number of layers in the neural network n_neurons: int > 0 Number of neurons in every layer """ def __init__(self, xsz, ysz, n_neurons=64, n_layers=1, kernel_size=3, dropout=None): super(CNN1DClassificationNN, self).__init__() ssz = int(xsz[0]) hsz = int(xsz[1]) ysz = int(ysz) n_neurons = int(n_neurons) n_layers = int(n_layers) kernel_size = int(kernel_size) if dropout is not None: dropout = float(dropout) layers = [] for i in range(n_layers): layers.append(nn.Conv1d(hsz, n_neurons, kernel_size=kernel_size, padding=1)) layers.append(nn.LeakyReLU()) if dropout is not None: if dropout > 0.03: layers.append(nn.Dropout(p=dropout)) hsz = n_neurons # here avoid empty sequence essz = ssz / 2.0 if essz < 1.0: break layers.append(nn.MaxPool1d(2, ceil_mode=True)) ssz = math.ceil(essz) self.seq = nn.ModuleList(layers) # calculate flatten hsz = hsz * ssz self.ffnn = FFNNClassificationNN(hsz, ysz, n_neurons=n_neurons, n_layers=1, dropout=dropout) def forward(self, x): # reshape input to (batch size, channels, seq length) x = x.transpose(1, 2) for l in self.seq: x = l(x) # flatten the data x = x.view(x.size(0), -1) x = self.ffnn(x) return x
Ancestors (in MRO)
- CNN1DClassificationNN
- torch.nn.modules.module.Module
- builtins.object
Class variables
var dump_patches
Static methods
def __init__(
self, xsz, ysz, n_neurons=64, n_layers=1, kernel_size=3, dropout=None)
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, xsz, ysz, n_neurons=64, n_layers=1, kernel_size=3, dropout=None): super(CNN1DClassificationNN, self).__init__() ssz = int(xsz[0]) hsz = int(xsz[1]) ysz = int(ysz) n_neurons = int(n_neurons) n_layers = int(n_layers) kernel_size = int(kernel_size) if dropout is not None: dropout = float(dropout) layers = [] for i in range(n_layers): layers.append(nn.Conv1d(hsz, n_neurons, kernel_size=kernel_size, padding=1)) layers.append(nn.LeakyReLU()) if dropout is not None: if dropout > 0.03: layers.append(nn.Dropout(p=dropout)) hsz = n_neurons # here avoid empty sequence essz = ssz / 2.0 if essz < 1.0: break layers.append(nn.MaxPool1d(2, ceil_mode=True)) ssz = math.ceil(essz) self.seq = nn.ModuleList(layers) # calculate flatten hsz = hsz * ssz self.ffnn = FFNNClassificationNN(hsz, ysz, n_neurons=n_neurons, n_layers=1, dropout=dropout)
def add_module(
self, name, module)
Adds a child module to the current module.
The module can be accessed as an attribute using the given name.
Args: name (string): name of the child module. The child module can be accessed from this module using the given name parameter (Module): child module to be added to the module.
def add_module(self, name, module): """Adds a child module to the current module. The module can be accessed as an attribute using the given name. Args: name (string): name of the child module. The child module can be accessed from this module using the given name parameter (Module): child module to be added to the module. """ if not isinstance(module, Module) and module is not None: raise TypeError("{} is not a Module subclass".format( torch.typename(module))) if hasattr(self, name) and name not in self._modules: raise KeyError("attribute '{}' already exists".format(name)) self._modules[name] = module
def apply(
self, fn)
Applies fn
recursively to every submodule (as returned by .children()
)
as well as self. Typical use includes initializing the parameters of a model
(see also :ref:torch-nn-init
).
Args:
fn (:class:Module
-> None): function to be applied to each submodule
Returns: Module: self
Example: >>> def init_weights(m): >>> print(m) >>> if type(m) == nn.Linear: >>> m.weight.data.fill_(1.0) >>> print(m.weight) >>> >>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2)) >>> net.apply(init_weights) Linear (2 -> 2) Parameter containing: 1 1 1 1 [torch.FloatTensor of size 2x2] Linear (2 -> 2) Parameter containing: 1 1 1 1 [torch.FloatTensor of size 2x2] Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) )
def apply(self, fn): """Applies ``fn`` recursively to every submodule (as returned by ``.children()``) as well as self. Typical use includes initializing the parameters of a model (see also :ref:`torch-nn-init`). Args: fn (:class:`Module` -> None): function to be applied to each submodule Returns: Module: self Example: >>> def init_weights(m): >>> print(m) >>> if type(m) == nn.Linear: >>> m.weight.data.fill_(1.0) >>> print(m.weight) >>> >>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2)) >>> net.apply(init_weights) Linear (2 -> 2) Parameter containing: 1 1 1 1 [torch.FloatTensor of size 2x2] Linear (2 -> 2) Parameter containing: 1 1 1 1 [torch.FloatTensor of size 2x2] Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) ) """ for module in self.children(): module.apply(fn) fn(self) return self
def children(
self)
Returns an iterator over immediate children modules.
Yields: Module: a child module
def children(self): """Returns an iterator over immediate children modules. Yields: Module: a child module """ for name, module in self.named_children(): yield module
def cpu(
self)
Moves all model parameters and buffers to the CPU.
Returns: Module: self
def cpu(self): """Moves all model parameters and buffers to the CPU. Returns: Module: self """ return self._apply(lambda t: t.cpu())
def cuda(
self, device=None)
Moves all model parameters and buffers to the GPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing optimizer if the module will live on GPU while being optimized.
Arguments: device (int, optional): if specified, all parameters will be copied to that device
Returns: Module: self
def cuda(self, device=None): """Moves all model parameters and buffers to the GPU. This also makes associated parameters and buffers different objects. So it should be called before constructing optimizer if the module will live on GPU while being optimized. Arguments: device (int, optional): if specified, all parameters will be copied to that device Returns: Module: self """ return self._apply(lambda t: t.cuda(device))
def double(
self)
Casts all parameters and buffers to double datatype.
Returns: Module: self
def double(self): """Casts all parameters and buffers to double datatype. Returns: Module: self """ return self._apply(lambda t: t.double())
def eval(
self)
Sets the module in evaluation mode.
This has any effect only on modules such as Dropout or BatchNorm.
def eval(self): """Sets the module in evaluation mode. This has any effect only on modules such as Dropout or BatchNorm. """ return self.train(False)
def float(
self)
Casts all parameters and buffers to float datatype.
Returns: Module: self
def float(self): """Casts all parameters and buffers to float datatype. Returns: Module: self """ return self._apply(lambda t: t.float())
def forward(
self, x)
Defines the computation performed at every call.
Should be overriden by all subclasses.
.. note::
Although the recipe for forward pass needs to be defined within
this function, one should call the :class:Module
instance afterwards
instead of this since the former takes care of running the
registered hooks while the latter silently ignores them.
def forward(self, x): # reshape input to (batch size, channels, seq length) x = x.transpose(1, 2) for l in self.seq: x = l(x) # flatten the data x = x.view(x.size(0), -1) x = self.ffnn(x) return x
def half(
self)
Casts all parameters and buffers to half datatype.
Returns: Module: self
def half(self): """Casts all parameters and buffers to half datatype. Returns: Module: self """ return self._apply(lambda t: t.half())
def load_state_dict(
self, state_dict, strict=True)
Copies parameters and buffers from :attr:state_dict
into
this module and its descendants. If :attr:strict
is True
then
the keys of :attr:state_dict
must exactly match the keys returned
by this module's :func:state_dict()
function.
Arguments:
state_dict (dict): A dict containing parameters and
persistent buffers.
strict (bool): Strictly enforce that the keys in :attr:state_dict
match the keys returned by this module's :func:
state_dict()`
function.
def load_state_dict(self, state_dict, strict=True): """Copies parameters and buffers from :attr:`state_dict` into this module and its descendants. If :attr:`strict` is ``True`` then the keys of :attr:`state_dict` must exactly match the keys returned by this module's :func:`state_dict()` function. Arguments: state_dict (dict): A dict containing parameters and persistent buffers. strict (bool): Strictly enforce that the keys in :attr:`state_dict` match the keys returned by this module's `:func:`state_dict()` function. """ own_state = self.state_dict() for name, param in state_dict.items(): if name in own_state: if isinstance(param, Parameter): # backwards compatibility for serialized parameters param = param.data try: own_state[name].copy_(param) except Exception: raise RuntimeError('While copying the parameter named {}, ' 'whose dimensions in the model are {} and ' 'whose dimensions in the checkpoint are {}.' .format(name, own_state[name].size(), param.size())) elif strict: raise KeyError('unexpected key "{}" in state_dict' .format(name)) if strict: missing = set(own_state.keys()) - set(state_dict.keys()) if len(missing) > 0: raise KeyError('missing keys in state_dict: "{}"'.format(missing))
def modules(
self)
Returns an iterator over all modules in the network.
Yields: Module: a module in the network
Note:
Duplicate modules are returned only once. In the following
example, l
will be returned only once.
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.modules()): >>> print(idx, '->', m) 0 -> Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) ) 1 -> Linear (2 -> 2)
def modules(self): """Returns an iterator over all modules in the network. Yields: Module: a module in the network Note: Duplicate modules are returned only once. In the following example, ``l`` will be returned only once. >>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.modules()): >>> print(idx, '->', m) 0 -> Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) ) 1 -> Linear (2 -> 2) """ for name, module in self.named_modules(): yield module
def named_children(
self)
Returns an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
Yields: (string, Module): Tuple containing a name and child module
Example: >>> for name, module in model.named_children(): >>> if name in ['conv4', 'conv5']: >>> print(module)
def named_children(self): """Returns an iterator over immediate children modules, yielding both the name of the module as well as the module itself. Yields: (string, Module): Tuple containing a name and child module Example: >>> for name, module in model.named_children(): >>> if name in ['conv4', 'conv5']: >>> print(module) """ memo = set() for name, module in self._modules.items(): if module is not None and module not in memo: memo.add(module) yield name, module
def named_modules(
self, memo=None, prefix='')
Returns an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
Yields: (string, Module): Tuple of name and module
Note:
Duplicate modules are returned only once. In the following
example, l
will be returned only once.
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.named_modules()): >>> print(idx, '->', m) 0 -> ('', Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) )) 1 -> ('0', Linear (2 -> 2))
def named_modules(self, memo=None, prefix=''): """Returns an iterator over all modules in the network, yielding both the name of the module as well as the module itself. Yields: (string, Module): Tuple of name and module Note: Duplicate modules are returned only once. In the following example, ``l`` will be returned only once. >>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.named_modules()): >>> print(idx, '->', m) 0 -> ('', Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) )) 1 -> ('0', Linear (2 -> 2)) """ if memo is None: memo = set() if self not in memo: memo.add(self) yield prefix, self for name, module in self._modules.items(): if module is None: continue submodule_prefix = prefix + ('.' if prefix else '') + name for m in module.named_modules(memo, submodule_prefix): yield m
def named_parameters(
self, memo=None, prefix='')
Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself
Yields: (string, Parameter): Tuple containing the name and parameter
Example: >>> for name, param in self.named_parameters(): >>> if name in ['bias']: >>> print(param.size())
def named_parameters(self, memo=None, prefix=''): """Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself Yields: (string, Parameter): Tuple containing the name and parameter Example: >>> for name, param in self.named_parameters(): >>> if name in ['bias']: >>> print(param.size()) """ if memo is None: memo = set() for name, p in self._parameters.items(): if p is not None and p not in memo: memo.add(p) yield prefix + ('.' if prefix else '') + name, p for mname, module in self.named_children(): submodule_prefix = prefix + ('.' if prefix else '') + mname for name, p in module.named_parameters(memo, submodule_prefix): yield name, p
def parameters(
self)
Returns an iterator over module parameters.
This is typically passed to an optimizer.
Yields: Parameter: module parameter
Example:
>>> for param in model.parameters():
>>> print(type(param.data), param.size())
def parameters(self): """Returns an iterator over module parameters. This is typically passed to an optimizer. Yields: Parameter: module parameter Example: >>> for param in model.parameters(): >>> print(type(param.data), param.size()) <class 'torch.FloatTensor'> (20L,) <class 'torch.FloatTensor'> (20L, 1L, 5L, 5L) """ for name, param in self.named_parameters(): yield param
def register_backward_hook(
self, hook)
Registers a backward hook on the module.
The hook will be called every time the gradients with respect to module inputs are computed. The hook should have the following signature::
hook(module, grad_input, grad_output) -> Tensor or None
The :attr:grad_input
and :attr:grad_output
may be tuples if the
module has multiple inputs or outputs. The hook should not modify its
arguments, but it can optionally return a new gradient with respect to
input that will be used in place of :attr:grad_input
in subsequent
computations.
Returns:
:class:torch.utils.hooks.RemovableHandle
:
a handle that can be used to remove the added hook by calling
handle.remove()
def register_backward_hook(self, hook): """Registers a backward hook on the module. The hook will be called every time the gradients with respect to module inputs are computed. The hook should have the following signature:: hook(module, grad_input, grad_output) -> Tensor or None The :attr:`grad_input` and :attr:`grad_output` may be tuples if the module has multiple inputs or outputs. The hook should not modify its arguments, but it can optionally return a new gradient with respect to input that will be used in place of :attr:`grad_input` in subsequent computations. Returns: :class:`torch.utils.hooks.RemovableHandle`: a handle that can be used to remove the added hook by calling ``handle.remove()`` """ handle = hooks.RemovableHandle(self._backward_hooks) self._backward_hooks[handle.id] = hook return handle
def register_buffer(
self, name, tensor)
Adds a persistent buffer to the module.
This is typically used to register a buffer that should not to be
considered a model parameter. For example, BatchNorm's running_mean
is not a parameter, but is part of the persistent state.
Buffers can be accessed as attributes using given names.
Args: name (string): name of the buffer. The buffer can be accessed from this module using the given name tensor (Tensor): buffer to be registered.
Example: >>> self.register_buffer('running_mean', torch.zeros(num_features))
def register_buffer(self, name, tensor): """Adds a persistent buffer to the module. This is typically used to register a buffer that should not to be considered a model parameter. For example, BatchNorm's ``running_mean`` is not a parameter, but is part of the persistent state. Buffers can be accessed as attributes using given names. Args: name (string): name of the buffer. The buffer can be accessed from this module using the given name tensor (Tensor): buffer to be registered. Example: >>> self.register_buffer('running_mean', torch.zeros(num_features)) """ if hasattr(self, name) and name not in self._buffers: raise KeyError("attribute '{}' already exists".format(name)) self._buffers[name] = tensor
def register_forward_hook(
self, hook)
Registers a forward hook on the module.
The hook will be called every time after :func:forward
has computed an output.
It should have the following signature::
hook(module, input, output) -> None
The hook should not modify the input or output.
Returns:
:class:torch.utils.hooks.RemovableHandle
:
a handle that can be used to remove the added hook by calling
handle.remove()
def register_forward_hook(self, hook): r"""Registers a forward hook on the module. The hook will be called every time after :func:`forward` has computed an output. It should have the following signature:: hook(module, input, output) -> None The hook should not modify the input or output. Returns: :class:`torch.utils.hooks.RemovableHandle`: a handle that can be used to remove the added hook by calling ``handle.remove()`` """ handle = hooks.RemovableHandle(self._forward_hooks) self._forward_hooks[handle.id] = hook return handle
def register_forward_pre_hook(
self, hook)
Registers a forward pre-hook on the module.
The hook will be called every time before :func:forward
is invoked.
It should have the following signature::
hook(module, input) -> None
The hook should not modify the input.
Returns:
:class:torch.utils.hooks.RemovableHandle
:
a handle that can be used to remove the added hook by calling
handle.remove()
def register_forward_pre_hook(self, hook): """Registers a forward pre-hook on the module. The hook will be called every time before :func:`forward` is invoked. It should have the following signature:: hook(module, input) -> None The hook should not modify the input. Returns: :class:`torch.utils.hooks.RemovableHandle`: a handle that can be used to remove the added hook by calling ``handle.remove()`` """ handle = hooks.RemovableHandle(self._forward_pre_hooks) self._forward_pre_hooks[handle.id] = hook return handle
def register_parameter(
self, name, param)
Adds a parameter to the module.
The parameter can be accessed as an attribute using given name.
Args: name (string): name of the parameter. The parameter can be accessed from this module using the given name parameter (Parameter): parameter to be added to the module.
def register_parameter(self, name, param): """Adds a parameter to the module. The parameter can be accessed as an attribute using given name. Args: name (string): name of the parameter. The parameter can be accessed from this module using the given name parameter (Parameter): parameter to be added to the module. """ if '_parameters' not in self.__dict__: raise AttributeError( "cannot assign parameter before Module.__init__() call") if hasattr(self, name) and name not in self._parameters: raise KeyError("attribute '{}' already exists".format(name)) if param is None: self._parameters[name] = None elif not isinstance(param, Parameter): raise TypeError("cannot assign '{}' object to parameter '{}' " "(torch.nn.Parameter or None required)" .format(torch.typename(param), name)) elif param.grad_fn: raise ValueError( "Cannot assign non-leaf Variable to parameter '{0}'. Model " "parameters must be created explicitly. To express '{0}' " "as a function of another variable, compute the value in " "the forward() method.".format(name)) else: self._parameters[name] = param
def state_dict(
self, destination=None, prefix='', keep_vars=False)
Returns a dictionary containing a whole state of the module.
Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names.
When keep_vars is True
, it returns a Variable for each parameter
(rather than a Tensor).
Args:
destination (dict, optional):
if not None, the return dictionary is stored into destination.
Default: None
prefix (string, optional): Adds a prefix to the key (name) of every
parameter and buffer in the result dictionary. Default: ''
keep_vars (bool, optional): if True
, returns a Variable for each
parameter. If False
, returns a Tensor for each parameter.
Default: False
Returns: dict: a dictionary containing a whole state of the module
Example: >>> module.state_dict().keys() ['bias', 'weight']
def state_dict(self, destination=None, prefix='', keep_vars=False): """Returns a dictionary containing a whole state of the module. Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names. When keep_vars is ``True``, it returns a Variable for each parameter (rather than a Tensor). Args: destination (dict, optional): if not None, the return dictionary is stored into destination. Default: None prefix (string, optional): Adds a prefix to the key (name) of every parameter and buffer in the result dictionary. Default: '' keep_vars (bool, optional): if ``True``, returns a Variable for each parameter. If ``False``, returns a Tensor for each parameter. Default: ``False`` Returns: dict: a dictionary containing a whole state of the module Example: >>> module.state_dict().keys() ['bias', 'weight'] """ if destination is None: destination = OrderedDict() for name, param in self._parameters.items(): if param is not None: destination[prefix + name] = param if keep_vars else param.data for name, buf in self._buffers.items(): if buf is not None: destination[prefix + name] = buf for name, module in self._modules.items(): if module is not None: module.state_dict(destination, prefix + name + '.', keep_vars=keep_vars) return destination
def train(
self, mode=True)
Sets the module in training mode.
This has any effect only on modules such as Dropout or BatchNorm.
Returns: Module: self
def train(self, mode=True): """Sets the module in training mode. This has any effect only on modules such as Dropout or BatchNorm. Returns: Module: self """ self.training = mode for module in self.children(): module.train(mode) return self
def type(
self, dst_type)
Casts all parameters and buffers to dst_type.
Arguments: dst_type (type or string): the desired type
Returns: Module: self
def type(self, dst_type): """Casts all parameters and buffers to dst_type. Arguments: dst_type (type or string): the desired type Returns: Module: self """ return self._apply(lambda t: t.type(dst_type))
def zero_grad(
self)
Sets gradients of all model parameters to zero.
def zero_grad(self): """Sets gradients of all model parameters to zero.""" for p in self.parameters(): if p.grad is not None: if p.grad.volatile: p.grad.data.zero_() else: data = p.grad.data p.grad = Variable(data.new().resize_as_(data).zero_())
Instance variables
var ffnn
var seq
class CNN1DClassifier
Estimator with one dimensional convolutional neural network.
Parameters
For any parameters not listed, see PTLClassifierBase.
n_layers: int > 0 Number of layers in the NN
n_neurons: int > 0 Number of neurons in every layer
class CNN1DClassifier(PTLClassifierBase): """ Estimator with one dimensional convolutional neural network. Parameters ---------- For any parameters not listed, see PTLClassifierBase. n_layers: int > 0 Number of layers in the NN n_neurons: int > 0 Number of neurons in every layer """ def __init__(self, kernel_size=3, dropout=None, n_layers=1, n_neurons=32, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999): super(CNN1DClassifier, self).__init__( epochs, batch_size, alpha, beta1, beta2 ) self.n_neurons = n_neurons self.n_layers = n_layers self.kernel_size = kernel_size self.dropout = dropout def make_architecture(self, X, y): """ See PTLBase.make_architecture for explanations. """ net = CNN1DClassificationNN( X.shape[1:], len(set(y)), self.n_neurons, self.n_layers, self.kernel_size, dropout=self.dropout ) return net
Ancestors (in MRO)
- CNN1DClassifier
- PTLClassifierBase
- PTLBase
- sklearn.base.BaseEstimator
- sklearn.base.ClassifierMixin
- builtins.object
Static methods
def __init__(
self, kernel_size=3, dropout=None, n_layers=1, n_neurons=32, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999)
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, kernel_size=3, dropout=None, n_layers=1, n_neurons=32, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999): super(CNN1DClassifier, self).__init__( epochs, batch_size, alpha, beta1, beta2 ) self.n_neurons = n_neurons self.n_layers = n_layers self.kernel_size = kernel_size self.dropout = dropout
def fit(
self, X, y)
Trains a classifier on provided data.
Parameters
X: iterable of size n_samples Representation of dataset.
y: iterable of size n_samples Representation of classes
Return
self
def fit(self, X, y): """ Trains a classifier on provided data. Parameters ---------- X: iterable of size n_samples Representation of dataset. y: iterable of size n_samples Representation of classes Return ------ self """ # encode outputs self.label_encoder = LabelEncoder() y = self.label_encoder.fit_transform(y) criterion = nn.CrossEntropyLoss() super(PTLClassifierBase, self).fit(X, y, criterion) return self
def get_params(
self, deep=True)
Get parameters for this estimator.
Parameters
deep : boolean, optional If True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns
params : mapping of string to any Parameter names mapped to their values.
def get_params(self, deep=True): """Get parameters for this estimator. Parameters ---------- deep : boolean, optional If True, will return the parameters for this estimator and contained subobjects that are estimators. Returns ------- params : mapping of string to any Parameter names mapped to their values. """ out = dict() for key in self._get_param_names(): # We need deprecation warnings to always be on in order to # catch deprecated param values. # This is set in utils/__init__.py but it gets overwritten # when running under python3 somehow. warnings.simplefilter("always", DeprecationWarning) try: with warnings.catch_warnings(record=True) as w: value = getattr(self, key, None) if len(w) and w[0].category == DeprecationWarning: # if the parameter is deprecated, don't show it continue finally: warnings.filters.pop(0) # XXX: should we rather test if instance of estimator? if deep and hasattr(value, 'get_params'): deep_items = value.get_params().items() out.update((key + '__' + k, val) for k, val in deep_items) out[key] = value return out
def make_architecture(
self, X, y)
See PTLBase.make_architecture for explanations.
def make_architecture(self, X, y): """ See PTLBase.make_architecture for explanations. """ net = CNN1DClassificationNN( X.shape[1:], len(set(y)), self.n_neurons, self.n_layers, self.kernel_size, dropout=self.dropout ) return net
def predict(
self, X)
Estimate output classes.
Parameters
X: iterable of size n_samples Representation of inputs to classify.
Return
y: iterable of size n_samples Representation of classes
def predict(self, X): """ Estimate output classes. Parameters ---------- X: iterable of size n_samples Representation of inputs to classify. Return ------ y: iterable of size n_samples Representation of classes """ yp = super(PTLClassifierBase, self).predict(X) yp = np.argmax(yp, axis=1) yp = self.label_encoder.inverse_transform(yp) return yp
def score(
self, X, y, sample_weight=None)
Returns the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Parameters
X : array-like, shape = (n_samples, n_features) Test samples.
y : array-like, shape = (n_samples) or (n_samples, n_outputs) True labels for X.
sample_weight : array-like, shape = [n_samples], optional Sample weights.
Returns
score : float Mean accuracy of self.predict(X) wrt. y.
def score(self, X, y, sample_weight=None): """Returns the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted. Parameters ---------- X : array-like, shape = (n_samples, n_features) Test samples. y : array-like, shape = (n_samples) or (n_samples, n_outputs) True labels for X. sample_weight : array-like, shape = [n_samples], optional Sample weights. Returns ------- score : float Mean accuracy of self.predict(X) wrt. y. """ from .metrics import accuracy_score return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
def set_params(
self, **params)
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it's possible to update each
component of a nested object.
Returns
self
def set_params(self, **params): """Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form ``<component>__<parameter>`` so that it's possible to update each component of a nested object. Returns ------- self """ if not params: # Simple optimization to gain speed (inspect is slow) return self valid_params = self.get_params(deep=True) nested_params = defaultdict(dict) # grouped by prefix for key, value in params.items(): key, delim, sub_key = key.partition('__') if key not in valid_params: raise ValueError('Invalid parameter %s for estimator %s. ' 'Check the list of available parameters ' 'with `estimator.get_params().keys()`.' % (key, self)) if delim: nested_params[key][sub_key] = value else: setattr(self, key, value) for key, sub_params in nested_params.items(): valid_params[key].set_params(**sub_params) return self
Instance variables
var dropout
var kernel_size
var n_layers
var n_neurons
class FFNNClassificationNN
Simple fully connected feed forward NN.
Parameters
xsz: int > 0 Size of input vector
ysz: int > 0 Size of output vector
n_layers: int > 0 Number of layers in the neural network
n_neurons: int > 0 Number of neurons in every layer
class FFNNClassificationNN(nn.Module): """ Simple fully connected feed forward NN. Parameters ---------- xsz: int > 0 Size of input vector ysz: int > 0 Size of output vector n_layers: int > 0 Number of layers in the neural network n_neurons: int > 0 Number of neurons in every layer """ def __init__(self, xsz, ysz, n_neurons, n_layers, dropout=None): super(FFNNClassificationNN, self).__init__() hsz = int(xsz) ysz = int(ysz) n_neurons = int(n_neurons) n_layers = int(n_layers) if dropout is not None: dropout = float(dropout) layers = [] for i in range(n_layers): layers.append(nn.Linear(hsz, n_neurons)) layers.append(nn.LeakyReLU()) if dropout is not None: if dropout > 0.03: layers.append(nn.Dropout(p=dropout)) hsz = n_neurons layers.append(nn.Linear(hsz, ysz)) layers.append(nn.Softmax(dim=-1)) self.fc = nn.ModuleList(layers) def forward(self, x): for l in self.fc: x = l(x) return x
Ancestors (in MRO)
- FFNNClassificationNN
- torch.nn.modules.module.Module
- builtins.object
Class variables
var dump_patches
Static methods
def __init__(
self, xsz, ysz, n_neurons, n_layers, dropout=None)
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, xsz, ysz, n_neurons, n_layers, dropout=None): super(FFNNClassificationNN, self).__init__() hsz = int(xsz) ysz = int(ysz) n_neurons = int(n_neurons) n_layers = int(n_layers) if dropout is not None: dropout = float(dropout) layers = [] for i in range(n_layers): layers.append(nn.Linear(hsz, n_neurons)) layers.append(nn.LeakyReLU()) if dropout is not None: if dropout > 0.03: layers.append(nn.Dropout(p=dropout)) hsz = n_neurons layers.append(nn.Linear(hsz, ysz)) layers.append(nn.Softmax(dim=-1)) self.fc = nn.ModuleList(layers)
def add_module(
self, name, module)
Adds a child module to the current module.
The module can be accessed as an attribute using the given name.
Args: name (string): name of the child module. The child module can be accessed from this module using the given name parameter (Module): child module to be added to the module.
def add_module(self, name, module): """Adds a child module to the current module. The module can be accessed as an attribute using the given name. Args: name (string): name of the child module. The child module can be accessed from this module using the given name parameter (Module): child module to be added to the module. """ if not isinstance(module, Module) and module is not None: raise TypeError("{} is not a Module subclass".format( torch.typename(module))) if hasattr(self, name) and name not in self._modules: raise KeyError("attribute '{}' already exists".format(name)) self._modules[name] = module
def apply(
self, fn)
Applies fn
recursively to every submodule (as returned by .children()
)
as well as self. Typical use includes initializing the parameters of a model
(see also :ref:torch-nn-init
).
Args:
fn (:class:Module
-> None): function to be applied to each submodule
Returns: Module: self
Example: >>> def init_weights(m): >>> print(m) >>> if type(m) == nn.Linear: >>> m.weight.data.fill_(1.0) >>> print(m.weight) >>> >>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2)) >>> net.apply(init_weights) Linear (2 -> 2) Parameter containing: 1 1 1 1 [torch.FloatTensor of size 2x2] Linear (2 -> 2) Parameter containing: 1 1 1 1 [torch.FloatTensor of size 2x2] Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) )
def apply(self, fn): """Applies ``fn`` recursively to every submodule (as returned by ``.children()``) as well as self. Typical use includes initializing the parameters of a model (see also :ref:`torch-nn-init`). Args: fn (:class:`Module` -> None): function to be applied to each submodule Returns: Module: self Example: >>> def init_weights(m): >>> print(m) >>> if type(m) == nn.Linear: >>> m.weight.data.fill_(1.0) >>> print(m.weight) >>> >>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2)) >>> net.apply(init_weights) Linear (2 -> 2) Parameter containing: 1 1 1 1 [torch.FloatTensor of size 2x2] Linear (2 -> 2) Parameter containing: 1 1 1 1 [torch.FloatTensor of size 2x2] Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) ) """ for module in self.children(): module.apply(fn) fn(self) return self
def children(
self)
Returns an iterator over immediate children modules.
Yields: Module: a child module
def children(self): """Returns an iterator over immediate children modules. Yields: Module: a child module """ for name, module in self.named_children(): yield module
def cpu(
self)
Moves all model parameters and buffers to the CPU.
Returns: Module: self
def cpu(self): """Moves all model parameters and buffers to the CPU. Returns: Module: self """ return self._apply(lambda t: t.cpu())
def cuda(
self, device=None)
Moves all model parameters and buffers to the GPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing optimizer if the module will live on GPU while being optimized.
Arguments: device (int, optional): if specified, all parameters will be copied to that device
Returns: Module: self
def cuda(self, device=None): """Moves all model parameters and buffers to the GPU. This also makes associated parameters and buffers different objects. So it should be called before constructing optimizer if the module will live on GPU while being optimized. Arguments: device (int, optional): if specified, all parameters will be copied to that device Returns: Module: self """ return self._apply(lambda t: t.cuda(device))
def double(
self)
Casts all parameters and buffers to double datatype.
Returns: Module: self
def double(self): """Casts all parameters and buffers to double datatype. Returns: Module: self """ return self._apply(lambda t: t.double())
def eval(
self)
Sets the module in evaluation mode.
This has any effect only on modules such as Dropout or BatchNorm.
def eval(self): """Sets the module in evaluation mode. This has any effect only on modules such as Dropout or BatchNorm. """ return self.train(False)
def float(
self)
Casts all parameters and buffers to float datatype.
Returns: Module: self
def float(self): """Casts all parameters and buffers to float datatype. Returns: Module: self """ return self._apply(lambda t: t.float())
def forward(
self, x)
Defines the computation performed at every call.
Should be overriden by all subclasses.
.. note::
Although the recipe for forward pass needs to be defined within
this function, one should call the :class:Module
instance afterwards
instead of this since the former takes care of running the
registered hooks while the latter silently ignores them.
def forward(self, x): for l in self.fc: x = l(x) return x
def half(
self)
Casts all parameters and buffers to half datatype.
Returns: Module: self
def half(self): """Casts all parameters and buffers to half datatype. Returns: Module: self """ return self._apply(lambda t: t.half())
def load_state_dict(
self, state_dict, strict=True)
Copies parameters and buffers from :attr:state_dict
into
this module and its descendants. If :attr:strict
is True
then
the keys of :attr:state_dict
must exactly match the keys returned
by this module's :func:state_dict()
function.
Arguments:
state_dict (dict): A dict containing parameters and
persistent buffers.
strict (bool): Strictly enforce that the keys in :attr:state_dict
match the keys returned by this module's :func:
state_dict()`
function.
def load_state_dict(self, state_dict, strict=True): """Copies parameters and buffers from :attr:`state_dict` into this module and its descendants. If :attr:`strict` is ``True`` then the keys of :attr:`state_dict` must exactly match the keys returned by this module's :func:`state_dict()` function. Arguments: state_dict (dict): A dict containing parameters and persistent buffers. strict (bool): Strictly enforce that the keys in :attr:`state_dict` match the keys returned by this module's `:func:`state_dict()` function. """ own_state = self.state_dict() for name, param in state_dict.items(): if name in own_state: if isinstance(param, Parameter): # backwards compatibility for serialized parameters param = param.data try: own_state[name].copy_(param) except Exception: raise RuntimeError('While copying the parameter named {}, ' 'whose dimensions in the model are {} and ' 'whose dimensions in the checkpoint are {}.' .format(name, own_state[name].size(), param.size())) elif strict: raise KeyError('unexpected key "{}" in state_dict' .format(name)) if strict: missing = set(own_state.keys()) - set(state_dict.keys()) if len(missing) > 0: raise KeyError('missing keys in state_dict: "{}"'.format(missing))
def modules(
self)
Returns an iterator over all modules in the network.
Yields: Module: a module in the network
Note:
Duplicate modules are returned only once. In the following
example, l
will be returned only once.
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.modules()): >>> print(idx, '->', m) 0 -> Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) ) 1 -> Linear (2 -> 2)
def modules(self): """Returns an iterator over all modules in the network. Yields: Module: a module in the network Note: Duplicate modules are returned only once. In the following example, ``l`` will be returned only once. >>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.modules()): >>> print(idx, '->', m) 0 -> Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) ) 1 -> Linear (2 -> 2) """ for name, module in self.named_modules(): yield module
def named_children(
self)
Returns an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
Yields: (string, Module): Tuple containing a name and child module
Example: >>> for name, module in model.named_children(): >>> if name in ['conv4', 'conv5']: >>> print(module)
def named_children(self): """Returns an iterator over immediate children modules, yielding both the name of the module as well as the module itself. Yields: (string, Module): Tuple containing a name and child module Example: >>> for name, module in model.named_children(): >>> if name in ['conv4', 'conv5']: >>> print(module) """ memo = set() for name, module in self._modules.items(): if module is not None and module not in memo: memo.add(module) yield name, module
def named_modules(
self, memo=None, prefix='')
Returns an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
Yields: (string, Module): Tuple of name and module
Note:
Duplicate modules are returned only once. In the following
example, l
will be returned only once.
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.named_modules()): >>> print(idx, '->', m) 0 -> ('', Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) )) 1 -> ('0', Linear (2 -> 2))
def named_modules(self, memo=None, prefix=''): """Returns an iterator over all modules in the network, yielding both the name of the module as well as the module itself. Yields: (string, Module): Tuple of name and module Note: Duplicate modules are returned only once. In the following example, ``l`` will be returned only once. >>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.named_modules()): >>> print(idx, '->', m) 0 -> ('', Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) )) 1 -> ('0', Linear (2 -> 2)) """ if memo is None: memo = set() if self not in memo: memo.add(self) yield prefix, self for name, module in self._modules.items(): if module is None: continue submodule_prefix = prefix + ('.' if prefix else '') + name for m in module.named_modules(memo, submodule_prefix): yield m
def named_parameters(
self, memo=None, prefix='')
Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself
Yields: (string, Parameter): Tuple containing the name and parameter
Example: >>> for name, param in self.named_parameters(): >>> if name in ['bias']: >>> print(param.size())
def named_parameters(self, memo=None, prefix=''): """Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself Yields: (string, Parameter): Tuple containing the name and parameter Example: >>> for name, param in self.named_parameters(): >>> if name in ['bias']: >>> print(param.size()) """ if memo is None: memo = set() for name, p in self._parameters.items(): if p is not None and p not in memo: memo.add(p) yield prefix + ('.' if prefix else '') + name, p for mname, module in self.named_children(): submodule_prefix = prefix + ('.' if prefix else '') + mname for name, p in module.named_parameters(memo, submodule_prefix): yield name, p
def parameters(
self)
Returns an iterator over module parameters.
This is typically passed to an optimizer.
Yields: Parameter: module parameter
Example:
>>> for param in model.parameters():
>>> print(type(param.data), param.size())
def parameters(self): """Returns an iterator over module parameters. This is typically passed to an optimizer. Yields: Parameter: module parameter Example: >>> for param in model.parameters(): >>> print(type(param.data), param.size()) <class 'torch.FloatTensor'> (20L,) <class 'torch.FloatTensor'> (20L, 1L, 5L, 5L) """ for name, param in self.named_parameters(): yield param
def register_backward_hook(
self, hook)
Registers a backward hook on the module.
The hook will be called every time the gradients with respect to module inputs are computed. The hook should have the following signature::
hook(module, grad_input, grad_output) -> Tensor or None
The :attr:grad_input
and :attr:grad_output
may be tuples if the
module has multiple inputs or outputs. The hook should not modify its
arguments, but it can optionally return a new gradient with respect to
input that will be used in place of :attr:grad_input
in subsequent
computations.
Returns:
:class:torch.utils.hooks.RemovableHandle
:
a handle that can be used to remove the added hook by calling
handle.remove()
def register_backward_hook(self, hook): """Registers a backward hook on the module. The hook will be called every time the gradients with respect to module inputs are computed. The hook should have the following signature:: hook(module, grad_input, grad_output) -> Tensor or None The :attr:`grad_input` and :attr:`grad_output` may be tuples if the module has multiple inputs or outputs. The hook should not modify its arguments, but it can optionally return a new gradient with respect to input that will be used in place of :attr:`grad_input` in subsequent computations. Returns: :class:`torch.utils.hooks.RemovableHandle`: a handle that can be used to remove the added hook by calling ``handle.remove()`` """ handle = hooks.RemovableHandle(self._backward_hooks) self._backward_hooks[handle.id] = hook return handle
def register_buffer(
self, name, tensor)
Adds a persistent buffer to the module.
This is typically used to register a buffer that should not to be
considered a model parameter. For example, BatchNorm's running_mean
is not a parameter, but is part of the persistent state.
Buffers can be accessed as attributes using given names.
Args: name (string): name of the buffer. The buffer can be accessed from this module using the given name tensor (Tensor): buffer to be registered.
Example: >>> self.register_buffer('running_mean', torch.zeros(num_features))
def register_buffer(self, name, tensor): """Adds a persistent buffer to the module. This is typically used to register a buffer that should not to be considered a model parameter. For example, BatchNorm's ``running_mean`` is not a parameter, but is part of the persistent state. Buffers can be accessed as attributes using given names. Args: name (string): name of the buffer. The buffer can be accessed from this module using the given name tensor (Tensor): buffer to be registered. Example: >>> self.register_buffer('running_mean', torch.zeros(num_features)) """ if hasattr(self, name) and name not in self._buffers: raise KeyError("attribute '{}' already exists".format(name)) self._buffers[name] = tensor
def register_forward_hook(
self, hook)
Registers a forward hook on the module.
The hook will be called every time after :func:forward
has computed an output.
It should have the following signature::
hook(module, input, output) -> None
The hook should not modify the input or output.
Returns:
:class:torch.utils.hooks.RemovableHandle
:
a handle that can be used to remove the added hook by calling
handle.remove()
def register_forward_hook(self, hook): r"""Registers a forward hook on the module. The hook will be called every time after :func:`forward` has computed an output. It should have the following signature:: hook(module, input, output) -> None The hook should not modify the input or output. Returns: :class:`torch.utils.hooks.RemovableHandle`: a handle that can be used to remove the added hook by calling ``handle.remove()`` """ handle = hooks.RemovableHandle(self._forward_hooks) self._forward_hooks[handle.id] = hook return handle
def register_forward_pre_hook(
self, hook)
Registers a forward pre-hook on the module.
The hook will be called every time before :func:forward
is invoked.
It should have the following signature::
hook(module, input) -> None
The hook should not modify the input.
Returns:
:class:torch.utils.hooks.RemovableHandle
:
a handle that can be used to remove the added hook by calling
handle.remove()
def register_forward_pre_hook(self, hook): """Registers a forward pre-hook on the module. The hook will be called every time before :func:`forward` is invoked. It should have the following signature:: hook(module, input) -> None The hook should not modify the input. Returns: :class:`torch.utils.hooks.RemovableHandle`: a handle that can be used to remove the added hook by calling ``handle.remove()`` """ handle = hooks.RemovableHandle(self._forward_pre_hooks) self._forward_pre_hooks[handle.id] = hook return handle
def register_parameter(
self, name, param)
Adds a parameter to the module.
The parameter can be accessed as an attribute using given name.
Args: name (string): name of the parameter. The parameter can be accessed from this module using the given name parameter (Parameter): parameter to be added to the module.
def register_parameter(self, name, param): """Adds a parameter to the module. The parameter can be accessed as an attribute using given name. Args: name (string): name of the parameter. The parameter can be accessed from this module using the given name parameter (Parameter): parameter to be added to the module. """ if '_parameters' not in self.__dict__: raise AttributeError( "cannot assign parameter before Module.__init__() call") if hasattr(self, name) and name not in self._parameters: raise KeyError("attribute '{}' already exists".format(name)) if param is None: self._parameters[name] = None elif not isinstance(param, Parameter): raise TypeError("cannot assign '{}' object to parameter '{}' " "(torch.nn.Parameter or None required)" .format(torch.typename(param), name)) elif param.grad_fn: raise ValueError( "Cannot assign non-leaf Variable to parameter '{0}'. Model " "parameters must be created explicitly. To express '{0}' " "as a function of another variable, compute the value in " "the forward() method.".format(name)) else: self._parameters[name] = param
def state_dict(
self, destination=None, prefix='', keep_vars=False)
Returns a dictionary containing a whole state of the module.
Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names.
When keep_vars is True
, it returns a Variable for each parameter
(rather than a Tensor).
Args:
destination (dict, optional):
if not None, the return dictionary is stored into destination.
Default: None
prefix (string, optional): Adds a prefix to the key (name) of every
parameter and buffer in the result dictionary. Default: ''
keep_vars (bool, optional): if True
, returns a Variable for each
parameter. If False
, returns a Tensor for each parameter.
Default: False
Returns: dict: a dictionary containing a whole state of the module
Example: >>> module.state_dict().keys() ['bias', 'weight']
def state_dict(self, destination=None, prefix='', keep_vars=False): """Returns a dictionary containing a whole state of the module. Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names. When keep_vars is ``True``, it returns a Variable for each parameter (rather than a Tensor). Args: destination (dict, optional): if not None, the return dictionary is stored into destination. Default: None prefix (string, optional): Adds a prefix to the key (name) of every parameter and buffer in the result dictionary. Default: '' keep_vars (bool, optional): if ``True``, returns a Variable for each parameter. If ``False``, returns a Tensor for each parameter. Default: ``False`` Returns: dict: a dictionary containing a whole state of the module Example: >>> module.state_dict().keys() ['bias', 'weight'] """ if destination is None: destination = OrderedDict() for name, param in self._parameters.items(): if param is not None: destination[prefix + name] = param if keep_vars else param.data for name, buf in self._buffers.items(): if buf is not None: destination[prefix + name] = buf for name, module in self._modules.items(): if module is not None: module.state_dict(destination, prefix + name + '.', keep_vars=keep_vars) return destination
def train(
self, mode=True)
Sets the module in training mode.
This has any effect only on modules such as Dropout or BatchNorm.
Returns: Module: self
def train(self, mode=True): """Sets the module in training mode. This has any effect only on modules such as Dropout or BatchNorm. Returns: Module: self """ self.training = mode for module in self.children(): module.train(mode) return self
def type(
self, dst_type)
Casts all parameters and buffers to dst_type.
Arguments: dst_type (type or string): the desired type
Returns: Module: self
def type(self, dst_type): """Casts all parameters and buffers to dst_type. Arguments: dst_type (type or string): the desired type Returns: Module: self """ return self._apply(lambda t: t.type(dst_type))
def zero_grad(
self)
Sets gradients of all model parameters to zero.
def zero_grad(self): """Sets gradients of all model parameters to zero.""" for p in self.parameters(): if p.grad is not None: if p.grad.volatile: p.grad.data.zero_() else: data = p.grad.data p.grad = Variable(data.new().resize_as_(data).zero_())
Instance variables
var fc
class GRUClassification
Recurent neural network module. Maps sequence to vector output.
Parameters
xsz: int > 0 Size of input vector
ysz: int > 0 Size of output vector
n_layers: int > 0 Number of layers in the neural network
n_neurons: int > 0 Number of neurons in every layer
class GRUClassification(nn.Module): """ Recurent neural network module. Maps sequence to vector output. Parameters ---------- xsz: int > 0 Size of input vector ysz: int > 0 Size of output vector n_layers: int > 0 Number of layers in the neural network n_neurons: int > 0 Number of neurons in every layer """ def __init__(self, xsz, ysz, n_neurons=64, n_layers=1, dropout=None): super(GRUClassification, self).__init__() ssz = int(xsz[0]) hsz = int(xsz[1]) ysz = int(ysz) n_neurons = int(n_neurons) n_layers = int(n_layers) if dropout is not None: dropout = float(dropout) else: dropout = 0.0 self.rnn = nn.GRU(hsz, n_neurons, n_layers, dropout=dropout) # calculate flatten hsz = n_neurons self.ffnn = FFNNClassificationNN(hsz, ysz, n_neurons=n_neurons, n_layers=1) def forward(self, x): # swap to (seq_len, batch, input_size) x = x.transpose(0, 1) _, x = self.rnn(x) # flatten the data x = x[0, :, :] x = self.ffnn(x) return x
Ancestors (in MRO)
- GRUClassification
- torch.nn.modules.module.Module
- builtins.object
Class variables
var dump_patches
Static methods
def __init__(
self, xsz, ysz, n_neurons=64, n_layers=1, dropout=None)
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, xsz, ysz, n_neurons=64, n_layers=1, dropout=None): super(GRUClassification, self).__init__() ssz = int(xsz[0]) hsz = int(xsz[1]) ysz = int(ysz) n_neurons = int(n_neurons) n_layers = int(n_layers) if dropout is not None: dropout = float(dropout) else: dropout = 0.0 self.rnn = nn.GRU(hsz, n_neurons, n_layers, dropout=dropout) # calculate flatten hsz = n_neurons self.ffnn = FFNNClassificationNN(hsz, ysz, n_neurons=n_neurons, n_layers=1)
def add_module(
self, name, module)
Adds a child module to the current module.
The module can be accessed as an attribute using the given name.
Args: name (string): name of the child module. The child module can be accessed from this module using the given name parameter (Module): child module to be added to the module.
def add_module(self, name, module): """Adds a child module to the current module. The module can be accessed as an attribute using the given name. Args: name (string): name of the child module. The child module can be accessed from this module using the given name parameter (Module): child module to be added to the module. """ if not isinstance(module, Module) and module is not None: raise TypeError("{} is not a Module subclass".format( torch.typename(module))) if hasattr(self, name) and name not in self._modules: raise KeyError("attribute '{}' already exists".format(name)) self._modules[name] = module
def apply(
self, fn)
Applies fn
recursively to every submodule (as returned by .children()
)
as well as self. Typical use includes initializing the parameters of a model
(see also :ref:torch-nn-init
).
Args:
fn (:class:Module
-> None): function to be applied to each submodule
Returns: Module: self
Example: >>> def init_weights(m): >>> print(m) >>> if type(m) == nn.Linear: >>> m.weight.data.fill_(1.0) >>> print(m.weight) >>> >>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2)) >>> net.apply(init_weights) Linear (2 -> 2) Parameter containing: 1 1 1 1 [torch.FloatTensor of size 2x2] Linear (2 -> 2) Parameter containing: 1 1 1 1 [torch.FloatTensor of size 2x2] Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) )
def apply(self, fn): """Applies ``fn`` recursively to every submodule (as returned by ``.children()``) as well as self. Typical use includes initializing the parameters of a model (see also :ref:`torch-nn-init`). Args: fn (:class:`Module` -> None): function to be applied to each submodule Returns: Module: self Example: >>> def init_weights(m): >>> print(m) >>> if type(m) == nn.Linear: >>> m.weight.data.fill_(1.0) >>> print(m.weight) >>> >>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2)) >>> net.apply(init_weights) Linear (2 -> 2) Parameter containing: 1 1 1 1 [torch.FloatTensor of size 2x2] Linear (2 -> 2) Parameter containing: 1 1 1 1 [torch.FloatTensor of size 2x2] Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) ) """ for module in self.children(): module.apply(fn) fn(self) return self
def children(
self)
Returns an iterator over immediate children modules.
Yields: Module: a child module
def children(self): """Returns an iterator over immediate children modules. Yields: Module: a child module """ for name, module in self.named_children(): yield module
def cpu(
self)
Moves all model parameters and buffers to the CPU.
Returns: Module: self
def cpu(self): """Moves all model parameters and buffers to the CPU. Returns: Module: self """ return self._apply(lambda t: t.cpu())
def cuda(
self, device=None)
Moves all model parameters and buffers to the GPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing optimizer if the module will live on GPU while being optimized.
Arguments: device (int, optional): if specified, all parameters will be copied to that device
Returns: Module: self
def cuda(self, device=None): """Moves all model parameters and buffers to the GPU. This also makes associated parameters and buffers different objects. So it should be called before constructing optimizer if the module will live on GPU while being optimized. Arguments: device (int, optional): if specified, all parameters will be copied to that device Returns: Module: self """ return self._apply(lambda t: t.cuda(device))
def double(
self)
Casts all parameters and buffers to double datatype.
Returns: Module: self
def double(self): """Casts all parameters and buffers to double datatype. Returns: Module: self """ return self._apply(lambda t: t.double())
def eval(
self)
Sets the module in evaluation mode.
This has any effect only on modules such as Dropout or BatchNorm.
def eval(self): """Sets the module in evaluation mode. This has any effect only on modules such as Dropout or BatchNorm. """ return self.train(False)
def float(
self)
Casts all parameters and buffers to float datatype.
Returns: Module: self
def float(self): """Casts all parameters and buffers to float datatype. Returns: Module: self """ return self._apply(lambda t: t.float())
def forward(
self, x)
Defines the computation performed at every call.
Should be overriden by all subclasses.
.. note::
Although the recipe for forward pass needs to be defined within
this function, one should call the :class:Module
instance afterwards
instead of this since the former takes care of running the
registered hooks while the latter silently ignores them.
def forward(self, x): # swap to (seq_len, batch, input_size) x = x.transpose(0, 1) _, x = self.rnn(x) # flatten the data x = x[0, :, :] x = self.ffnn(x) return x
def half(
self)
Casts all parameters and buffers to half datatype.
Returns: Module: self
def half(self): """Casts all parameters and buffers to half datatype. Returns: Module: self """ return self._apply(lambda t: t.half())
def load_state_dict(
self, state_dict, strict=True)
Copies parameters and buffers from :attr:state_dict
into
this module and its descendants. If :attr:strict
is True
then
the keys of :attr:state_dict
must exactly match the keys returned
by this module's :func:state_dict()
function.
Arguments:
state_dict (dict): A dict containing parameters and
persistent buffers.
strict (bool): Strictly enforce that the keys in :attr:state_dict
match the keys returned by this module's :func:
state_dict()`
function.
def load_state_dict(self, state_dict, strict=True): """Copies parameters and buffers from :attr:`state_dict` into this module and its descendants. If :attr:`strict` is ``True`` then the keys of :attr:`state_dict` must exactly match the keys returned by this module's :func:`state_dict()` function. Arguments: state_dict (dict): A dict containing parameters and persistent buffers. strict (bool): Strictly enforce that the keys in :attr:`state_dict` match the keys returned by this module's `:func:`state_dict()` function. """ own_state = self.state_dict() for name, param in state_dict.items(): if name in own_state: if isinstance(param, Parameter): # backwards compatibility for serialized parameters param = param.data try: own_state[name].copy_(param) except Exception: raise RuntimeError('While copying the parameter named {}, ' 'whose dimensions in the model are {} and ' 'whose dimensions in the checkpoint are {}.' .format(name, own_state[name].size(), param.size())) elif strict: raise KeyError('unexpected key "{}" in state_dict' .format(name)) if strict: missing = set(own_state.keys()) - set(state_dict.keys()) if len(missing) > 0: raise KeyError('missing keys in state_dict: "{}"'.format(missing))
def modules(
self)
Returns an iterator over all modules in the network.
Yields: Module: a module in the network
Note:
Duplicate modules are returned only once. In the following
example, l
will be returned only once.
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.modules()): >>> print(idx, '->', m) 0 -> Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) ) 1 -> Linear (2 -> 2)
def modules(self): """Returns an iterator over all modules in the network. Yields: Module: a module in the network Note: Duplicate modules are returned only once. In the following example, ``l`` will be returned only once. >>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.modules()): >>> print(idx, '->', m) 0 -> Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) ) 1 -> Linear (2 -> 2) """ for name, module in self.named_modules(): yield module
def named_children(
self)
Returns an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
Yields: (string, Module): Tuple containing a name and child module
Example: >>> for name, module in model.named_children(): >>> if name in ['conv4', 'conv5']: >>> print(module)
def named_children(self): """Returns an iterator over immediate children modules, yielding both the name of the module as well as the module itself. Yields: (string, Module): Tuple containing a name and child module Example: >>> for name, module in model.named_children(): >>> if name in ['conv4', 'conv5']: >>> print(module) """ memo = set() for name, module in self._modules.items(): if module is not None and module not in memo: memo.add(module) yield name, module
def named_modules(
self, memo=None, prefix='')
Returns an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
Yields: (string, Module): Tuple of name and module
Note:
Duplicate modules are returned only once. In the following
example, l
will be returned only once.
>>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.named_modules()): >>> print(idx, '->', m) 0 -> ('', Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) )) 1 -> ('0', Linear (2 -> 2))
def named_modules(self, memo=None, prefix=''): """Returns an iterator over all modules in the network, yielding both the name of the module as well as the module itself. Yields: (string, Module): Tuple of name and module Note: Duplicate modules are returned only once. In the following example, ``l`` will be returned only once. >>> l = nn.Linear(2, 2) >>> net = nn.Sequential(l, l) >>> for idx, m in enumerate(net.named_modules()): >>> print(idx, '->', m) 0 -> ('', Sequential ( (0): Linear (2 -> 2) (1): Linear (2 -> 2) )) 1 -> ('0', Linear (2 -> 2)) """ if memo is None: memo = set() if self not in memo: memo.add(self) yield prefix, self for name, module in self._modules.items(): if module is None: continue submodule_prefix = prefix + ('.' if prefix else '') + name for m in module.named_modules(memo, submodule_prefix): yield m
def named_parameters(
self, memo=None, prefix='')
Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself
Yields: (string, Parameter): Tuple containing the name and parameter
Example: >>> for name, param in self.named_parameters(): >>> if name in ['bias']: >>> print(param.size())
def named_parameters(self, memo=None, prefix=''): """Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself Yields: (string, Parameter): Tuple containing the name and parameter Example: >>> for name, param in self.named_parameters(): >>> if name in ['bias']: >>> print(param.size()) """ if memo is None: memo = set() for name, p in self._parameters.items(): if p is not None and p not in memo: memo.add(p) yield prefix + ('.' if prefix else '') + name, p for mname, module in self.named_children(): submodule_prefix = prefix + ('.' if prefix else '') + mname for name, p in module.named_parameters(memo, submodule_prefix): yield name, p
def parameters(
self)
Returns an iterator over module parameters.
This is typically passed to an optimizer.
Yields: Parameter: module parameter
Example:
>>> for param in model.parameters():
>>> print(type(param.data), param.size())
def parameters(self): """Returns an iterator over module parameters. This is typically passed to an optimizer. Yields: Parameter: module parameter Example: >>> for param in model.parameters(): >>> print(type(param.data), param.size()) <class 'torch.FloatTensor'> (20L,) <class 'torch.FloatTensor'> (20L, 1L, 5L, 5L) """ for name, param in self.named_parameters(): yield param
def register_backward_hook(
self, hook)
Registers a backward hook on the module.
The hook will be called every time the gradients with respect to module inputs are computed. The hook should have the following signature::
hook(module, grad_input, grad_output) -> Tensor or None
The :attr:grad_input
and :attr:grad_output
may be tuples if the
module has multiple inputs or outputs. The hook should not modify its
arguments, but it can optionally return a new gradient with respect to
input that will be used in place of :attr:grad_input
in subsequent
computations.
Returns:
:class:torch.utils.hooks.RemovableHandle
:
a handle that can be used to remove the added hook by calling
handle.remove()
def register_backward_hook(self, hook): """Registers a backward hook on the module. The hook will be called every time the gradients with respect to module inputs are computed. The hook should have the following signature:: hook(module, grad_input, grad_output) -> Tensor or None The :attr:`grad_input` and :attr:`grad_output` may be tuples if the module has multiple inputs or outputs. The hook should not modify its arguments, but it can optionally return a new gradient with respect to input that will be used in place of :attr:`grad_input` in subsequent computations. Returns: :class:`torch.utils.hooks.RemovableHandle`: a handle that can be used to remove the added hook by calling ``handle.remove()`` """ handle = hooks.RemovableHandle(self._backward_hooks) self._backward_hooks[handle.id] = hook return handle
def register_buffer(
self, name, tensor)
Adds a persistent buffer to the module.
This is typically used to register a buffer that should not to be
considered a model parameter. For example, BatchNorm's running_mean
is not a parameter, but is part of the persistent state.
Buffers can be accessed as attributes using given names.
Args: name (string): name of the buffer. The buffer can be accessed from this module using the given name tensor (Tensor): buffer to be registered.
Example: >>> self.register_buffer('running_mean', torch.zeros(num_features))
def register_buffer(self, name, tensor): """Adds a persistent buffer to the module. This is typically used to register a buffer that should not to be considered a model parameter. For example, BatchNorm's ``running_mean`` is not a parameter, but is part of the persistent state. Buffers can be accessed as attributes using given names. Args: name (string): name of the buffer. The buffer can be accessed from this module using the given name tensor (Tensor): buffer to be registered. Example: >>> self.register_buffer('running_mean', torch.zeros(num_features)) """ if hasattr(self, name) and name not in self._buffers: raise KeyError("attribute '{}' already exists".format(name)) self._buffers[name] = tensor
def register_forward_hook(
self, hook)
Registers a forward hook on the module.
The hook will be called every time after :func:forward
has computed an output.
It should have the following signature::
hook(module, input, output) -> None
The hook should not modify the input or output.
Returns:
:class:torch.utils.hooks.RemovableHandle
:
a handle that can be used to remove the added hook by calling
handle.remove()
def register_forward_hook(self, hook): r"""Registers a forward hook on the module. The hook will be called every time after :func:`forward` has computed an output. It should have the following signature:: hook(module, input, output) -> None The hook should not modify the input or output. Returns: :class:`torch.utils.hooks.RemovableHandle`: a handle that can be used to remove the added hook by calling ``handle.remove()`` """ handle = hooks.RemovableHandle(self._forward_hooks) self._forward_hooks[handle.id] = hook return handle
def register_forward_pre_hook(
self, hook)
Registers a forward pre-hook on the module.
The hook will be called every time before :func:forward
is invoked.
It should have the following signature::
hook(module, input) -> None
The hook should not modify the input.
Returns:
:class:torch.utils.hooks.RemovableHandle
:
a handle that can be used to remove the added hook by calling
handle.remove()
def register_forward_pre_hook(self, hook): """Registers a forward pre-hook on the module. The hook will be called every time before :func:`forward` is invoked. It should have the following signature:: hook(module, input) -> None The hook should not modify the input. Returns: :class:`torch.utils.hooks.RemovableHandle`: a handle that can be used to remove the added hook by calling ``handle.remove()`` """ handle = hooks.RemovableHandle(self._forward_pre_hooks) self._forward_pre_hooks[handle.id] = hook return handle
def register_parameter(
self, name, param)
Adds a parameter to the module.
The parameter can be accessed as an attribute using given name.
Args: name (string): name of the parameter. The parameter can be accessed from this module using the given name parameter (Parameter): parameter to be added to the module.
def register_parameter(self, name, param): """Adds a parameter to the module. The parameter can be accessed as an attribute using given name. Args: name (string): name of the parameter. The parameter can be accessed from this module using the given name parameter (Parameter): parameter to be added to the module. """ if '_parameters' not in self.__dict__: raise AttributeError( "cannot assign parameter before Module.__init__() call") if hasattr(self, name) and name not in self._parameters: raise KeyError("attribute '{}' already exists".format(name)) if param is None: self._parameters[name] = None elif not isinstance(param, Parameter): raise TypeError("cannot assign '{}' object to parameter '{}' " "(torch.nn.Parameter or None required)" .format(torch.typename(param), name)) elif param.grad_fn: raise ValueError( "Cannot assign non-leaf Variable to parameter '{0}'. Model " "parameters must be created explicitly. To express '{0}' " "as a function of another variable, compute the value in " "the forward() method.".format(name)) else: self._parameters[name] = param
def state_dict(
self, destination=None, prefix='', keep_vars=False)
Returns a dictionary containing a whole state of the module.
Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names.
When keep_vars is True
, it returns a Variable for each parameter
(rather than a Tensor).
Args:
destination (dict, optional):
if not None, the return dictionary is stored into destination.
Default: None
prefix (string, optional): Adds a prefix to the key (name) of every
parameter and buffer in the result dictionary. Default: ''
keep_vars (bool, optional): if True
, returns a Variable for each
parameter. If False
, returns a Tensor for each parameter.
Default: False
Returns: dict: a dictionary containing a whole state of the module
Example: >>> module.state_dict().keys() ['bias', 'weight']
def state_dict(self, destination=None, prefix='', keep_vars=False): """Returns a dictionary containing a whole state of the module. Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names. When keep_vars is ``True``, it returns a Variable for each parameter (rather than a Tensor). Args: destination (dict, optional): if not None, the return dictionary is stored into destination. Default: None prefix (string, optional): Adds a prefix to the key (name) of every parameter and buffer in the result dictionary. Default: '' keep_vars (bool, optional): if ``True``, returns a Variable for each parameter. If ``False``, returns a Tensor for each parameter. Default: ``False`` Returns: dict: a dictionary containing a whole state of the module Example: >>> module.state_dict().keys() ['bias', 'weight'] """ if destination is None: destination = OrderedDict() for name, param in self._parameters.items(): if param is not None: destination[prefix + name] = param if keep_vars else param.data for name, buf in self._buffers.items(): if buf is not None: destination[prefix + name] = buf for name, module in self._modules.items(): if module is not None: module.state_dict(destination, prefix + name + '.', keep_vars=keep_vars) return destination
def train(
self, mode=True)
Sets the module in training mode.
This has any effect only on modules such as Dropout or BatchNorm.
Returns: Module: self
def train(self, mode=True): """Sets the module in training mode. This has any effect only on modules such as Dropout or BatchNorm. Returns: Module: self """ self.training = mode for module in self.children(): module.train(mode) return self
def type(
self, dst_type)
Casts all parameters and buffers to dst_type.
Arguments: dst_type (type or string): the desired type
Returns: Module: self
def type(self, dst_type): """Casts all parameters and buffers to dst_type. Arguments: dst_type (type or string): the desired type Returns: Module: self """ return self._apply(lambda t: t.type(dst_type))
def zero_grad(
self)
Sets gradients of all model parameters to zero.
def zero_grad(self): """Sets gradients of all model parameters to zero.""" for p in self.parameters(): if p.grad is not None: if p.grad.volatile: p.grad.data.zero_() else: data = p.grad.data p.grad = Variable(data.new().resize_as_(data).zero_())
Instance variables
var ffnn
var rnn
class GRUClassifier
Estimator with one dimensional convolutional neural network.
Parameters
For any parameters not listed, see PTLClassifierBase.
n_layers: int > 0 Number of layers in the NN
n_neurons: int > 0 Number of neurons in every layer
class GRUClassifier(PTLClassifierBase): """ Estimator with one dimensional convolutional neural network. Parameters ---------- For any parameters not listed, see PTLClassifierBase. n_layers: int > 0 Number of layers in the NN n_neurons: int > 0 Number of neurons in every layer """ def __init__(self, n_layers=1, n_neurons=32, dropout=None, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999): super(GRUClassifier, self).__init__( epochs, batch_size, alpha, beta1, beta2 ) self.n_neurons = n_neurons self.n_layers = n_layers self.dropout = dropout def make_architecture(self, X, y): """ See PTLBase.make_architecture for explanations. """ net = GRUClassification( X.shape[1:], len(set(y)), self.n_neurons, self.n_layers, dropout=self.dropout ) return net
Ancestors (in MRO)
- GRUClassifier
- PTLClassifierBase
- PTLBase
- sklearn.base.BaseEstimator
- sklearn.base.ClassifierMixin
- builtins.object
Static methods
def __init__(
self, n_layers=1, n_neurons=32, dropout=None, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999)
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, n_layers=1, n_neurons=32, dropout=None, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999): super(GRUClassifier, self).__init__( epochs, batch_size, alpha, beta1, beta2 ) self.n_neurons = n_neurons self.n_layers = n_layers self.dropout = dropout
def fit(
self, X, y)
Trains a classifier on provided data.
Parameters
X: iterable of size n_samples Representation of dataset.
y: iterable of size n_samples Representation of classes
Return
self
def fit(self, X, y): """ Trains a classifier on provided data. Parameters ---------- X: iterable of size n_samples Representation of dataset. y: iterable of size n_samples Representation of classes Return ------ self """ # encode outputs self.label_encoder = LabelEncoder() y = self.label_encoder.fit_transform(y) criterion = nn.CrossEntropyLoss() super(PTLClassifierBase, self).fit(X, y, criterion) return self
def get_params(
self, deep=True)
Get parameters for this estimator.
Parameters
deep : boolean, optional If True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns
params : mapping of string to any Parameter names mapped to their values.
def get_params(self, deep=True): """Get parameters for this estimator. Parameters ---------- deep : boolean, optional If True, will return the parameters for this estimator and contained subobjects that are estimators. Returns ------- params : mapping of string to any Parameter names mapped to their values. """ out = dict() for key in self._get_param_names(): # We need deprecation warnings to always be on in order to # catch deprecated param values. # This is set in utils/__init__.py but it gets overwritten # when running under python3 somehow. warnings.simplefilter("always", DeprecationWarning) try: with warnings.catch_warnings(record=True) as w: value = getattr(self, key, None) if len(w) and w[0].category == DeprecationWarning: # if the parameter is deprecated, don't show it continue finally: warnings.filters.pop(0) # XXX: should we rather test if instance of estimator? if deep and hasattr(value, 'get_params'): deep_items = value.get_params().items() out.update((key + '__' + k, val) for k, val in deep_items) out[key] = value return out
def make_architecture(
self, X, y)
See PTLBase.make_architecture for explanations.
def make_architecture(self, X, y): """ See PTLBase.make_architecture for explanations. """ net = GRUClassification( X.shape[1:], len(set(y)), self.n_neurons, self.n_layers, dropout=self.dropout ) return net
def predict(
self, X)
Estimate output classes.
Parameters
X: iterable of size n_samples Representation of inputs to classify.
Return
y: iterable of size n_samples Representation of classes
def predict(self, X): """ Estimate output classes. Parameters ---------- X: iterable of size n_samples Representation of inputs to classify. Return ------ y: iterable of size n_samples Representation of classes """ yp = super(PTLClassifierBase, self).predict(X) yp = np.argmax(yp, axis=1) yp = self.label_encoder.inverse_transform(yp) return yp
def score(
self, X, y, sample_weight=None)
Returns the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Parameters
X : array-like, shape = (n_samples, n_features) Test samples.
y : array-like, shape = (n_samples) or (n_samples, n_outputs) True labels for X.
sample_weight : array-like, shape = [n_samples], optional Sample weights.
Returns
score : float Mean accuracy of self.predict(X) wrt. y.
def score(self, X, y, sample_weight=None): """Returns the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted. Parameters ---------- X : array-like, shape = (n_samples, n_features) Test samples. y : array-like, shape = (n_samples) or (n_samples, n_outputs) True labels for X. sample_weight : array-like, shape = [n_samples], optional Sample weights. Returns ------- score : float Mean accuracy of self.predict(X) wrt. y. """ from .metrics import accuracy_score return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
def set_params(
self, **params)
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it's possible to update each
component of a nested object.
Returns
self
def set_params(self, **params): """Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form ``<component>__<parameter>`` so that it's possible to update each component of a nested object. Returns ------- self """ if not params: # Simple optimization to gain speed (inspect is slow) return self valid_params = self.get_params(deep=True) nested_params = defaultdict(dict) # grouped by prefix for key, value in params.items(): key, delim, sub_key = key.partition('__') if key not in valid_params: raise ValueError('Invalid parameter %s for estimator %s. ' 'Check the list of available parameters ' 'with `estimator.get_params().keys()`.' % (key, self)) if delim: nested_params[key][sub_key] = value else: setattr(self, key, value) for key, sub_params in nested_params.items(): valid_params[key].set_params(**sub_params) return self
Instance variables
var dropout
var n_layers
var n_neurons
class MLPClassifier
Estimator with Feed Forward Neural Network.
Parameters
For any parameters not listed, see PTLClassifierBase.
n_layers: int > 0 Number of layers in the NN
n_neurons: int > 0 Number of neurons in every layer
class MLPClassifier(PTLClassifierBase): """ Estimator with Feed Forward Neural Network. Parameters ---------- For any parameters not listed, see PTLClassifierBase. n_layers: int > 0 Number of layers in the NN n_neurons: int > 0 Number of neurons in every layer """ def __init__(self, dropout=None, n_layers=1, n_neurons=32, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999): super(MLPClassifier, self).__init__( epochs, batch_size, alpha, beta1, beta2 ) self.n_neurons = n_neurons self.n_layers = n_layers self.dropout = dropout def make_architecture(self, X, y): """ See PTLBase.make_architecture for explanations. """ net = FFNNClassificationNN( X.shape[-1], len(set(y)), self.n_neurons, self.n_layers, dropout=self.dropout ) return net
Ancestors (in MRO)
- MLPClassifier
- PTLClassifierBase
- PTLBase
- sklearn.base.BaseEstimator
- sklearn.base.ClassifierMixin
- builtins.object
Static methods
def __init__(
self, dropout=None, n_layers=1, n_neurons=32, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999)
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, dropout=None, n_layers=1, n_neurons=32, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999): super(MLPClassifier, self).__init__( epochs, batch_size, alpha, beta1, beta2 ) self.n_neurons = n_neurons self.n_layers = n_layers self.dropout = dropout
def fit(
self, X, y)
Trains a classifier on provided data.
Parameters
X: iterable of size n_samples Representation of dataset.
y: iterable of size n_samples Representation of classes
Return
self
def fit(self, X, y): """ Trains a classifier on provided data. Parameters ---------- X: iterable of size n_samples Representation of dataset. y: iterable of size n_samples Representation of classes Return ------ self """ # encode outputs self.label_encoder = LabelEncoder() y = self.label_encoder.fit_transform(y) criterion = nn.CrossEntropyLoss() super(PTLClassifierBase, self).fit(X, y, criterion) return self
def get_params(
self, deep=True)
Get parameters for this estimator.
Parameters
deep : boolean, optional If True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns
params : mapping of string to any Parameter names mapped to their values.
def get_params(self, deep=True): """Get parameters for this estimator. Parameters ---------- deep : boolean, optional If True, will return the parameters for this estimator and contained subobjects that are estimators. Returns ------- params : mapping of string to any Parameter names mapped to their values. """ out = dict() for key in self._get_param_names(): # We need deprecation warnings to always be on in order to # catch deprecated param values. # This is set in utils/__init__.py but it gets overwritten # when running under python3 somehow. warnings.simplefilter("always", DeprecationWarning) try: with warnings.catch_warnings(record=True) as w: value = getattr(self, key, None) if len(w) and w[0].category == DeprecationWarning: # if the parameter is deprecated, don't show it continue finally: warnings.filters.pop(0) # XXX: should we rather test if instance of estimator? if deep and hasattr(value, 'get_params'): deep_items = value.get_params().items() out.update((key + '__' + k, val) for k, val in deep_items) out[key] = value return out
def make_architecture(
self, X, y)
See PTLBase.make_architecture for explanations.
def make_architecture(self, X, y): """ See PTLBase.make_architecture for explanations. """ net = FFNNClassificationNN( X.shape[-1], len(set(y)), self.n_neurons, self.n_layers, dropout=self.dropout ) return net
def predict(
self, X)
Estimate output classes.
Parameters
X: iterable of size n_samples Representation of inputs to classify.
Return
y: iterable of size n_samples Representation of classes
def predict(self, X): """ Estimate output classes. Parameters ---------- X: iterable of size n_samples Representation of inputs to classify. Return ------ y: iterable of size n_samples Representation of classes """ yp = super(PTLClassifierBase, self).predict(X) yp = np.argmax(yp, axis=1) yp = self.label_encoder.inverse_transform(yp) return yp
def score(
self, X, y, sample_weight=None)
Returns the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Parameters
X : array-like, shape = (n_samples, n_features) Test samples.
y : array-like, shape = (n_samples) or (n_samples, n_outputs) True labels for X.
sample_weight : array-like, shape = [n_samples], optional Sample weights.
Returns
score : float Mean accuracy of self.predict(X) wrt. y.
def score(self, X, y, sample_weight=None): """Returns the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted. Parameters ---------- X : array-like, shape = (n_samples, n_features) Test samples. y : array-like, shape = (n_samples) or (n_samples, n_outputs) True labels for X. sample_weight : array-like, shape = [n_samples], optional Sample weights. Returns ------- score : float Mean accuracy of self.predict(X) wrt. y. """ from .metrics import accuracy_score return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
def set_params(
self, **params)
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it's possible to update each
component of a nested object.
Returns
self
def set_params(self, **params): """Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form ``<component>__<parameter>`` so that it's possible to update each component of a nested object. Returns ------- self """ if not params: # Simple optimization to gain speed (inspect is slow) return self valid_params = self.get_params(deep=True) nested_params = defaultdict(dict) # grouped by prefix for key, value in params.items(): key, delim, sub_key = key.partition('__') if key not in valid_params: raise ValueError('Invalid parameter %s for estimator %s. ' 'Check the list of available parameters ' 'with `estimator.get_params().keys()`.' % (key, self)) if delim: nested_params[key][sub_key] = value else: setattr(self, key, value) for key, sub_params in nested_params.items(): valid_params[key].set_params(**sub_params) return self
Instance variables
var dropout
var n_layers
var n_neurons
class PTLBase
A base class for learning algorithms with pytorch.
Parameters
epochs: int > 0 Number of epochs to train neural network for.
batch_size: int > 0 Size of subsample of dataset to use to approximate the gradient in stochatic gradient descent procedure.
alpha: float > 0 Learning rate. Tunes the amount of update done after processing of single batch size.
beta1: float 0.0 < x < 1.0 Beta 1 parameter of Adam stochastic gradient descent algorithm.
beta2: float 0.0 < x < 1.0 Beta 2 parameter of Adam stochastic gradient descent algorithm.
class PTLBase(BaseEstimator): """ A base class for learning algorithms with pytorch. Parameters ---------- epochs: int > 0 Number of epochs to train neural network for. batch_size: int > 0 Size of subsample of dataset to use to approximate the gradient in stochatic gradient descent procedure. alpha: float > 0 Learning rate. Tunes the amount of update done after processing of single batch size. beta1: float 0.0 < x < 1.0 Beta 1 parameter of Adam stochastic gradient descent algorithm. beta2: float 0.0 < x < 1.0 Beta 2 parameter of Adam stochastic gradient descent algorithm. """ def __init__(self, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999): self.epochs = epochs self.batch_size = batch_size self.alpha = alpha self.beta1 = beta1 self.beta2 = beta2 self.net = None @abstractmethod def make_architecture(self, X, y): """ Should return nn.Module instance, which represents architecture of the neural network. Parameters ---------- X: iterable of size n_samples Representation of dataset. y: iterable of size n_samples Representation of output Return ------ net: an instance of a neural network to be trained. """ pass def fit(self, X, y, criterion): """ Trains a neural network on provided data. Parameters ---------- X: iterable of size n_samples Representation of dataset. y: iterable of size n_samples Representation of output criterion: callable with 2 arguments, possibly a nn._Loss instance. Cost function to minimize. Return ------ self """ check_X_y(X, y, allow_nd=True, dtype=None) self.net = self.make_architecture(X, y) optimizer = optim.Adam(self.net.parameters(), lr=self.alpha, betas=(self.beta1, self.beta2)) data = torch.utils.data.TensorDataset( torch.FloatTensor(X), torch.LongTensor(y) ) # this creates mixed batches trainloader = torch.utils.data.DataLoader( data, batch_size=self.batch_size, shuffle=True ) for epoch in range(self.epochs): # loop over the dataset multiple times for i, data in enumerate(trainloader, 0): # get the inputs inputs, labels = data # wrap them in Variable inputs, labels = Variable(inputs), Variable(labels) # zero the parameter gradients optimizer.zero_grad() # forward + backward + optimize outputs = self.net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() return self def predict(self, X): """ Make estimation with trained neural network. Parameters ---------- X: iterable of size n_samples Representation of inputs. Should be consistent with inputs in the training dataset. Return ------ X: iterable of size n_samples Representation of estimated outputs. """ if self.net is None: raise RuntimeError("The model is not fit. Did you forget to call the fit method on a dataset?") X = Variable(torch.FloatTensor(X), volatile=True) yp = self.net(X).data.numpy() return yp
Ancestors (in MRO)
- PTLBase
- sklearn.base.BaseEstimator
- builtins.object
Static methods
def __init__(
self, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999)
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999): self.epochs = epochs self.batch_size = batch_size self.alpha = alpha self.beta1 = beta1 self.beta2 = beta2 self.net = None
def fit(
self, X, y, criterion)
Trains a neural network on provided data.
Parameters
X: iterable of size n_samples Representation of dataset.
y: iterable of size n_samples Representation of output
criterion: callable with 2 arguments, possibly a nn._Loss instance. Cost function to minimize.
Return
self
def fit(self, X, y, criterion): """ Trains a neural network on provided data. Parameters ---------- X: iterable of size n_samples Representation of dataset. y: iterable of size n_samples Representation of output criterion: callable with 2 arguments, possibly a nn._Loss instance. Cost function to minimize. Return ------ self """ check_X_y(X, y, allow_nd=True, dtype=None) self.net = self.make_architecture(X, y) optimizer = optim.Adam(self.net.parameters(), lr=self.alpha, betas=(self.beta1, self.beta2)) data = torch.utils.data.TensorDataset( torch.FloatTensor(X), torch.LongTensor(y) ) # this creates mixed batches trainloader = torch.utils.data.DataLoader( data, batch_size=self.batch_size, shuffle=True ) for epoch in range(self.epochs): # loop over the dataset multiple times for i, data in enumerate(trainloader, 0): # get the inputs inputs, labels = data # wrap them in Variable inputs, labels = Variable(inputs), Variable(labels) # zero the parameter gradients optimizer.zero_grad() # forward + backward + optimize outputs = self.net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() return self
def get_params(
self, deep=True)
Get parameters for this estimator.
Parameters
deep : boolean, optional If True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns
params : mapping of string to any Parameter names mapped to their values.
def get_params(self, deep=True): """Get parameters for this estimator. Parameters ---------- deep : boolean, optional If True, will return the parameters for this estimator and contained subobjects that are estimators. Returns ------- params : mapping of string to any Parameter names mapped to their values. """ out = dict() for key in self._get_param_names(): # We need deprecation warnings to always be on in order to # catch deprecated param values. # This is set in utils/__init__.py but it gets overwritten # when running under python3 somehow. warnings.simplefilter("always", DeprecationWarning) try: with warnings.catch_warnings(record=True) as w: value = getattr(self, key, None) if len(w) and w[0].category == DeprecationWarning: # if the parameter is deprecated, don't show it continue finally: warnings.filters.pop(0) # XXX: should we rather test if instance of estimator? if deep and hasattr(value, 'get_params'): deep_items = value.get_params().items() out.update((key + '__' + k, val) for k, val in deep_items) out[key] = value return out
def make_architecture(
self, X, y)
Should return nn.Module instance, which represents architecture of the neural network.
Parameters
X: iterable of size n_samples Representation of dataset.
y: iterable of size n_samples Representation of output
Return
net: an instance of a neural network to be trained.
@abstractmethod def make_architecture(self, X, y): """ Should return nn.Module instance, which represents architecture of the neural network. Parameters ---------- X: iterable of size n_samples Representation of dataset. y: iterable of size n_samples Representation of output Return ------ net: an instance of a neural network to be trained. """ pass
def predict(
self, X)
Make estimation with trained neural network.
Parameters
X: iterable of size n_samples Representation of inputs. Should be consistent with inputs in the training dataset.
Return
X: iterable of size n_samples Representation of estimated outputs.
def predict(self, X): """ Make estimation with trained neural network. Parameters ---------- X: iterable of size n_samples Representation of inputs. Should be consistent with inputs in the training dataset. Return ------ X: iterable of size n_samples Representation of estimated outputs. """ if self.net is None: raise RuntimeError("The model is not fit. Did you forget to call the fit method on a dataset?") X = Variable(torch.FloatTensor(X), volatile=True) yp = self.net(X).data.numpy() return yp
def set_params(
self, **params)
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it's possible to update each
component of a nested object.
Returns
self
def set_params(self, **params): """Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form ``<component>__<parameter>`` so that it's possible to update each component of a nested object. Returns ------- self """ if not params: # Simple optimization to gain speed (inspect is slow) return self valid_params = self.get_params(deep=True) nested_params = defaultdict(dict) # grouped by prefix for key, value in params.items(): key, delim, sub_key = key.partition('__') if key not in valid_params: raise ValueError('Invalid parameter %s for estimator %s. ' 'Check the list of available parameters ' 'with `estimator.get_params().keys()`.' % (key, self)) if delim: nested_params[key][sub_key] = value else: setattr(self, key, value) for key, sub_params in nested_params.items(): valid_params[key].set_params(**sub_params) return self
Instance variables
var alpha
var batch_size
var beta1
var beta2
var epochs
var net
class PTLClassifierBase
A base class for learning classifiers with pytorch.
Parameters
See parent classes for corresponding parameters.
class PTLClassifierBase(PTLBase, ClassifierMixin): """ A base class for learning classifiers with pytorch. Parameters ---------- See parent classes for corresponding parameters. """ def __init__(self, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999): super(PTLClassifierBase, self).__init__( epochs, batch_size, alpha, beta1, beta2 ) self.label_encoder = None def fit(self, X, y): """ Trains a classifier on provided data. Parameters ---------- X: iterable of size n_samples Representation of dataset. y: iterable of size n_samples Representation of classes Return ------ self """ # encode outputs self.label_encoder = LabelEncoder() y = self.label_encoder.fit_transform(y) criterion = nn.CrossEntropyLoss() super(PTLClassifierBase, self).fit(X, y, criterion) return self def predict(self, X): """ Estimate output classes. Parameters ---------- X: iterable of size n_samples Representation of inputs to classify. Return ------ y: iterable of size n_samples Representation of classes """ yp = super(PTLClassifierBase, self).predict(X) yp = np.argmax(yp, axis=1) yp = self.label_encoder.inverse_transform(yp) return yp
Ancestors (in MRO)
- PTLClassifierBase
- PTLBase
- sklearn.base.BaseEstimator
- sklearn.base.ClassifierMixin
- builtins.object
Static methods
def __init__(
self, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999)
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, epochs=10, batch_size=256, alpha=0.001, beta1=0.9, beta2=0.999): super(PTLClassifierBase, self).__init__( epochs, batch_size, alpha, beta1, beta2 ) self.label_encoder = None
def fit(
self, X, y)
Trains a classifier on provided data.
Parameters
X: iterable of size n_samples Representation of dataset.
y: iterable of size n_samples Representation of classes
Return
self
def fit(self, X, y): """ Trains a classifier on provided data. Parameters ---------- X: iterable of size n_samples Representation of dataset. y: iterable of size n_samples Representation of classes Return ------ self """ # encode outputs self.label_encoder = LabelEncoder() y = self.label_encoder.fit_transform(y) criterion = nn.CrossEntropyLoss() super(PTLClassifierBase, self).fit(X, y, criterion) return self
def get_params(
self, deep=True)
Get parameters for this estimator.
Parameters
deep : boolean, optional If True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns
params : mapping of string to any Parameter names mapped to their values.
def get_params(self, deep=True): """Get parameters for this estimator. Parameters ---------- deep : boolean, optional If True, will return the parameters for this estimator and contained subobjects that are estimators. Returns ------- params : mapping of string to any Parameter names mapped to their values. """ out = dict() for key in self._get_param_names(): # We need deprecation warnings to always be on in order to # catch deprecated param values. # This is set in utils/__init__.py but it gets overwritten # when running under python3 somehow. warnings.simplefilter("always", DeprecationWarning) try: with warnings.catch_warnings(record=True) as w: value = getattr(self, key, None) if len(w) and w[0].category == DeprecationWarning: # if the parameter is deprecated, don't show it continue finally: warnings.filters.pop(0) # XXX: should we rather test if instance of estimator? if deep and hasattr(value, 'get_params'): deep_items = value.get_params().items() out.update((key + '__' + k, val) for k, val in deep_items) out[key] = value return out
def make_architecture(
self, X, y)
Should return nn.Module instance, which represents architecture of the neural network.
Parameters
X: iterable of size n_samples Representation of dataset.
y: iterable of size n_samples Representation of output
Return
net: an instance of a neural network to be trained.
@abstractmethod def make_architecture(self, X, y): """ Should return nn.Module instance, which represents architecture of the neural network. Parameters ---------- X: iterable of size n_samples Representation of dataset. y: iterable of size n_samples Representation of output Return ------ net: an instance of a neural network to be trained. """ pass
def predict(
self, X)
Estimate output classes.
Parameters
X: iterable of size n_samples Representation of inputs to classify.
Return
y: iterable of size n_samples Representation of classes
def predict(self, X): """ Estimate output classes. Parameters ---------- X: iterable of size n_samples Representation of inputs to classify. Return ------ y: iterable of size n_samples Representation of classes """ yp = super(PTLClassifierBase, self).predict(X) yp = np.argmax(yp, axis=1) yp = self.label_encoder.inverse_transform(yp) return yp
def score(
self, X, y, sample_weight=None)
Returns the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Parameters
X : array-like, shape = (n_samples, n_features) Test samples.
y : array-like, shape = (n_samples) or (n_samples, n_outputs) True labels for X.
sample_weight : array-like, shape = [n_samples], optional Sample weights.
Returns
score : float Mean accuracy of self.predict(X) wrt. y.
def score(self, X, y, sample_weight=None): """Returns the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted. Parameters ---------- X : array-like, shape = (n_samples, n_features) Test samples. y : array-like, shape = (n_samples) or (n_samples, n_outputs) True labels for X. sample_weight : array-like, shape = [n_samples], optional Sample weights. Returns ------- score : float Mean accuracy of self.predict(X) wrt. y. """ from .metrics import accuracy_score return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
def set_params(
self, **params)
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it's possible to update each
component of a nested object.
Returns
self
def set_params(self, **params): """Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form ``<component>__<parameter>`` so that it's possible to update each component of a nested object. Returns ------- self """ if not params: # Simple optimization to gain speed (inspect is slow) return self valid_params = self.get_params(deep=True) nested_params = defaultdict(dict) # grouped by prefix for key, value in params.items(): key, delim, sub_key = key.partition('__') if key not in valid_params: raise ValueError('Invalid parameter %s for estimator %s. ' 'Check the list of available parameters ' 'with `estimator.get_params().keys()`.' % (key, self)) if delim: nested_params[key][sub_key] = value else: setattr(self, key, value) for key, sub_params in nested_params.items(): valid_params[key].set_params(**sub_params) return self
Instance variables
var label_encoder