-
Notifications
You must be signed in to change notification settings - Fork 103
/
BasicOperations.fs
49 lines (37 loc) · 1.39 KB
/
BasicOperations.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace TensorFlowNET.Examples.FSharp
open type Tensorflow.Binding
module BasicOperations =
let private run() =
tf.enable_eager_execution()
// Define tensor constants.
let a = tf.constant(2)
let b = tf.constant(3)
let c = tf.constant(5)
// Various tensor operations.
// Note: Tensors also support operators (+, *, ...)
let add = tf.add(a, b)
let sub = tf.subtract(a, b)
let mul = tf.multiply(a, b)
let div = tf.divide(a, b)
// Access tensors value.
print("add =", add.numpy())
print("sub =", sub.numpy())
print("mul =", mul.numpy())
print("div =", div.numpy())
// Some more operations.
let mean = tf.reduce_mean([| a; b; c |])
let sum = tf.reduce_sum([| a; b; c |])
// Access tensors value.
print("mean =", mean.numpy())
print("sum =", sum.numpy())
// Matrix multiplications.
let matrix1 = tf.constant(array2D [ [ 1; 2 ]; [ 3; 4 ] ])
let matrix2 = tf.constant(array2D [ [ 5; 6 ]; [ 7; 8 ] ])
let product = tf.matmul(matrix1, matrix2)
// Convert Tensor to Numpy.
print("product =", product.numpy())
true
let Example =
{ Config = ExampleConfig.Create ("Basic Operations", priority = 2)
Run = run
}