Skip to content

Commit 2b0116d

Browse files
committed
added bitwise op examples into BasicMath*
1 parent fb45e09 commit 2b0116d

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

BasicMath/BasicMathProgram.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Gradient;
44
using tensorflow;
55
using tensorflow.core.protobuf.config_pb2;
6+
using tensorflow.python.ops.gen_bitwise_ops;
67
using tensorflow.summary;
78

89
static class BasicMathProgram {
@@ -16,6 +17,12 @@ static void Main() {
1617
Tensor sum = tf.add(a, b, name: "sum");
1718
Tensor div = tf.divide(a, b, name: "div");
1819

20+
Tensor x = tf.constant(0b101, name: "B101");
21+
Tensor y = tf.constant(0b011, name: "B011");
22+
23+
Tensor xor = tf.bitwise.bitwise_xor(x, y);
24+
Tensor bitcount = gen_bitwise_ops.population_count_dyn(xor);
25+
1926
dynamic config = config_pb2.ConfigProto.CreateInstance();
2027
// unless this is set, tensorflow-gpu consumes all of GPU memory
2128
// don't set it if you don't want you training to crash due to random OOM in the middle
@@ -27,6 +34,11 @@ static void Main() {
2734
Console.WriteLine($"b = {session.run(b)}");
2835
Console.WriteLine($"a + b = {session.run(sum)}");
2936
Console.WriteLine($"a / b = {session.run(div)}");
37+
Console.WriteLine();
38+
39+
string xorBinary = Convert.ToString(session.run(xor), toBase: 2).PadLeft(3, '0');
40+
Console.WriteLine($"101 ^ 011 = {xorBinary} with popcount: {session.run(bitcount)}");
41+
3042
writer.close();
3143
session.close();
3244
});

FSharp/BasicMathF/BasicMath.fs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open Gradient.BuiltIns
66
open tensorflow
77
open tensorflow.summary
88
open tensorflow.core.protobuf.config_pb2
9+
open tensorflow.python.ops.gen_bitwise_ops
910

1011
let inline (!>) (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit : ^a -> ^b) x)
1112
// F# does not use implicit conversions, when resolving an overload
@@ -28,6 +29,12 @@ let main argv =
2829
let sum = tf.add(a, b, name="sum")
2930
let div = tf.divide(a, b, name="div")
3031

32+
let x = tf.constant(0b101, name="B101")
33+
let y = tf.constant(0b010, name="B010")
34+
35+
let xor = tf.bitwise.bitwise_xor(x, y)
36+
let bitcount = gen_bitwise_ops.population_count_dyn(xor)
37+
3138
let config = !? config_pb2.ConfigProto ()
3239
config?gpu_options?allow_growth <- true
3340

@@ -37,6 +44,10 @@ let main argv =
3744
printfn "b = %O" (sess.run b)
3845
printfn "a + b = %O" (sess.run sum)
3946
printfn "a / b = %O" (sess.run div)
47+
printfn ""
48+
49+
let xorBinary = Convert.ToString(sess.run xor |> Dyn.implicitConvert<int>, toBase=2).PadLeft(3, '0')
50+
printfn "101 ^ 010 = %s with popcount: %O" xorBinary (sess.run bitcount)
4051

4152
writer.close()
4253
sess.close())

VB/BasicMathVB/BasicMath.vb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Imports Gradient
22
Imports SharPy.Runtime
33
Imports tensorflow
44
Imports tensorflow.core.protobuf.config_pb2
5+
Imports tensorflow.python.ops.gen_bitwise_ops
56
Imports tensorflow.summary
67

78
Module Program
@@ -15,7 +16,13 @@ Module Program
1516
Dim b = tf.constant(10.0, name:="b")
1617

1718
Dim sum = tf.add(a, b, name:="sum")
18-
Dim div = tf.div(a, b, name:="div")
19+
Dim div = tf.divide(a, b, name:="div")
20+
21+
Dim x = tf.constant(&B101, name:="B101")
22+
Dim y = tf.constant(&B110, name:="B110")
23+
24+
Dim x_or = tf.bitwise.bitwise_xor(x, y)
25+
Dim bitcount = gen_bitwise_ops.population_count_dyn(x_or)
1926

2027
' ConfigProto here must be wrapped in () to tell Visual Basic,
2128
' that .ConfigProto() is not the same as .ConfigProto.
@@ -31,6 +38,11 @@ Module Program
3138
Console.WriteLine($"b = { .run(b) }")
3239
Console.WriteLine($"a + b = { .run(sum) }")
3340
Console.WriteLine($"a / b = { .run(div) }")
41+
Console.WriteLine()
42+
43+
Dim xorBinary = Convert.ToString(.run(x_or), toBase:=2).PadLeft(3, "0"c)
44+
Console.WriteLine($"101 ^ 110 = {xorBinary} with popcount: { .run(bitcount)}")
45+
3446
writer.close()
3547
.close()
3648
End With

0 commit comments

Comments
 (0)