<filename>main.py
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#ac
#     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.
# ==============================================================================

"""Simple, end-to-end, LeNet-5-like convolutional MNIST model example.

This should achieve a test error of 0.7%. Please keep this model as simple and
linear as possible, it is meant as a tutorial for simple convolutional models.
Run with --self_test on the command line to execute a short self-test.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import gzip
import os
import sys
import time

import numpy as np
from six.moves import urllib
from six.moves import xrange  # pylint: disable=redefined-builtin
import tensorflow as tf
from settings import *
#import datasets.mnist as mnist
# CVDF mirror of http://yann.lecun.com/exdb/mnist/
SOURCE_URL = 'https://storage.googleapis.com/cvdf-datasets/mnist/'
WORK_DIRECTORY = 'data'
IMAGE_SIZE = 28
NUM_CHANNELS = 1
PIXEL_DEPTH = 255
NUM_LABELS = 10
VALIDATION_SIZE = 5000  # Size of the validation set.
SEED = None  # Set to None for random seed.
BATCH_SIZE = 16
NUM_EPOCHS =200
EVAL_BATCH_SIZE = 16
EVAL_FREQUENCY = 100  # Number of steps between evaluations.


FLAGS = None

def _dense_to_one_hot(labels, num_classes):
  """Convert class labels from scalars to one-hot vectors."""
  num_labels = labels.shape[0]
  index_offset = np.arange(num_labels) * num_classes
  labels_one_hot = np.zeros(
    shape=(num_labels, num_classes), dtype=np.uint8
  )
  labels_one_hot.flat[index_offset + labels.ravel()] = 1
  return labels_one_hot

def data_type():
  """Return the type of the activations, weights, and placeholder variables."""
  if FLAGS.use_fp16:
    return tf.float16
  else:
    return tf.float32


def fake_data(num_images):
  """Generate a fake dataset that matches the dimensions of MNIST."""
  data = np.ndarray(
      shape=(num_images, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS),
      dtype=np.float32)
  labels = np.zeros(shape=(num_images,), dtype=np.int64)
  for image in xrange(num_images):
    label = image % 2
    data[image, :, :, 0] = label - 0.5
    labels[image] = label
  return data, labels


def error_rate(predictions, labels):
  """Return the error rate based on dense predictions and sparse labels."""
  return 100.0 - (
      100.0 *
      np.sum(np.argmax(predictions, 1) == np.argmax(labels,1)) /
      predictions.shape[0])


def main(_):
  if FLAGS.self_test:
    print('Running self-test.')
    train_data, train_labels = fake_data(256)
    validation_data, validation_labels = fake_data(EVAL_BATCH_SIZE)
    test_data, test_labels = fake_data(EVAL_BATCH_SIZE)
    num_epochs = 1
  else:
    # Extract it into np arrays.
    (train_data,train_labels),(test_data,test_labels) = tf.keras.datasets.mnist.load_data(path='mnist.npz')
    # (train_data,train_labels),(test_data,test_labels) = tf.keras.datasets.cifar10.load_data()
    if np.rank(train_data)==3:
        train_data = np.expand_dims(train_data,axis=-1)
        test_data = np.expand_dims(test_data,axis=-1)
    #train_data = extract_data(train_data_filename, 60000)
    #train_labels = extract_labels(train_labels_filename, 60000)
    #test_data = extract_data(test_data_filename, 10000)
    #test_labels = extract_labels(test_labels_filename, 10000)

    # Generate a validation set.
    validation_data = train_data[:VALIDATION_SIZE, ...]
    validation_labels = train_labels[:VALIDATION_SIZE]
    train_data = train_data[VALIDATION_SIZE:, ...]
    train_labels = train_labels[VALIDATION_SIZE:]
    num_epochs = NUM_EPOCHS

    # convert labels to one-hot
    train_labels = _dense_to_one_hot(train_labels,NUM_LABELS)
    test_labels = _dense_to_one_hot(test_labels,NUM_LABELS)
    validation_labels = _dense_to_one_hot(validation_labels,NUM_LABELS)

  train_size = train_labels.shape[0]

    
  # This is where training samples and labels are fed to the graph.
  # These placeholder nodes will be fed a batch of training data at each
  # training step using the {feed_dict} argument to the Run() call below.
  train_data_node = tf.placeholder(
      data_type(),
      shape=(BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS))
  train_labels_node = tf.placeholder(tf.int64, shape=(BATCH_SIZE,NUM_LABELS))

  eval_data = tf.placeholder(
      data_type(),
      shape=(EVAL_BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS))
  eval_label = tf.placeholder(tf.int64, shape=(EVAL_BATCH_SIZE,NUM_LABELS))
  # We will replicate the model structure for the training subgraph, as well
  # as the evaluation subgraphs, while sharing the trainable parameters.
  epsilon = 1e-9
  def model(data, label,train=False):
    
    with tf.variable_scope('model',reuse=tf.AUTO_REUSE):
      logits,recon=capsules.nets.capsules_v0(
      data, label,num_classes=10, iterations=1, name='capsulesEM-V0'
    )
      #activations = tf.nn.relu(activations)
      #logits_sum = tf.reduce_sum(activations,axis=1,keepdims=True)
      #logits_sum = tf.tile(logits_sum,[1,NUM_LABELS])+epsilon
      #logits = activations / logits_sum
      #logits = tf.log((logits+epsilon)/ (1-logits + epsilon)) + epsilon
      #logits = tf.reshape(activations,shape=[activations.shape[0],-1])
      #logits = tf.layers.dense(logits,units=10)
      #return tf.nn.softmax(activations+1e-9,axis=-1)
      return logits,recon
      #return logits
  # Training computation: logits + cross-entropy loss.
  logits,recon = model(train_data_node,train_labels_node, True)
  #loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
  #    labels=train_labels_node, logits=logits))
  
  #loss = tf.reduce_mean(-tf.reduce_sum(logits * tf.log(tf.one_hot(train_labels_node,NUM_LABELS)+epsilon), reduction_indices=[1]))

  #loss = capsules.nets.spread_loss(
  #    train_labels_node, logits, margin=1.0, name='spread_loss'
  # )
  
  loss = tf.reduce_mean(
      tf.nn.softmax_cross_entropy_with_logits(
         labels=train_labels_node, logits=logits, name='cross_entropy_loss'
      )
    )
  loss = loss #+tf.reduce_mean((recon-train_data_node)**2)*1e-2
  number_params=np.sum([np.product([xi.value for xi in x.get_shape()]) for x in tf.all_variables()])
  print("Number of parameters:{}".format(number_params//1e3),"K")
  # Optimizer: set up a variable that's incremented once per batch and
  # controls the learning rate decay.
  batch = tf.Variable(0, dtype=data_type())
  # Decay once per epoch, using an exponential schedule starting at 0.01.
  learning_rate = tf.train.exponential_decay(
      0.001,                # Base learning rate.
      batch * BATCH_SIZE,  # Current index into the dataset.
      train_size,          # Decay step.
      0.95,                # Decay rate.
      staircase=True)
  # Use simple momentum for the optimization.
  optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss,
                                                       global_step=batch)

  # Predictions for the current training minibatch.
  train_prediction = tf.nn.softmax(logits)

  # Predictions for the test and validation, which we'll compute less often.
  eval_prediction = tf.nn.softmax(model(eval_data,eval_label)[0])

  # Small utility function to evaluate a dataset by feeding batches of data to
  # {eval_data} and pulling the results from {eval_predictions}.
  # Saves memory and enables this to run on smaller GPUs.
  def eval_in_batches(data, sess):
    """Get all predictions for a dataset by running it in small batches."""
    size = data.shape[0]
    if size < EVAL_BATCH_SIZE:
      raise ValueError("batch size for evals larger than dataset: %d" % size)
    predictions = np.ndarray(shape=(size, NUM_LABELS), dtype=np.float32)
    for begin in range(0, size, EVAL_BATCH_SIZE):
      end = begin + EVAL_BATCH_SIZE
      if end <= size:
        predictions[begin:end, :] = sess.run(
            eval_prediction,
            feed_dict={eval_data: data[begin:end, ...]})
      else:
        batch_predictions = sess.run(
            eval_prediction,
            feed_dict={eval_data: data[-EVAL_BATCH_SIZE:, ...]})
        predictions[begin:, :] = batch_predictions[begin - size:, :]
    return predictions

  # Create a local session to run the training.
  start_time = time.time()
  saver = tf.train.Saver()

  # create log files
  try:
    os.stat("./eval_log")
  except:
    os.mkdir("eval_log")
  minibatch_file = open("./eval_log/minibatch_error.txt", 'w')
  validation_file = open("./eval_log/validation_error.txt", 'w')
  test_file = open("./eval_log/test_error.txt","w")

  with tf.Session() as sess:
    # Run all the initializers to prepare the trainable parameters.
    tf.global_variables_initializer().run()
    print('Initialized!')
    # Loop through training steps.
    for step in xrange(int(num_epochs * train_size) // BATCH_SIZE):
      # Compute the offset of the current minibatch in the data.
      # Note that we could use better randomization across epochs.
      offset = (step * BATCH_SIZE) % (train_size - BATCH_SIZE)
      batch_data = train_data[offset:(offset + BATCH_SIZE), ...]
      batch_labels = train_labels[offset:(offset + BATCH_SIZE)]
      # This dictionary maps the batch data (as a np array) to the
      # node in the graph it schould be fed to.
      feed_dict = {train_data_node: batch_data,
                   train_labels_node: batch_labels}
      # Run the optimizer to update weights.
      sess.run(optimizer, feed_dict=feed_dict)
      # print some extra information once reach the evaluation frequency
      if step % EVAL_FREQUENCY == 0:
        # tf.Print([batch_data[0,...]],logits)
        # fetch some extra nodes' data
        l, lr, predictions = sess.run([loss, learning_rate, train_prediction],
                                      feed_dict=feed_dict)
        elapsed_time = time.time() - start_time
        start_time = time.time()
        print('Step %d (epoch %.2f), %.1f ms' %
              (step, float(step) * BATCH_SIZE / train_size,
               1000 * elapsed_time / EVAL_FREQUENCY))
        print('Minibatch loss: %.3f, learning rate: %.6f' % (l, lr))

        minibatch_error = error_rate(predictions, batch_labels)
        print('Minibatch error: %.1f%%' % minibatch_error)

        validation_error = error_rate(
            eval_in_batches(validation_data, sess), validation_labels)
        print('Validation error: %.1f%%' % validation_error)

        # test_error = error_rate(eval_in_batches(test_data, sess), test_labels)
        # print('Test error: %.1f%%' % test_error)

        minibatch_file.write(str(minibatch_error) + '\n')
        minibatch_file.flush()

        validation_file.write(str(validation_error) + '\n')
        validation_file.flush()
        # test_file.write(str(test_error) + '\n')

        sys.stdout.flush()

      if step%10000 ==0 :
        saver.save(sess,"./my_train_log/step.{}.ckpt".format(step))
        test_error = error_rate(eval_in_batches(test_data, sess), test_labels)
        test_file.write(str(test_error)+'\n')
        test_file.flush()
        sys.stdout.flush()
    # Finally print the result!
    test_error = error_rate(eval_in_batches(test_data, sess), test_labels)
    print('Test error: %.1f%%' % test_error)
    if FLAGS.self_test:
      print('test_error', test_error)
      assert test_error == 0.0, 'expected 0.0 test_error, got %.2f' % (
          test_error,)


if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '--use_fp16',
      default=False,
      help='Use half floats instead of full floats if True.',
      action='store_true')
  parser.add_argument(
      '--self_test',
      default=False,
      action='store_true',
      help='True if running a self test.')

  FLAGS, unparsed = parser.parse_known_args()
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
  app = QApplication(sys.argv)
  app.aboutToQuit.connect(app.deleteLater)



<filename>main.py
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#ac
#     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.
# ==============================================================================

"""Simple, end-to-end, LeNet-5-like convolutional MNIST model example.

This should achieve a test error of 0.7%. Please keep this model as simple and
linear as possible, it is meant as a tutorial for simple convolutional models.
Run with --self_test on the command line to execute a short self-test.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import gzip
import os
import sys
import time

import numpy as np
from six.moves import urllib
from six.moves import xrange  # pylint: disable=redefined-builtin
import tensorflow as tf
from settings import *
#import datasets.mnist as mnist
# CVDF mirror of http://yann.lecun.com/exdb/mnist/
SOURCE_URL = 'https://storage.googleapis.com/cvdf-datasets/mnist/'
WORK_DIRECTORY = 'data'
IMAGE_SIZE = 28
NUM_CHANNELS = 1
PIXEL_DEPTH = 255
NUM_LABELS = 10
VALIDATION_SIZE = 5000  # Size of the validation set.
SEED = None  # Set to None for random seed.
BATCH_SIZE = 16
NUM_EPOCHS =200
EVAL_BATCH_SIZE = 16
EVAL_FREQUENCY = 100  # Number of steps between evaluations.


FLAGS = None

def _dense_to_one_hot(labels, num_classes):
  """Convert class labels from scalars to one-hot vectors."""
  num_labels = labels.shape[0]
  index_offset = np.arange(num_labels) * num_classes
  labels_one_hot = np.zeros(
    shape=(num_labels, num_classes), dtype=np.uint8
  )
  labels_one_hot.flat[index_offset + labels.ravel()] = 1
  return labels_one_hot

def data_type():
  """Return the type of the activations, weights, and placeholder variables."""
  if FLAGS.use_fp16:
    return tf.float16
  else:
    return tf.float32


def fake_data(num_images):
  """Generate a fake dataset that matches the dimensions of MNIST."""
  data = np.ndarray(
      shape=(num_images, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS),
      dtype=np.float32)
  labels = np.zeros(shape=(num_images,), dtype=np.int64)
  for image in xrange(num_images):
    label = image % 2
    data[image, :, :, 0] = label - 0.5
    labels[image] = label
  return data, labels


def error_rate(predictions, labels):
  """Return the error rate based on dense predictions and sparse labels."""
  return 100.0 - (
      100.0 *
      np.sum(np.argmax(predictions, 1) == np.argmax(labels,1)) /
      predictions.shape[0])


def main(_):
  if FLAGS.self_test:
    print('Running self-test.')
    train_data, train_labels = fake_data(256)
    validation_data, validation_labels = fake_data(EVAL_BATCH_SIZE)
    test_data, test_labels = fake_data(EVAL_BATCH_SIZE)
    num_epochs = 1
  else:
    # Extract it into np arrays.
    (train_data,train_labels),(test_data,test_labels) = tf.keras.datasets.mnist.load_data(path='mnist.npz')
    # (train_data,train_labels),(test_data,test_labels) = tf.keras.datasets.cifar10.load_data()
    if np.rank(train_data)==3:
        train_data = np.expand_dims(train_data,axis=-1)
        test_data = np.expand_dims(test_data,axis=-1)
    #train_data = extract_data(train_data_filename, 60000)
    #train_labels = extract_labels(train_labels_filename, 60000)
    #test_data = extract_data(test_data_filename, 10000)
    #test_labels = extract_labels(test_labels_filename, 10000)

    # Generate a validation set.
    validation_data = train_data[:VALIDATION_SIZE, ...]
    validation_labels = train_labels[:VALIDATION_SIZE]
    train_data = train_data[VALIDATION_SIZE:, ...]
    train_labels = train_labels[VALIDATION_SIZE:]
    num_epochs = NUM_EPOCHS

    # convert labels to one-hot
    train_labels = _dense_to_one_hot(train_labels,NUM_LABELS)
    test_labels = _dense_to_one_hot(test_labels,NUM_LABELS)
    validation_labels = _dense_to_one_hot(validation_labels,NUM_LABELS)

  train_size = train_labels.shape[0]

    
  # This is where training samples and labels are fed to the graph.
  # These placeholder nodes will be fed a batch of training data at each
  # training step using the {feed_dict} argument to the Run() call below.
  train_data_node = tf.placeholder(
      data_type(),
      shape=(BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS))
  train_labels_node = tf.placeholder(tf.int64, shape=(BATCH_SIZE,NUM_LABELS))

  eval_data = tf.placeholder(
      data_type(),
      shape=(EVAL_BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS))
  eval_label = tf.placeholder(tf.int64, shape=(EVAL_BATCH_SIZE,NUM_LABELS))
  # We will replicate the model structure for the training subgraph, as well
  # as the evaluation subgraphs, while sharing the trainable parameters.
  epsilon = 1e-9
  def model(data, label,train=False):
    
    with tf.variable_scope('model',reuse=tf.AUTO_REUSE):
      logits,recon=capsules.nets.capsules_v0(
      data, label,num_classes=10, iterations=1, name='capsulesEM-V0'
    )
      #activations = tf.nn.relu(activations)
      #logits_sum = tf.reduce_sum(activations,axis=1,keepdims=True)
      #logits_sum = tf.tile(logits_sum,[1,NUM_LABELS])+epsilon
      #logits = activations / logits_sum
      #logits = tf.log((logits+epsilon)/ (1-logits + epsilon)) + epsilon
      #logits = tf.reshape(activations,shape=[activations.shape[0],-1])
      #logits = tf.layers.dense(logits,units=10)
      #return tf.nn.softmax(activations+1e-9,axis=-1)
      return logits,recon
      #return logits
  # Training computation: logits + cross-entropy loss.
  logits,recon = model(train_data_node,train_labels_node, True)
  #loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
  #    labels=train_labels_node, logits=logits))
  
  #loss = tf.reduce_mean(-tf.reduce_sum(logits * tf.log(tf.one_hot(train_labels_node,NUM_LABELS)+epsilon), reduction_indices=[1]))

  #loss = capsules.nets.spread_loss(
  #    train_labels_node, logits, margin=1.0, name='spread_loss'
  # )
  
  loss = tf.reduce_mean(
      tf.nn.softmax_cross_entropy_with_logits(
         labels=train_labels_node, logits=logits, name='cross_entropy_loss'
      )
    )
  loss = loss #+tf.reduce_mean((recon-train_data_node)**2)*1e-2
  number_params=np.sum([np.product([xi.value for xi in x.get_shape()]) for x in tf.all_variables()])
  print("Number of parameters:{}".format(number_params//1e3),"K")
  # Optimizer: set up a variable that's incremented once per batch and
  # controls the learning rate decay.
  batch = tf.Variable(0, dtype=data_type())
  # Decay once per epoch, using an exponential schedule starting at 0.01.
  learning_rate = tf.train.exponential_decay(
      0.001,                # Base learning rate.
      batch * BATCH_SIZE,  # Current index into the dataset.
      train_size,          # Decay step.
      0.95,                # Decay rate.
      staircase=True)
  # Use simple momentum for the optimization.
  optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss,
                                                       global_step=batch)

  # Predictions for the current training minibatch.
  train_prediction = tf.nn.softmax(logits)

  # Predictions for the test and validation, which we'll compute less often.
  eval_prediction = tf.nn.softmax(model(eval_data,eval_label)[0])

  # Small utility function to evaluate a dataset by feeding batches of data to
  # {eval_data} and pulling the results from {eval_predictions}.
  # Saves memory and enables this to run on smaller GPUs.
  def eval_in_batches(data, sess):
    """Get all predictions for a dataset by running it in small batches."""
    size = data.shape[0]
    if size < EVAL_BATCH_SIZE:
      raise ValueError("batch size for evals larger than dataset: %d" % size)
    predictions = np.ndarray(shape=(size, NUM_LABELS), dtype=np.float32)
    for begin in range(0, size, EVAL_BATCH_SIZE):
      end = begin + EVAL_BATCH_SIZE
      if end <= size:
        predictions[begin:end, :] = sess.run(
            eval_prediction,
            feed_dict={eval_data: data[begin:end, ...]})
      else:
        batch_predictions = sess.run(
            eval_prediction,
            feed_dict={eval_data: data[-EVAL_BATCH_SIZE:, ...]})
        predictions[begin:, :] = batch_predictions[begin - size:, :]
    return predictions

  # Create a local session to run the training.
  start_time = time.time()
  saver = tf.train.Saver()

  # create log files
  try:
    os.stat("./eval_log")
  except:
    os.mkdir("eval_log")
  minibatch_file = open("./eval_log/minibatch_error.txt", 'w')
  validation_file = open("./eval_log/validation_error.txt", 'w')
  test_file = open("./eval_log/test_error.txt","w")

  with tf.Session() as sess:
    # Run all the initializers to prepare the trainable parameters.
    tf.global_variables_initializer().run()
    print('Initialized!')
    # Loop through training steps.
    for step in xrange(int(num_epochs * train_size) // BATCH_SIZE):
      # Compute the offset of the current minibatch in the data.
      # Note that we could use better randomization across epochs.
      offset = (step * BATCH_SIZE) % (train_size - BATCH_SIZE)
      batch_data = train_data[offset:(offset + BATCH_SIZE), ...]
      batch_labels = train_labels[offset:(offset + BATCH_SIZE)]
      # This dictionary maps the batch data (as a np array) to the
      # node in the graph it schould be fed to.
      feed_dict = {train_data_node: batch_data,
                   train_labels_node: batch_labels}
      # Run the optimizer to update weights.
      sess.run(optimizer, feed_dict=feed_dict)
      # print some extra information once reach the evaluation frequency
      if step % EVAL_FREQUENCY == 0:
        # tf.Print([batch_data[0,...]],logits)
        # fetch some extra nodes' data
        l, lr, predictions = sess.run([loss, learning_rate, train_prediction],
                                      feed_dict=feed_dict)
        elapsed_time = time.time() - start_time
        start_time = time.time()
        print('Step %d (epoch %.2f), %.1f ms' %
              (step, float(step) * BATCH_SIZE / train_size,
               1000 * elapsed_time / EVAL_FREQUENCY))
        print('Minibatch loss: %.3f, learning rate: %.6f' % (l, lr))

        minibatch_error = error_rate(predictions, batch_labels)
        print('Minibatch error: %.1f%%' % minibatch_error)

        validation_error = error_rate(
            eval_in_batches(validation_data, sess), validation_labels)
        print('Validation error: %.1f%%' % validation_error)

        # test_error = error_rate(eval_in_batches(test_data, sess), test_labels)
        # print('Test error: %.1f%%' % test_error)

        minibatch_file.write(str(minibatch_error) + '\n')
        minibatch_file.flush()

        validation_file.write(str(validation_error) + '\n')
        validation_file.flush()
        # test_file.write(str(test_error) + '\n')

        sys.stdout.flush()

      if step%10000 ==0 :
        saver.save(sess,"./my_train_log/step.{}.ckpt".format(step))
        test_error = error_rate(eval_in_batches(test_data, sess), test_labels)
        test_file.write(str(test_error)+'\n')
        test_file.flush()
        sys.stdout.flush()
    # Finally print the result!
    test_error = error_rate(eval_in_batches(test_data, sess), test_labels)
    print('Test error: %.1f%%' % test_error)
    if FLAGS.self_test:
      print('test_error', test_error)
      assert test_error == 0.0, 'expected 0.0 test_error, got %.2f' % (
          test_error,)


if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '--use_fp16',
      default=False,
      help='Use half floats instead of full floats if True.',
      action='store_true')
  parser.add_argument(
      '--self_test',
      default=False,
      action='store_true',
      help='True if running a self test.')

  FLAGS, unparsed = parser.parse_known_args()
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
  app = QApplication(sys.argv)
  app.aboutToQuit.connect(app.deleteLater)



# Copyright 2015 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at #ac #     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. # ============================================================================== Simple, end-to-end, LeNet-5-like convolutional MNIST model example.  This should achieve a test error of 0.7%. Please keep this model as simple and linear as possible, it is meant as a tutorial for simple convolutional models. Run with --self_test on the command line to execute a short self-test. # pylint: disable=redefined-builtin #import datasets.mnist as mnist # CVDF mirror of http://yann.lecun.com/exdb/mnist/ # Size of the validation set. # Set to None for random seed. # Number of steps between evaluations. Convert class labels from scalars to one-hot vectors. Return the type of the activations, weights, and placeholder variables. Generate a fake dataset that matches the dimensions of MNIST. Return the error rate based on dense predictions and sparse labels. # Extract it into np arrays. # (train_data,train_labels),(test_data,test_labels) = tf.keras.datasets.cifar10.load_data() #train_data = extract_data(train_data_filename, 60000) #train_labels = extract_labels(train_labels_filename, 60000) #test_data = extract_data(test_data_filename, 10000) #test_labels = extract_labels(test_labels_filename, 10000) # Generate a validation set. # convert labels to one-hot # This is where training samples and labels are fed to the graph. # These placeholder nodes will be fed a batch of training data at each # training step using the {feed_dict} argument to the Run() call below. # We will replicate the model structure for the training subgraph, as well # as the evaluation subgraphs, while sharing the trainable parameters. #activations = tf.nn.relu(activations) #logits_sum = tf.reduce_sum(activations,axis=1,keepdims=True) #logits_sum = tf.tile(logits_sum,[1,NUM_LABELS])+epsilon #logits = activations / logits_sum #logits = tf.log((logits+epsilon)/ (1-logits + epsilon)) + epsilon #logits = tf.reshape(activations,shape=[activations.shape[0],-1]) #logits = tf.layers.dense(logits,units=10) #return tf.nn.softmax(activations+1e-9,axis=-1) #return logits # Training computation: logits + cross-entropy loss. #loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits( #    labels=train_labels_node, logits=logits)) #loss = tf.reduce_mean(-tf.reduce_sum(logits * tf.log(tf.one_hot(train_labels_node,NUM_LABELS)+epsilon), reduction_indices=[1])) #loss = capsules.nets.spread_loss( #    train_labels_node, logits, margin=1.0, name='spread_loss' # ) #+tf.reduce_mean((recon-train_data_node)**2)*1e-2 # Optimizer: set up a variable that's incremented once per batch and # controls the learning rate decay. # Decay once per epoch, using an exponential schedule starting at 0.01. # Base learning rate. # Current index into the dataset. # Decay step. # Decay rate. # Use simple momentum for the optimization. # Predictions for the current training minibatch. # Predictions for the test and validation, which we'll compute less often. # Small utility function to evaluate a dataset by feeding batches of data to # {eval_data} and pulling the results from {eval_predictions}. # Saves memory and enables this to run on smaller GPUs. Get all predictions for a dataset by running it in small batches. # Create a local session to run the training. # create log files # Run all the initializers to prepare the trainable parameters. # Loop through training steps. # Compute the offset of the current minibatch in the data. # Note that we could use better randomization across epochs. # This dictionary maps the batch data (as a np array) to the # node in the graph it schould be fed to. # Run the optimizer to update weights. # print some extra information once reach the evaluation frequency # tf.Print([batch_data[0,...]],logits) # fetch some extra nodes' data # test_error = error_rate(eval_in_batches(test_data, sess), test_labels) # print('Test error: %.1f%%' % test_error) # test_file.write(str(test_error) + '\n') # Finally print the result!