def __init__(self, ses, lr, train_labels, train_data, test_data, test_labels, batch_size): self.ses = ses self.accuracy = 0 self.batch_size = batch_size self.batch_no = 0 self.train_data = train_data self.train_labels = train_labels self.test_data = test_data self.test_labels = test_labels self.input = tf1.placeholder(shape=[ None, self.train_data[0].shape[0], self.train_data[0].shape[1] ], dtype=tf.uint8, name="input") self.predictions = tf1.placeholder(shape=[None, 4], dtype=tf.float32, name="predictions") self.perceptron = tf.identity(model.lenet_5(self.input / 255), name="perceptron") loss_fn = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=self.predictions, logits=self.perceptron)) self.loss = tf.identity(loss_fn, name="loss") self.optimizer = tf1.train.GradientDescentOptimizer(learning_rate=lr, name="optimizer") self.train_op = self.optimizer.minimize(self.loss) self.acc_val = tf.cast(tf.equal(tf.argmax(self.predictions, 1), tf.argmax(self.perceptron, 1)), dtype=tf.float32) self.acc = tf.reduce_mean(self.acc_val) self.ses.run(tf1.global_variables_initializer())
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import numpy as np from sklearn.decomposition import PCA from sklearn.manifold import TSNE import matplotlib.pyplot as plt import model model_dir = './lenet-5/' mnist = input_data.read_data_sets("mnist_data", one_hot=True) x = tf.placeholder(tf.float32, [None, 784]) y_ = tf.placeholder(tf.float32, [None, 10]) image = tf.reshape(x, [-1, 28, 28, 1]) keep_prob = tf.placeholder(tf.float32) y, intermediate, conv1, conv2 = model.lenet_5(image, keep_prob) predicted = tf.argmax(y, 1) label = tf.argmax(y_, 1) correct = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) saver = tf.train.Saver() init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint(model_dir)) print('restore succeed.') samples = []
from tensorflow.examples.tutorials.mnist import input_data import numpy as np from sklearn.decomposition import PCA from sklearn.manifold import TSNE import matplotlib.pyplot as plt import model import time model_dir = './lenet-5/' mnist = input_data.read_data_sets("data/fashion", one_hot=True) x = tf.placeholder(tf.float32, [None, 784]) y_ = tf.placeholder(tf.float32, [None, 10]) image = tf.reshape(x, [-1, 28, 28, 1]) keep_prob = tf.placeholder(tf.float32) y, intermediate, w_fc2, w_fc3 = model.lenet_5(image, keep_prob) predicted = tf.argmax(y, 1) label = tf.argmax(y_, 1) correct = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) saver = tf.train.Saver() init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint(model_dir)) print('restore succeed.') samples = []
def main(argv=None): x = tf.placeholder(tf.float32, [None, 784]) y_ = tf.placeholder(tf.float32, [None, 10]) image = tf.reshape(x, [-1, 28, 28, 1]) keep_prob = tf.placeholder(tf.float32) y, _, _, _ = model.lenet_5(image, keep_prob) # print(y) loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_, logits=y)) train_step = tf.train.AdamOptimizer(1e-3).minimize(loss) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) saver = tf.train.Saver(max_to_keep=1) init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: restore_dir = tf.train.latest_checkpoint(train_dir) if restore_dir: saver.restore(sess, restore_dir) print('restore succeed') else: sess.run(init) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) for step in range(50000 + 1): start_time = time.time() batch = mnist.train.next_batch(100) train_step.run(feed_dict={ x: batch[0], y_: batch[1], keep_prob: 0.5 }) duration = time.time() - start_time if step % 100 == 0: valid_batch = mnist.validation.next_batch(100) train_accuracy = accuracy.eval(feed_dict={ x: valid_batch[0], y_: valid_batch[1], keep_prob: 1.0 }) print("step %d, training accuracy %g, %.6fsec" % (step, train_accuracy, duration)) if step % 2000 == 0: saver.save(sess, train_dir, global_step=step) test_batch = mnist.test.next_batch(5000) # print("test accuracy %g" % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})) print("1st part test accuracy %g" % accuracy.eval(feed_dict={ x: test_batch[0], y_: test_batch[1], keep_prob: 1.0 })) test_batch = mnist.test.next_batch(5000) print("2nd part test accuracy %g" % accuracy.eval(feed_dict={ x: test_batch[0], y_: test_batch[1], keep_prob: 1.0 })) coord.request_stop() coord.join(threads) return