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

Directly constructing a Tensor #15

Closed
zeroexcuses opened this issue Jul 21, 2019 · 7 comments
Closed

Directly constructing a Tensor #15

zeroexcuses opened this issue Jul 21, 2019 · 7 comments

Comments

@zeroexcuses
Copy link

In PyTorch, we can do:

x = torch.tensor([5.5, 3])
print(x)
Out:

tensor([5.5000, 3.0000])

Is there a corresponding function in OCaml-Torch? I don't see the corresponding function in Torch. or Torch.Tensor .

Thanks!

@LaurentMazare
Copy link
Owner

Sure, you can run the following code.

open Torch;;
let x = Tensor.of_float1 [| 5.5; 3. |];;
Tensor.print x;;

Fwiw for playing with the library I often use utop by just running dune utop at the root of the github repo. You can then try various functions and have some nice code completion. You can also use the ocaml-jupyter kernel but it requires slightly more set up.
When writing code in an editor, merlin is a must have for completion, it allows you to easily discover the functions that you're looking for.

@zeroexcuses
Copy link
Author

  1. Thanks, code worked.

  2. I have "dune utop" running, not at the top of the github repo, but in a separate directory with a dune file of

(executable
 (name scratch)
 (libraries base torch torch.core stdio))
  1. dune top is running inside tmux, which I can send commands to from Vim via vim-slime.

  2. I also have merlin/syntastic installed + working inside vim.

I definitely don't have a fluid workflow though. Here are issues I run into:

  1. When I type in Tensor. and hit C-x C-o for auto complete, it returns for me 1014 auto completion results. Mentally, I'm looking for one titled "tensor" or "from_slice" -- not "of_float." Secondly, because there's "of_float0, of_float1, of_float2, of_float3", it looks like auto generated internal functions, not functions I should call.

  2. I'm not sure if this is a problem of merlin or VIM -- but in my auto completion, it only shows the function name, not the type signature -- which is unfortunate, as reading the type signature would help alot.

  3. Of course, the type signature of tensor.ml functions are in tensor.mli, but I haven't quite figured outa fluid way to switch the two yet. There's ':MerlinTypeOf', but I haven't quite got a hang of it yet.

what would really help

Most of the time, I'd really like to be able to filter functions by type signature. So in this particular case, if I could say "list me all functions that take a float array as one of it's arguments, and returns a Tensor as output" that would drastically narrow everything. I'm not sure existing tools allow me to do this.

@LaurentMazare
Copy link
Owner

Cool, you seem to already have a nice setup. I do get the types with merlin in vim and that's very helpful.
Re (5), there are some ocamldoc comments in src/torch/tensor.mli that describe what of_floatX is doing. I don't get them in merlin though, maybe it's possible to set this up.

@zeroexcuses
Copy link
Author

I do get the types with merlin in vim and that's very helpful.

Interesting. Are you (1) getting this during auto completion, where it shows the type signature of each potential completion or (2) getting this outside of auto completion, when cursor is over some identifier / expr ?

I can achieve (2) via :MerlinTypeOf, but I can't figure out how to do (1).

@LaurentMazare
Copy link
Owner

I get both (1) and (2), (1) looks like the following for me. I don't remember having to do anything special to get this to work (and I also get the types when using the completion in jupyter notebooks).
image

@zeroexcuses
Copy link
Author

share

Weird, I'm not getting type signatures yet. Will look into why.

@zeroexcuses
Copy link
Author

It turns out "Tensor." C-x C-o => names, but no types
"Tensor.X" C-x C-o where X xis any char => names with types

So now (1) also "works" for me. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants