コード例 #1
0
    def fit(self, X, y, quiet=True):
        for epoch in range(100):
            running_loss = 0
            _skf = StratifiedKFold(n_splits=10, shuffle=True)
            for more, less in _skf.split(X, y):
                # Get data and labels
                inputs = torch.from_numpy(X[more])
                labels = torch.FloatTensor(np.array([y[more]]).T * 1.0)

                # Make sure positive and negative samples have the same number
                inputs = inputs[:torch.sum(labels).type(torch.int16) * 2]
                labels = labels[:torch.sum(labels).type(torch.int16) * 2]

                # wrap them in Variable
                inputs = Variable(inputs.cuda())
                labels = Variable(labels.cuda())

                # zero the parameter gradients
                self.optimizer.zero_grad()

                # forward + backward + optimize
                outputs = self.net(inputs)
                loss = self.criterion(outputs, labels)
                loss.backward()

                self.optimizer.step()

                running_loss += loss.data

            self.scheduler.step()

            if not quiet:
                if epoch % 10 == 0:
                    print(f'Epoch {epoch}: {running_loss}')
コード例 #2
0
 def predict(self, X):
     inputs = torch.from_numpy(X)
     inputs = Variable(inputs.cuda())
     return self.net(inputs)
コード例 #3
0
import sys
import pickle

# Computing -------------------------------------
import numpy as np
from sklearn.model_selection import StratifiedKFold

# Local tools -----------------------------------
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'tools'))  # noqa
from MEG_worker import MEG_Worker

# EEG net ---------------------------------------
import torchsummary
from eegnet import torch, nn, EEGNet, optim, Variable, evaluate
net = EEGNet().cuda()
print(net.forward(Variable(torch.Tensor(np.random.rand(1, 1, 61,
                                                       272)).cuda())))
torchsummary.summary(net, input_size=(1, 61, 272))

# Init parameters -------------------------------
# Init results_folder
RESULTS_FOLDER = os.path.join('.', 'results_eegnet')

# Make sure RESULTS_FOLDER is not a file
assert (not os.path.isfile(RESULTS_FOLDER))

# Mkdir if RESULTS_FOLDER does not exist
if not os.path.exists(RESULTS_FOLDER):
    os.mkdir(RESULTS_FOLDER)

# %%