Module: TensorStream::Maths::MathFunctions

Included in:
TensorStream::Maths, NN
Defined in:
lib/tensor_stream/math/math_ops.rb

Instance Method Summary collapse

Instance Method Details

#l2_normalize(x, axis: nil, epsilon: 1e-12, name: nil) ⇒ Object

Normalizes along dimension axis using an L2 norm.



10
11
12
13
14
15
16
17
# File 'lib/tensor_stream/math/math_ops.rb', line 10

def l2_normalize(x, axis: nil, epsilon: 1e-12, name: nil)
  TensorStream.name_scope(name, "l2_normalize", values: [x]) do |name|
    x = TensorStream.convert_to_tensor(x, name: "x")
    square_sum = TensorStream.reduce_sum(TensorStream.square(x), axis, keepdims: true)
    x_inv_norm = TensorStream.rsqrt(TensorStream.maximum(square_sum, epsilon))
    TensorStream.multiply(x, x_inv_norm, name: name)
  end
end