Example #1
0
def train_mnist(args):
    # write tensorflow models
    x = tf.placeholder(tf.float32, [args.batch_size, 784])
    t = tf.placeholder(tf.float32, [args.batch_size, 10])
    w = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))
    y = tf.matmul(x, w) + b
    cost = tf.reduce_mean(
        -tf.reduce_sum(t * tf.log(tf.nn.softmax(y)), reduction_indices=[1]))
    init = tf.initialize_all_variables()

    # import graph_def
    with tf.Session() as sess:
        graph_def = sess.graph_def
    importer = TFImporter()
    importer.parse_graph_def(graph_def)

    # get handle of ngraph ops
    x_ng, t_ng, cost_ng, init_op_ng = importer.get_op_handle(
        [x, t, cost, init])

    # transformer and computations
    transformer = ngt.make_transformer()
    updates = SGDOptimizer(args.lrate).minimize(cost_ng)
    train_comp = transformer.computation([cost_ng, updates], x_ng, t_ng)
    init_comp = transformer.computation(init_op_ng)
    transformer.initialize()

    # train
    mnist = input_data.read_data_sets(args.data_dir, one_hot=True)
    init_comp()
    for idx in range(args.max_iter):
        batch_xs, batch_ys = mnist.train.next_batch(args.batch_size)
        cost_val, _ = train_comp(batch_xs, batch_ys)
        print("[Iter %s] Cost = %s" % (idx, cost_val))

    # train in tensorflow as comparison
    with tf.Session() as sess:
        # train in tensorflow
        train_step = tf.train.GradientDescentOptimizer(
            args.lrate).minimize(cost)
        sess.run(init)

        mnist = input_data.read_data_sets(args.data_dir, one_hot=True)
        for idx in range(args.max_iter):
            batch_xs, batch_ys = mnist.train.next_batch(args.batch_size)
            cost_val, _ = sess.run([cost, train_step],
                                   feed_dict={
                                       x: batch_xs,
                                       t: batch_ys
                                   })
            print("[Iter %s] Cost = %s" % (idx, cost_val))
Example #2
0
    def test_mnist_softmax(self):
        # parameters
        max_iter = 10
        lrate = 0.1
        bsz = 128

        # write tensorflow models
        x = tf.placeholder(tf.float32, [bsz, 784])
        t = tf.placeholder(tf.float32, [bsz, 10])
        W = tf.Variable(tf.zeros([784, 10]))
        b = tf.Variable(tf.zeros([10]))
        y = tf.matmul(x, W) + b
        cost = tf.reduce_mean(-tf.reduce_sum(
            t * tf.log(tf.nn.softmax(y)), reduction_indices=[1]))
        init_op = tf.initialize_all_variables()

        # import graph_def
        with tf.Session() as sess:
            graph_def = sess.graph_def
        importer = TFImporter()
        importer.parse_graph_def(graph_def)

        # get handle of ngraph ops
        x_ng, t_ng, cost_ng, init_op_ng = importer.get_op_handle(
            [x, t, cost, init_op])

        # transformer and computations
        transformer = ngt.make_transformer()
        updates = SGDOptimizer(lrate).minimize(cost_ng)
        train_comp = transformer.computation([cost_ng, updates], x_ng, t_ng)
        init_comp = transformer.computation(init_op_ng)
        transformer.initialize()

        # train
        mnist = input_data.read_data_sets('/tmp/data', one_hot=True)
        init_comp()
        ng_costs = []
        for idx in range(max_iter):
            batch_xs, batch_ys = mnist.train.next_batch(bsz)
            cost_val, _ = train_comp(batch_xs, batch_ys)
            print("[Iter %s] Cost = %s" % (idx, cost_val))
            ng_costs.append(float(cost_val))

        # train in tensorflow as comparison
        with tf.Session() as sess:
            # train in tensorflow
            train_step = tf.train.GradientDescentOptimizer(lrate).minimize(
                cost)
            sess.run(init_op)

            mnist = input_data.read_data_sets('/tmp/data', one_hot=True)
            tf_costs = []
            for idx in range(max_iter):
                batch_xs, batch_ys = mnist.train.next_batch(bsz)
                cost_val, _ = sess.run([cost, train_step],
                                       feed_dict={x: batch_xs,
                                                  t: batch_ys})
                print("[Iter %s] Cost = %s" % (idx, cost_val))
                tf_costs.append(cost_val)

        # check results
        assert np.allclose(
            np.asarray(tf_costs).astype(np.float32),
            np.asarray(ng_costs).astype(np.float32))
Example #3
0
lrate = 0.1

# placeholders
x = tf.placeholder(tf.float32, shape=(4, 3))
t = tf.placeholder(tf.float32, shape=(4, 1))
w = tf.Variable(tf.zeros([3, 1]))
y = tf.nn.sigmoid(tf.matmul(x, w))
log_likelihoods = tf.log(y) * t + tf.log(1 - y) * (1 - t)
cost = -tf.reduce_sum(log_likelihoods)
init_op = tf.initialize_all_variables()

# import graph_def
with tf.Session() as sess:
    graph_def = sess.graph_def
importer = TFImporter()
importer.parse_graph_def(graph_def)

# get handle of ngraph ops
x_ng, t_ng, cost_ng, init_op_ng = importer.get_op_handle([x, t, cost, init_op])

# transformer and computations
transformer = ngt.make_transformer()
updates = SGDOptimizer(lrate).minimize(cost_ng)
train_comp = transformer.computation([cost_ng, updates], x_ng, t_ng)
init_comp = transformer.computation(init_op_ng)
transformer.initialize()

# train
init_comp()
for idx in range(max_iter):
    loss_val, _ = train_comp(xs_np, ys_np)
Example #4
0
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------

from __future__ import print_function
from ngraph.frontends.tensorflow.tf_importer.importer import TFImporter
import tensorflow as tf
import ngraph.transformers as ngt

# tensorflow ops
x = tf.constant(1.)
y = tf.constant(2.)
f = x + y

# import
importer = TFImporter()
importer.parse_graph_def(tf.Session().graph_def)

# get handle
f_ng = importer.get_op_handle(f)

# execute
f_result = ngt.make_transformer().computation(f_ng)()
print(f_result)