Class: TensorStream::NN
- Inherits:
-
Object
show all
- Extended by:
- OpHelper
- Defined in:
- lib/tensor_stream/nn/nn_ops.rb
Overview
High level machine learning functions
Class Method Summary
collapse
Methods included from OpHelper
_op, cons, dtype_eval, format_source, fp_type?, i_cons, i_op, shape_eval, val_to_dtype
Class Method Details
.relu(features, name: nil) ⇒ Object
9
10
11
|
# File 'lib/tensor_stream/nn/nn_ops.rb', line 9
def self.relu(features, name: nil)
TensorStream.max(features, 0, name: "relu_#{name}")
end
|
.sigmoid(input, name: nil) ⇒ Object
13
14
15
|
# File 'lib/tensor_stream/nn/nn_ops.rb', line 13
def self.sigmoid(input, name: nil)
TensorStream.sigmoid(input, name)
end
|
.sigmoid_cross_entropy_with_logits(labels: nil, logits: nil, name: nil) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/tensor_stream/nn/nn_ops.rb', line 28
def self.sigmoid_cross_entropy_with_logits(labels: nil, logits: nil, name: nil)
TensorStream.name_scope(name, default: 'logistic_loss', values: [logits, labels]) do |name|
tf = TensorStream
logits = tf.convert_to_tensor(logits, name: 'logits')
labels = tf.convert_to_tensor(labels, name: 'labels')
zeros = tf.zeros_like(logits, dtype: logits.dtype)
cond = (logits >= zeros)
relu_logits = tf.where(cond, logits, zeros)
neg_abs_logits = tf.where(cond, -logits, logits)
return tf.add(
relu_logits - logits * labels,
tf.log1p(tf.exp(neg_abs_logits)),
name: name)
end
end
|
.softmax(logits, axis: nil, name: nil) ⇒ Object
5
6
7
|
# File 'lib/tensor_stream/nn/nn_ops.rb', line 5
def self.softmax(logits, axis: nil, name: nil)
_op(:softmax, logits, nil, axis: axis, name: name)
end
|
.softmax_cross_entropy_with_logits(labels: nil, logits: nil, name: nil) ⇒ Object
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/tensor_stream/nn/nn_ops.rb', line 17
def self.softmax_cross_entropy_with_logits(labels: nil, logits: nil, name: nil)
TensorStream.name_scope(name, default: 'softmax_cross_entropy_with_logits', values: [logits, labels]) do |name|
tf = TensorStream
logits = tf.convert_to_tensor(logits, name: 'logits')
labels = tf.convert_to_tensor(labels, name: 'labels')
labels = tf.cast(labels, logits.dtype)
softmax_logits = -tf.log(softmax(logits)) * labels
tf.reduce_sum(softmax_logits, tf.rank(logits) - 1)
end
end
|