question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: string indices must be integers

See original GitHub issue

I am using a custom batch function

def custom_prepare_batch(batch, device, non_blocking):
    x, y = batch["img"], batch["lab"]
    return (
        convert_tensor(x, device=device, non_blocking=non_blocking),
        convert_tensor(y, device=device, non_blocking=non_blocking),
    )

and my processing function is as follows:

def train_function(unused_engine, batch):
        images, targets = custom_prepare_batch(batch, device=device, non_blocking=True)
#         images, targets = batch["img"], batch["lab"]
    
        model.train()
        optimizer.zero_grad()

        y_preds = model(images)

        if agreement_threshold > 0.0:
            # The "batch_size" in this function refers to the batch size per env
            # Since we treat every example as one env, we should set the parameter
            # n_agreement_envs equal to batch size
            mean_loss, masks = and_mask_utils.get_grads(
                agreement_threshold=agreement_threshold,
                batch_size=1,
                loss_fn=criterion,
                n_agreement_envs=batch_size,
                params=optimizer.param_groups[0]['params'],
                output=y_preds,
                target=targets,
                method="and_mask",
                scale_grad_inverse_sparsity=scale_grad_inverse_sparsity,
            )
        else:
            mean_loss = criterion(y_pred, y)
            mean_loss.backward()

        optimizer.step()

        return y_preds, targets

and I am also passing the custom batch function as follows train_evaluator = create_supervised_evaluator(model, device=device, non_blocking=True, prepare_batch=custom_prepare_batch)

when I run my code

trainer_rng = np.random.RandomState()
trainer.run(next(iter(train_loader)), max_epochs=2,
                seed=trainer_rng.randint(2 ** 32))

I get the error

Current run is terminating due to exception: string indices must be integers.
Engine run is terminating due to exception: string indices must be integers.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-099d5afd435f> in <module>
     32 
     33 trainer_rng = np.random.RandomState()
---> 34 trainer.run(next(iter(train_loader)), max_epochs=2,
     35                 seed=trainer_rng.randint(2 ** 32))

~/.conda/envs/mila/lib/python3.8/site-packages/ignite/engine/engine.py in run(self, data, max_epochs, epoch_length, seed)
    689 
    690         self.state.dataloader = data
--> 691         return self._internal_run()
    692 
    693     @staticmethod

~/.conda/envs/mila/lib/python3.8/site-packages/ignite/engine/engine.py in _internal_run(self)
    760             self._dataloader_iter = None
    761             self.logger.error("Engine run is terminating due to exception: %s.", str(e))
--> 762             self._handle_exception(e)
    763 
    764         self._dataloader_iter = None

~/.conda/envs/mila/lib/python3.8/site-packages/ignite/engine/engine.py in _handle_exception(self, e)
    465             self._fire_event(Events.EXCEPTION_RAISED, e)
    466         else:
--> 467             raise e
    468 
    469     @property

~/.conda/envs/mila/lib/python3.8/site-packages/ignite/engine/engine.py in _internal_run(self)
    728                     self._setup_engine()
    729 
--> 730                 time_taken = self._run_once_on_dataset()
    731                 # time is available for handlers but must be update after fire
    732                 self.state.times[Events.EPOCH_COMPLETED.name] = time_taken

~/.conda/envs/mila/lib/python3.8/site-packages/ignite/engine/engine.py in _run_once_on_dataset(self)
    826         except Exception as e:
    827             self.logger.error("Current run is terminating due to exception: %s.", str(e))
--> 828             self._handle_exception(e)
    829 
    830         return time.time() - start_time

~/.conda/envs/mila/lib/python3.8/site-packages/ignite/engine/engine.py in _handle_exception(self, e)
    465             self._fire_event(Events.EXCEPTION_RAISED, e)
    466         else:
--> 467             raise e
    468 
    469     @property

~/.conda/envs/mila/lib/python3.8/site-packages/ignite/engine/engine.py in _run_once_on_dataset(self)
    809                 self.state.iteration += 1
    810                 self._fire_event(Events.ITERATION_STARTED)
--> 811                 self.state.output = self._process_function(self, self.state.batch)
    812                 self._fire_event(Events.ITERATION_COMPLETED)
    813 

<ipython-input-12-0c14fe7efe33> in train_function(unused_engine, batch)
      1 def train_function(unused_engine, batch):
----> 2         images, targets = custom_prepare_batch(batch, device=device, non_blocking=True)
      3 #         images, targets = batch["img"], batch["lab"]
      4 
      5         model.train()

<ipython-input-11-206b9292729e> in custom_prepare_batch(batch, device, non_blocking)
      1 def custom_prepare_batch(batch, device, non_blocking):
----> 2     x, y = batch["img"], batch["lab"]
      3     return (
      4         convert_tensor(x, device=device, non_blocking=non_blocking),
      5         convert_tensor(y, device=device, non_blocking=non_blocking),

TypeError: string indices must be integers

Kindly, assist me. Thank you.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
etettehcommented, Jan 30, 2021

Thank you. The code ran successfully after implementing your suggestions.

1reaction
vfdev-5commented, Jan 30, 2021

I think the issue is related to the way you pass the data: next(iter(train_loader)) which looks a bit strange. Maybe, you can pass directly train_loader ?

Btw: looks like you are using v0.3.0, please think to migrate to v0.4.2

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why am I seeing "TypeError: string indices must be integers"?
The variable item is a string. An index looks like this: >>> mystring = 'helloworld' >>> print mystring[0] 'h'. The above example uses...
Read more >
Python typeerror: string indices must be integers Solution
String indices must be integers. This means that when you're accessing an iterable object like a string, you must do it using a...
Read more >
TypeError: string indices must be integers
TypeError : string indices must be integers means an attempt to access a location within a string using an index that is not...
Read more >
Typeerror: string indices must be integers – How to Fix in Python
This error happens when working with Python strings for two main reasons – using a string instead of an index number (integer) when...
Read more >
How to Fix TypeError: String Indices must be Integers in Python
Typeerror : string indices must be integers error in Python occurs when we are attempting to access a value from an iterable using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found