Skip to content

Commit fac99ea

Browse files
dedaleOceania2018
authored andcommitted
[F#] Add BasicOperations
1 parent de89d4e commit fac99ea

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

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

src/TensorFlowNET.Examples.FSharp/Program.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ let main argv =
8787

8888
let examples =
8989
//FunctionApproximation.run() // Still needs updates
90-
[ LinearRegression.Example
90+
[ BasicOperations.Example
91+
LinearRegression.Example
9192
LinearRegressionEager.Example ]
9293
|> List.sortBy (fun e -> e.Config.Priority)
9394

src/TensorFlowNET.Examples.FSharp/TensorFlowNET.Examples.FSharp.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<ItemGroup>
1010
<Compile Include="SciSharpExample.fs" />
11+
<Compile Include="BasicOperations.fs" />
1112
<Compile Include="BasicModels\LinearRegression.fs" />
1213
<Compile Include="BasicModels\LinearRegressionEager.fs" />
1314
<Compile Include="FunctionApproximation.fs" />

0 commit comments

Comments
 (0)