Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug Expected object of backend CUDA but got backend CPU for argu… #191

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion seq2seq/evaluator/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def evaluate(self, model, data):
match = 0
total = 0

device = None if torch.cuda.is_available() else -1
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
batch_iterator = torchtext.data.BucketIterator(
dataset=data, batch_size=self.batch_size,
sort=True, sort_key=lambda x: len(x.src),
Expand Down
13 changes: 5 additions & 8 deletions seq2seq/trainer/supervised_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ def __init__(self, expt_dir='experiment', loss=NLLLoss(), batch_size=64,
os.makedirs(self.expt_dir)
self.batch_size = batch_size

self.logger = logging.getLogger(__name__)

def _train_batch(self, input_variable, input_lengths, target_variable, model, teacher_forcing_ratio):
loss = self.loss
# Forward propagation
Expand All @@ -67,12 +65,11 @@ def _train_batch(self, input_variable, input_lengths, target_variable, model, te

def _train_epoches(self, data, model, n_epochs, start_epoch, start_step,
dev_data=None, teacher_forcing_ratio=0):
log = self.logger

print_loss_total = 0 # Reset every print_every
epoch_loss_total = 0 # Reset every epoch

device = None if torch.cuda.is_available() else -1
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
batch_iterator = torchtext.data.BucketIterator(
dataset=data, batch_size=self.batch_size,
sort=False, sort_within_batch=True,
Expand All @@ -85,7 +82,7 @@ def _train_epoches(self, data, model, n_epochs, start_epoch, start_step,
step = start_step
step_elapsed = 0
for epoch in range(start_epoch, n_epochs + 1):
log.debug("Epoch: %d, Step: %d" % (epoch, step))
logging.debug("Epoch: %d, Step: %d" % (epoch, step))

batch_generator = batch_iterator.__iter__()
# consuming seen batches from previous training
Expand Down Expand Up @@ -113,7 +110,7 @@ def _train_epoches(self, data, model, n_epochs, start_epoch, start_step,
step / total_steps * 100,
self.loss.name,
print_loss_avg)
log.info(log_msg)
logging.info(log_msg)

# Checkpoint
if step % self.checkpoint_every == 0 or step == total_steps:
Expand All @@ -136,7 +133,7 @@ def _train_epoches(self, data, model, n_epochs, start_epoch, start_step,
else:
self.optimizer.update(epoch_loss_avg, epoch)

log.info(log_msg)
logging.info(log_msg)

def train(self, model, data, num_epochs=5,
resume=False, dev_data=None,
Expand Down Expand Up @@ -179,7 +176,7 @@ def train(self, model, data, num_epochs=5,
optimizer = Optimizer(optim.Adam(model.parameters()), max_grad_norm=5)
self.optimizer = optimizer

self.logger.info("Optimizer: %s, Scheduler: %s" % (self.optimizer.optimizer, self.optimizer.scheduler))
logging.info("Optimizer: %s, Scheduler: %s" % (self.optimizer.optimizer, self.optimizer.scheduler))

self._train_epoches(data, model, num_epochs,
start_epoch, step, dev_data=dev_data,
Expand Down