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

Added tensor support for strings and for getting TF_DataType #7

Merged
merged 1 commit into from
May 25, 2018

Conversation

anshuman23
Copy link
Owner

@josevalim, In this PR I have added some functions that were working perfectly. However I have actually been devoting a lot of time to getting TF_INT64 and TF_FLOAT tensors (especially multi-dimensional ones) to work in Elixir. I will add PRs for those soon, once I get them to work completely.

In this PR, the functionalities added are:

  • Support for string tensors
  • Getting to know the datatype of a tensor (Will be useful later on, when Tensor support is varied across many datatypes. I have already added support for all datatypes in this function)

An example for these functions is as follows:

iex(1)> tensor = Tensorflex.string_tensor "string tensors work!"
#Reference<0.697833995.47579139.50516>

iex(2)> tensor2 = Tensorflex.string_tensor 123
{:error, :non_binary_argument}

iex(3)> Tensorflex.tensor_datatype tensor
{:ok, :tf_string}

@anshuman23 anshuman23 merged commit 972f53c into master May 25, 2018
memcpy((void *) tensor_resource_alloc, (void *) &tensor, sizeof(TF_Tensor *));
ERL_NIF_TERM new_tensor = enif_make_resource(env, tensor_resource_alloc);
enif_release_resource(tensor_resource_alloc);
return new_tensor;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are returning {:error, ...} in failure cases, it is recommended to return {:ok, tensor} in success cases.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. In the next PR, I'll add this. I'll extend it to read_graph too

TF_Tensor **tensor_resource_alloc = enif_alloc_resource(tensor_resource, sizeof(TF_Tensor *));

if (!(enif_is_binary(env, argv[0]))) {
return enif_make_tuple2(env, enif_make_atom(env,"error"), enif_make_atom(env,"non_binary_argument"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the error happens because of bad input, it is preferrable to just raise an error: maybe we can use enif_make_badarg? http://erlang.org/doc/man/erl_nif.html#enif_make_badarg

In a nutshell, we return tuple errors for semantic errors: for example, you couldn't load a file because it is corrupt or it doesn't exist. For language errors, such as invalid syntax or invalid arguments, we should raise instead.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. Will incorporate this in the next PR as well

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

Successfully merging this pull request may close these issues.

2 participants