Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
### Perform training
net = skorch.NeuralNetClassifier(
module=wavetorch.WaveCell,
# Training configuration
max_epochs=cfg['training']['N_epochs'],
batch_size=cfg['training']['batch_size'],
lr=cfg['training']['lr'],
# train_split=skorch.dataset.CVSplit(cfg['training']['N_folds'], stratified=True, random_state=cfg['seed']),
optimizer=torch.optim.Adam,
criterion=torch.nn.CrossEntropyLoss,
callbacks=[
ClipDesignRegion,
skorch.callbacks.EpochScoring('accuracy', lower_is_better=False, on_train=True, name='train_acc'),
skorch.callbacks.Checkpoint(monitor=None, fn_prefix='1234_', dirname='test', f_params="params_{last_epoch[epoch]}.pt", f_optimizer='optimizer.pt', f_history='history.json')
],
callbacks__print_log__keys_ignored=None,
train_split=None,
# These al get passed as options to WaveCell
module__Nx=cfg['geom']['Nx'],
module__Ny=cfg['geom']['Ny'],
module__h=cfg['geom']['h'],
module__dt=cfg['geom']['dt'],
module__init=cfg['geom']['init'],
module__c0=cfg['geom']['c0'],
module__c1=cfg['geom']['c1'],
module__sigma=cfg['geom']['pml']['max'],
module__N=cfg['geom']['pml']['N'],
module__p=cfg['geom']['pml']['p'],
module__design_region=design_region,
help='Limit the input data to length N.')
parser.add_argument('--seed', type=int, default=1111,
help='random seed')
parser.add_argument('--no-cuda', dest='cuda', action='store_false',
help='use CUDA')
parser.add_argument('--save', type=str, default='model.pt',
help='path to save the final model')
args = parser.parse_args()
torch.manual_seed(args.seed)
corpus = data.Corpus(args.data)
ntokens = len(corpus.dictionary)
device = 'cuda' if args.cuda else 'cpu'
class LRAnnealing(skorch.callbacks.Callback):
def on_epoch_end(self, net, **kwargs):
if not net.history[-1]['valid_loss_best']:
net.lr /= 4.0
class ExamplePrinter(skorch.callbacks.Callback):
def on_epoch_end(self, net, **kwargs):
seed_sentence = "the meaning of"
indices = [corpus.dictionary.word2idx[n] for n in seed_sentence.split()]
indices = skorch.utils.to_tensor(
torch.LongTensor([indices]).t(), device=device)
sentence, _ = net.sample_n(num_words=10, input=indices)
print(seed_sentence,
" ".join([corpus.dictionary.idx2word[n] for n in sentence]))
def my_train_split(ds, y):
for reg_coef in [0, 100, 1e-2, 0.1, 1, 1e-3]:
print('Trying reg coef', reg_coef)
net = Net(
module=DFSGlimpseSingleObjectClassifier,
criterion=None,
max_epochs=50,
reg_coef=reg_coef,
optimizer=T.optim.RMSprop,
#optimizer__weight_decay=1e-4,
lr=1e-5,
batch_size=batch_size,
device='cuda' if USE_CUDA else 'cpu',
callbacks=[
Dump(),
skorch.callbacks.Checkpoint(monitor='acc_best'),
skorch.callbacks.ProgressBar(postfix_keys=['train_loss', 'valid_loss', 'acc', 'reg']),
skorch.callbacks.GradientNormClipping(0.01),
#skorch.callbacks.LRScheduler('ReduceLROnPlateau'),
],
iterator_train=data_generator,
iterator_train__shuffle=True,
iterator_valid=data_generator,
iterator_valid__shuffle=False,
)
#net.fit((mnist_train, mnist_valid), pretrain=True, epochs=50)
net.partial_fit((mnist_train, mnist_valid), pretrain=False, epochs=500)
" ".join([corpus.dictionary.idx2word[n] for n in sentence]))
def my_train_split(ds, y):
# Return (corpus.train, corpus.valid) in case the network
# is fitted using net.fit(corpus.train).
return ds, skorch.dataset.Dataset(corpus.valid[:200], y=None)
net = Net(
module=RNNModel,
max_epochs=args.epochs,
batch_size=args.batch_size,
device=device,
callbacks=[
skorch.callbacks.Checkpoint(),
skorch.callbacks.ProgressBar(),
LRAnnealing(),
ExamplePrinter()
],
module__rnn_type='LSTM',
module__ntoken=ntokens,
module__ninp=200,
module__nhid=200,
module__nlayers=2,
# Use (corpus.train, corpus.valid) as validation split.
# Even though we are doing a grid search, we use an internal
# validation set to determine when to save (Checkpoint callback)
# and when to decrease the learning rate (LRAnnealing callback).
train_split=my_train_split,
# To demonstrate that skorch is able to use already available
for reg_coef in [0, 100, 1e-2, 0.1, 1, 1e-3]:
print('Trying reg coef', reg_coef)
net = Net(
module=DFSGlimpseSingleObjectClassifier,
criterion=None,
max_epochs=50,
reg_coef=reg_coef,
optimizer=T.optim.RMSprop,
#optimizer__weight_decay=1e-4,
lr=1e-5,
batch_size=batch_size,
device='cuda' if USE_CUDA else 'cpu',
callbacks=[
Dump(),
skorch.callbacks.Checkpoint(monitor='acc_best'),
skorch.callbacks.ProgressBar(postfix_keys=['train_loss', 'valid_loss', 'acc', 'reg']),
skorch.callbacks.GradientNormClipping(0.01),
#skorch.callbacks.LRScheduler('ReduceLROnPlateau'),
],
iterator_train=data_generator,
iterator_train__shuffle=True,
iterator_valid=data_generator,
iterator_valid__shuffle=False,
)
#net.fit((mnist_train, mnist_valid), pretrain=True, epochs=50)
net.partial_fit((mnist_train, mnist_valid), pretrain=False, epochs=500)
print(seed_sentence,
" ".join([corpus.dictionary.idx2word[n] for n in sentence]))
def my_train_split(ds, y):
# Return (corpus.train, corpus.valid) in case the network
# is fitted using net.fit(corpus.train).
return ds, skorch.dataset.Dataset(corpus.valid[:200], y=None)
net = Net(
module=RNNModel,
max_epochs=args.epochs,
batch_size=args.batch_size,
device=device,
callbacks=[
skorch.callbacks.Checkpoint(),
skorch.callbacks.ProgressBar(),
LRAnnealing(),
ExamplePrinter()
],
module__rnn_type='LSTM',
module__ntoken=ntokens,
module__ninp=200,
module__nhid=200,
module__nlayers=2,
# Use (corpus.train, corpus.valid) as validation split.
# Even though we are doing a grid search, we use an internal
# validation set to determine when to save (Checkpoint callback)
# and when to decrease the learning rate (LRAnnealing callback).
train_split=my_train_split,
print('Trying reg coef', reg_coef)
net = Net(
module=DFSGlimpseSingleObjectClassifier,
criterion=None,
max_epochs=50,
reg_coef=reg_coef,
optimizer=T.optim.RMSprop,
#optimizer__weight_decay=1e-4,
lr=1e-5,
batch_size=batch_size,
device='cuda' if USE_CUDA else 'cpu',
callbacks=[
Dump(),
skorch.callbacks.Checkpoint(monitor='acc_best'),
skorch.callbacks.ProgressBar(postfix_keys=['train_loss', 'valid_loss', 'acc', 'reg']),
skorch.callbacks.GradientNormClipping(0.01),
#skorch.callbacks.LRScheduler('ReduceLROnPlateau'),
],
iterator_train=data_generator,
iterator_train__shuffle=True,
iterator_valid=data_generator,
iterator_valid__shuffle=False,
)
#net.fit((mnist_train, mnist_valid), pretrain=True, epochs=50)
net.partial_fit((mnist_train, mnist_valid), pretrain=False, epochs=500)
import sklearn
import skorch
class CroppedDataset(torch.utils.data.dataset.Dataset):
def __init__(self, dataset, indices):
self.dataset = dataset
self.indices = indices
def __getitem__(self, idx):
return self.dataset[self.indices[idx]]
def __len__(self):
return len(self.indices)
# TODO: move this into lib
class ClipDesignRegion(skorch.callbacks.Callback):
def on_batch_end(self, net, Xi=None, yi=None, training=None, **kwargs):
if training:
net.module_.clip_to_design_region()
parser = argparse.ArgumentParser()
parser.add_argument('config', type=str,
help='Configuration file for geometry, training, and data preparation')
parser.add_argument('--num_threads', type=int, default=4,
help='Number of threads to use')
parser.add_argument('--use-cuda', action='store_true',
help='Use CUDA to perform computations')
parser.add_argument('--name', type=str, default=time.strftime('%Y%m%d%H%M%S'),
help='Name to use when saving or loading the model file. If not specified when saving a time and date stamp is used')
parser.add_argument('--savedir', type=str, default='./study/',
help='Directory in which the model file is saved. Defaults to ./study/')
batch_size, n_steps, _ = y_pred.shape
if training:
#return F.cross_entropy(y_pred, y_true)
y_true = y_true[:, None].expand(batch_size, n_steps)
return F.cross_entropy(
y_pred.reshape(batch_size * n_steps, -1),
y_true.reshape(-1)
)
else:
y_prob, y_cls = y_pred.max(-1)
_, y_prob_maxind = y_prob.max(-1)
y_cls_final = y_cls.gather(1, y_prob_maxind[:, None])[:, 0]
return (y_cls_final == y_true).sum()
class Dump(skorch.callbacks.Callback):
def initialize(self):
self.epoch = 0
self.batch = 0
self.correct = 0
self.total = 0
self.best_acc = 0
self.nviz = 0
return self
def on_epoch_begin(self, net, **kwargs):
self.epoch += 1
self.batch = 0
self.correct = 0
self.total = 0
self.nviz = 0