Skip to content

How to turn several rust floats into a list of numpy of another list for final python usage? #228

@dbsxdbsx

Description

@dbsxdbsx

I working on rust side, and need to interact with python, and the shape of the parameter used for python function is like this:

 (1, [array([13.5], dtype=float32), array([349.3], dtype=float32), array([220.1], dtype=float32)])

It is a tuple(or list) of a combination of an int and a list of numpy.

My full rust code is like this:

let action1 = i64::from(action_tensor.get(0));//.to_object(self.py);
            let action2 = f32::from(action_tensor.get(1));//.to_object(self.py);
            let action3 = f32::from(action_tensor.get(2));//.to_object(self.py);
            let action4 = f32::from(action_tensor.get(3));//.to_object(self.py);

            let py_array = vec![action2].to_pyarray(self.py);

            let action2 = action2
                .call_method0(self.py, "numpy")
                .unwrap()
                .call_method1(self.py, "reshape", ("(1,)",))
                .unwrap();
            let action3 = action3
                .call_method0(self.py, "numpy")
                .unwrap()
                .call_method1(self.py, "reshape", ("(1,)",))
                .unwrap();
            let action4 = action4
                .call_method0(self.py, "numpy")
                .unwrap()
                .call_method1(self.py, "reshape", ("(1,)",))
                .unwrap();

            let np_lst = vec![action2, action3, action4];
            let action_tuple = (action1, np_lst);
            // let tuple2 = (action2, action3, action4);
            // let action = (action1, tuple2);
            // let x = i64::from(action_tensor.get(2));
            // tuple
            // let action = Vec::<i64>::from(action_tensor).to_object(self.py);
            self.env
                // .call_method1("step", tuple1)
                .call_method1("step", (action_tuple,))
                // .call_method1("step", (action,))
                // .call_method1("step", (&action,))
                .expect("call step method failed")

The action_tensor above is of type Tensor of crate https://github.com/LaurentMazare/tch-rs.
And the above code failed, since no way to turn a float instance in rust to call method numpy, but pure python version is worked, like this:

t = torch.tensor([1.0,13.5, 349.3,220.1], dtype=torch.float32)
act2 = t[1].numpy().reshape((1,))
act3 = t[2].numpy().reshape((1,))
act4 = t[3].numpy().reshape((1,))
action = (1, [act2, act3, act4])
print(action)
observation, reward, done, info = env.step(action)

I am quite at a loss. First, I tried to not use rust-numpy (with only pyo3), but failed, then I tried to work it out with crate rust-numpy, but I can't find a clear discription on this case, so I fallen into loss again...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions