Tomasz Kaczmarski
Tomasz Kaczmarski

Reputation: 136

how do I map tensor using dictionary

im trying to map tensor using dictionary but envir_with_agent[1,2] return tensor(4) instead 4 and dictionary cannot map it correctly min code is below

 envir_with_agent = b.mountain.clone()
envir_with_agent[b.position_agent[0], b.position_agent[1]] = 4
print(envir_with_agent[1,2])
print(b.dict_map_display[envir_with_agent[1,2]])

dict

        self.dict_map_display={ 
                            1:'.',
                            2:'o',
                            3:'O',
                            4:'A',
                            8:'E', 
                            9:'X'}

error


KeyError Traceback (most recent call last) in 2 envir_with_agent[b.position_agent[0], b.position_agent[1]] = 4 3 print(envir_with_agent[1,2]) ----> 4 print(b.dict_map_display[envir_with_agent[1,2]])

KeyError: tensor(4)

Upvotes: 0

Views: 924

Answers (2)

Nerveless_child
Nerveless_child

Reputation: 1412

You would want to first convert the scalar tensor to type int before using it as an index; like this:

envir_with_agent = b.mountain.clone()
envir_with_agent[int(b.position_agent[0]), int(b.position_agent[1])] = 4

Upvotes: 1

Ivan
Ivan

Reputation: 40618

You first need to convert the tensors to integers, provided b.position_agent is of dtype long:

envir_with_agent[b.position_agent[0].item(), b.position_agent[1].item()] = 4

Upvotes: 0

Related Questions