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

Readability improvements in higher-order templates #97

Closed
mratsim opened this issue Oct 8, 2017 · 0 comments
Closed

Readability improvements in higher-order templates #97

mratsim opened this issue Oct 8, 2017 · 0 comments

Comments

@mratsim
Copy link
Owner

mratsim commented Oct 8, 2017

Higher-order templates cover parts of #31.

However their syntax is hard to read, for example:

proc sigmoid_cross_entropy_backward*[T](
        gradient: Tensor[T] or T,
        cached_tensor: Tensor[T],
        target: Tensor[T]
        ): Tensor[T] {.inline.} =
  let batch_size = cached_tensor.shape[^1]

  # Deal with scalar and tensor gradient
  when gradient is T:
    let grad = gradient
  elif gradient is Tensor:
    let grad = gradient.data[gradient.offset]

  return map2T(cached_tensor, target, grad * (1 / (1 + exp(-x)) - y) / T(batch_size))

The last part would be more readable as:

result = map2T(cached_tensor, target):
  grad * (1 / (1 + exp(-x)) - y) / T(batch_size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant