Skip to content
Permalink
Browse files
added bitwise op examples into BasicMath*
  • Loading branch information
lostmsu committed Jan 16, 2020
1 parent fb45e09 commit 2b0116d21a770e8bd7497ef3d759b3703925074e
Showing with 36 additions and 1 deletion.
  1. +12 −0 BasicMath/BasicMathProgram.cs
  2. +11 −0 FSharp/BasicMathF/BasicMath.fs
  3. +13 −1 VB/BasicMathVB/BasicMath.vb
@@ -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 {
@@ -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
@@ -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();
});
@@ -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
@@ -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

@@ -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())
@@ -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
@@ -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.
@@ -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

0 comments on commit 2b0116d

Please sign in to comment.