Skip to content

Commit

Permalink
added bitwise op examples into BasicMath*
Browse files Browse the repository at this point in the history
  • Loading branch information
lostmsu committed Jan 16, 2020
1 parent fb45e09 commit 2b0116d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
12 changes: 12 additions & 0 deletions BasicMath/BasicMathProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Gradient;
using tensorflow;
using tensorflow.core.protobuf.config_pb2;
using tensorflow.python.ops.gen_bitwise_ops;
using tensorflow.summary;

static class BasicMathProgram {
Expand All @@ -16,6 +17,12 @@ static class BasicMathProgram {
Tensor sum = tf.add(a, b, name: "sum");
Tensor div = tf.divide(a, b, name: "div");

Tensor x = tf.constant(0b101, name: "B101");
Tensor y = tf.constant(0b011, name: "B011");

Tensor xor = tf.bitwise.bitwise_xor(x, y);
Tensor bitcount = gen_bitwise_ops.population_count_dyn(xor);

dynamic config = config_pb2.ConfigProto.CreateInstance();
// unless this is set, tensorflow-gpu consumes all of GPU memory
// don't set it if you don't want you training to crash due to random OOM in the middle
Expand All @@ -27,6 +34,11 @@ static class BasicMathProgram {
Console.WriteLine($"b = {session.run(b)}");
Console.WriteLine($"a + b = {session.run(sum)}");
Console.WriteLine($"a / b = {session.run(div)}");
Console.WriteLine();
string xorBinary = Convert.ToString(session.run(xor), toBase: 2).PadLeft(3, '0');
Console.WriteLine($"101 ^ 011 = {xorBinary} with popcount: {session.run(bitcount)}");
writer.close();
session.close();
});
Expand Down
11 changes: 11 additions & 0 deletions FSharp/BasicMathF/BasicMath.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open Gradient.BuiltIns
open tensorflow
open tensorflow.summary
open tensorflow.core.protobuf.config_pb2
open tensorflow.python.ops.gen_bitwise_ops

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

let x = tf.constant(0b101, name="B101")
let y = tf.constant(0b010, name="B010")

let xor = tf.bitwise.bitwise_xor(x, y)
let bitcount = gen_bitwise_ops.population_count_dyn(xor)

let config = !? config_pb2.ConfigProto ()
config?gpu_options?allow_growth <- true

Expand All @@ -37,6 +44,10 @@ let main argv =
printfn "b = %O" (sess.run b)
printfn "a + b = %O" (sess.run sum)
printfn "a / b = %O" (sess.run div)
printfn ""

let xorBinary = Convert.ToString(sess.run xor |> Dyn.implicitConvert<int>, toBase=2).PadLeft(3, '0')
printfn "101 ^ 010 = %s with popcount: %O" xorBinary (sess.run bitcount)

writer.close()
sess.close())
Expand Down
14 changes: 13 additions & 1 deletion VB/BasicMathVB/BasicMath.vb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Imports Gradient
Imports SharPy.Runtime
Imports tensorflow
Imports tensorflow.core.protobuf.config_pb2
Imports tensorflow.python.ops.gen_bitwise_ops
Imports tensorflow.summary

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

Dim sum = tf.add(a, b, name:="sum")
Dim div = tf.div(a, b, name:="div")
Dim div = tf.divide(a, b, name:="div")

Dim x = tf.constant(&B101, name:="B101")
Dim y = tf.constant(&B110, name:="B110")

Dim x_or = tf.bitwise.bitwise_xor(x, y)
Dim bitcount = gen_bitwise_ops.population_count_dyn(x_or)

' ConfigProto here must be wrapped in () to tell Visual Basic,
' that .ConfigProto() is not the same as .ConfigProto.
Expand All @@ -31,6 +38,11 @@ Module Program
Console.WriteLine($"b = { .run(b) }")
Console.WriteLine($"a + b = { .run(sum) }")
Console.WriteLine($"a / b = { .run(div) }")
Console.WriteLine()

Dim xorBinary = Convert.ToString(.run(x_or), toBase:=2).PadLeft(3, "0"c)
Console.WriteLine($"101 ^ 110 = {xorBinary} with popcount: { .run(bitcount)}")

writer.close()
.close()
End With
Expand Down

0 comments on commit 2b0116d

Please sign in to comment.