pytorch 0.4 updates
Esse commit está contido em:
@@ -142,37 +142,37 @@ class Deep4Net(BaseModel):
|
||||
model.add_module('conv_classifier',
|
||||
nn.Conv2d(self.n_filters_4, self.n_classes,
|
||||
(self.final_conv_length, 1), bias=True))
|
||||
model.add_module('softmax', nn.LogSoftmax())
|
||||
model.add_module('softmax', nn.LogSoftmax(dim=1))
|
||||
model.add_module('squeeze', Expression(_squeeze_final_output))
|
||||
|
||||
# Initialization, xavier is same as in our paper...
|
||||
# was default from lasagne
|
||||
init.xavier_uniform(model.conv_time.weight, gain=1)
|
||||
init.xavier_uniform_(model.conv_time.weight, gain=1)
|
||||
# maybe no bias in case of no split layer and batch norm
|
||||
if self.split_first_layer or (not self.batch_norm):
|
||||
init.constant(model.conv_time.bias, 0)
|
||||
init.constant_(model.conv_time.bias, 0)
|
||||
if self.split_first_layer:
|
||||
init.xavier_uniform(model.conv_spat.weight, gain=1)
|
||||
init.xavier_uniform_(model.conv_spat.weight, gain=1)
|
||||
if not self.batch_norm:
|
||||
init.constant(model.conv_spat.bias, 0)
|
||||
init.constant_(model.conv_spat.bias, 0)
|
||||
if self.batch_norm:
|
||||
init.constant(model.bnorm.weight, 1)
|
||||
init.constant(model.bnorm.bias, 0)
|
||||
init.constant_(model.bnorm.weight, 1)
|
||||
init.constant_(model.bnorm.bias, 0)
|
||||
param_dict = dict(list(model.named_parameters()))
|
||||
for block_nr in range(2,5):
|
||||
conv_weight = param_dict['conv_{:d}.weight'.format(block_nr)]
|
||||
init.xavier_uniform(conv_weight, gain=1)
|
||||
init.xavier_uniform_(conv_weight, gain=1)
|
||||
if not self.batch_norm:
|
||||
conv_bias = param_dict['conv_{:d}.bias'.format(block_nr)]
|
||||
init.constant(conv_bias, 0)
|
||||
init.constant_(conv_bias, 0)
|
||||
else:
|
||||
bnorm_weight = param_dict['bnorm_{:d}.weight'.format(block_nr)]
|
||||
bnorm_bias = param_dict['bnorm_{:d}.bias'.format(block_nr)]
|
||||
init.constant(bnorm_weight, 1)
|
||||
init.constant(bnorm_bias, 0)
|
||||
init.constant_(bnorm_weight, 1)
|
||||
init.constant_(bnorm_bias, 0)
|
||||
|
||||
init.xavier_uniform(model.conv_classifier.weight, gain=1)
|
||||
init.constant(model.conv_classifier.bias, 0)
|
||||
init.xavier_uniform_(model.conv_classifier.weight, gain=1)
|
||||
init.constant_(model.conv_classifier.bias, 0)
|
||||
|
||||
# Start in eval mode
|
||||
model.eval()
|
||||
|
||||
@@ -83,23 +83,23 @@ class ShallowFBCSPNet(BaseModel):
|
||||
model.add_module('conv_classifier',
|
||||
nn.Conv2d(n_filters_conv, self.n_classes,
|
||||
(self.final_conv_length, 1), bias=True))
|
||||
model.add_module('softmax', nn.LogSoftmax())
|
||||
model.add_module('softmax', nn.LogSoftmax(dim=1))
|
||||
model.add_module('squeeze', Expression(_squeeze_final_output))
|
||||
|
||||
# Initialization, xavier is same as in paper...
|
||||
init.xavier_uniform(model.conv_time.weight, gain=1)
|
||||
init.xavier_uniform_(model.conv_time.weight, gain=1)
|
||||
# maybe no bias in case of no split layer and batch norm
|
||||
if self.split_first_layer or (not self.batch_norm):
|
||||
init.constant(model.conv_time.bias, 0)
|
||||
init.constant_(model.conv_time.bias, 0)
|
||||
if self.split_first_layer:
|
||||
init.xavier_uniform(model.conv_spat.weight, gain=1)
|
||||
init.xavier_uniform_(model.conv_spat.weight, gain=1)
|
||||
if not self.batch_norm:
|
||||
init.constant(model.conv_spat.bias, 0)
|
||||
init.constant_(model.conv_spat.bias, 0)
|
||||
if self.batch_norm:
|
||||
init.constant(model.bnorm.weight, 1)
|
||||
init.constant(model.bnorm.bias, 0)
|
||||
init.xavier_uniform(model.conv_classifier.weight, gain=1)
|
||||
init.constant(model.conv_classifier.bias, 0)
|
||||
init.constant_(model.bnorm.weight, 1)
|
||||
init.constant_(model.bnorm.bias, 0)
|
||||
init.xavier_uniform_(model.conv_classifier.weight, gain=1)
|
||||
init.constant_(model.conv_classifier.bias, 0)
|
||||
|
||||
return model
|
||||
|
||||
|
||||
@@ -15,9 +15,9 @@ def glorot_weight_zero_bias(model):
|
||||
for module in model.modules():
|
||||
if hasattr(module, 'weight'):
|
||||
if not ('BatchNorm' in module.__class__.__name__):
|
||||
init.xavier_uniform(module.weight, gain=1)
|
||||
init.xavier_uniform_(module.weight, gain=1)
|
||||
else:
|
||||
init.constant(module.weight, 1)
|
||||
if hasattr(module, 'bias'):
|
||||
if module.bias is not None:
|
||||
init.constant(module.bias, 0)
|
||||
init.constant_(module.bias, 0)
|
||||
|
||||
@@ -45,9 +45,10 @@ class CosineAnnealing(object):
|
||||
[0], self.update_period_boundaries))
|
||||
|
||||
def get_lr(self, initial_val, i_update):
|
||||
# i_update + 1 in error message since first update is i_update=0
|
||||
assert i_update < self.update_period_boundaries[-1], (
|
||||
"More updates ({:d}) than expected ({:d})".format(
|
||||
i_update, self.update_period_boundaries[-1] - 1))
|
||||
i_update + 1, self.update_period_boundaries[-1]))
|
||||
i_end_period = np.searchsorted(self.update_period_boundaries,
|
||||
i_update, side='right')
|
||||
assert i_end_period > 0
|
||||
|
||||
@@ -4,12 +4,13 @@ import numpy as np
|
||||
import random
|
||||
|
||||
|
||||
def np_to_var(X, requires_grad=False, dtype=None, pin_memory=False, **var_kwargs):
|
||||
def np_to_var(X, requires_grad=False, dtype=None, pin_memory=False,
|
||||
**tensor_kwargs):
|
||||
"""
|
||||
Convenience function to transform numpy array to `torch.autograd.Variable`.
|
||||
|
||||
Convenience function to transform numpy array to `torch.Tensor`.
|
||||
|
||||
Converts `X` to ndarray using asarray if necessary.
|
||||
|
||||
|
||||
Parameters
|
||||
----------
|
||||
X: ndarray or list or number
|
||||
@@ -19,30 +20,29 @@ def np_to_var(X, requires_grad=False, dtype=None, pin_memory=False, **var_kwargs
|
||||
dtype: numpy dtype, optional
|
||||
var_kwargs:
|
||||
passed on to Variable constructor
|
||||
|
||||
|
||||
Returns
|
||||
-------
|
||||
var: `torch.autograd.Variable`
|
||||
var: `torch.Tensor`
|
||||
"""
|
||||
if not hasattr(X, '__len__'):
|
||||
X = [X]
|
||||
X = np.asarray(X)
|
||||
if dtype is not None:
|
||||
X = X.astype(dtype)
|
||||
X_tensor = th.from_numpy(X)
|
||||
X_tensor = th.tensor(X, requires_grad=requires_grad, **tensor_kwargs)
|
||||
if pin_memory:
|
||||
X_tensor = X_tensor.pin_memory()
|
||||
return Variable(X_tensor, requires_grad=requires_grad, **var_kwargs)
|
||||
return X_tensor
|
||||
|
||||
|
||||
def var_to_np(var):
|
||||
"""Convenience function to transform `torch.autograd.Variable` to numpy
|
||||
"""Convenience function to transform `torch.Tensor` to numpy
|
||||
array.
|
||||
|
||||
|
||||
Should work both for CPU and GPU."""
|
||||
return var.cpu().data.numpy()
|
||||
|
||||
|
||||
def set_random_seeds(seed, cuda):
|
||||
"""
|
||||
Set seeds for python random module numpy.random and torch.
|
||||
|
||||
Referência em uma Nova Issue
Bloquear um usuário