Index 2 is out of bounds for dimension 1 with size 2 #7494
Replies: 1 comment 3 replies
-
Hi @Applewine33, thanks for your interest here. Thanks. |
Beta Was this translation helpful? Give feedback.
-
Hi @Applewine33, thanks for your interest here. Thanks. |
Beta Was this translation helpful? Give feedback.
-
I try to run this code while when it run to "loss = loss_function(outputs, labels)" will get error and print this RuntimeError "Index 2 is out of bounds for dimension 1 with size 2". How can I fix it?
max_epochs = 100
val_interval = 2
best_metric = -1
best_metric_epoch = -1
epoch_loss_values = []
metric_values = []
post_pred = Compose([AsDiscrete(argmax=True, to_onehot=2)])
post_label = Compose([AsDiscrete(to_onehot=2)])
for epoch in range(max_epochs):
print("-" * 10)
print(f"epoch {epoch + 1}/{max_epochs}")
model.train()
epoch_loss = 0
step = 0
for batch_data in train_loader:
step += 1
inputs, labels = (
batch_data["image"].to(device),
batch_data["label"].to(device),
)
optimizer.zero_grad()
outputs = model(inputs)
loss = loss_function(outputs, labels)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
print(f"{step}/{len(train_ds) // train_loader.batch_size}, " f"train_loss: {loss.item():.4f}")
epoch_loss /= step
epoch_loss_values.append(epoch_loss)
print(f"epoch {epoch + 1} average loss: {epoch_loss:.4f}")
Beta Was this translation helpful? Give feedback.
All reactions