def run(self, step_limit): self.train() with tf.Session() as sess: tf.global_variables_initializer().run() #cifar-10 데이터(32*32*3)을 받아오고, fc로 할 수 있도록 형태를 변환한다.(32*32*3 => 3072) dataset = cifar.Cifar() train_data, train_label, test_data, test_label = dataset.getdata() train_data = train_data.reshape(-1, 3072) test_data = test_data.reshape(-1, 3072) test_indices = np.arange(len(test_data)) np.random.shuffle(test_indices) test_indices = test_indices[0:1000] name = self.info() path = "cifar/" + str(step_limit) + name saver = NNutils.save(path, sess) writer, writer_test, merged = NNutils.graph(path, sess) step = sess.run(self.global_step) while step < step_limit: print("step :", step) for start, end in zip( range(0, len(train_data), self.batch_size), range(self.batch_size, len(train_data), self.batch_size)): summary, \ _, loss, \ step = sess.run([merged, self.training, self.cost, self.global_step], feed_dict={self.x: train_data[start:end], self.y: train_label[start:end], self.dropout_conv: 1.0, self.dropout_normal: 1.0}) if step % 50 == 0: writer.add_summary(summary, step) print(step, datetime.now(), loss) summary, \ loss, \ accuracy = sess.run([merged, self.cost, self.accuracy], feed_dict={self.x: test_data, self.y: test_label, self.dropout_conv: 1.0, self.dropout_normal: 1.0}) writer_test.add_summary(summary, step) print("test results : ", accuracy, loss) saver.save(sess, path + "/" + name + ".ckpt", step)
def run(self, step_limit): self.train() with tf.Session() as sess: tf.global_variables_initializer().run() dataset = cifar.Cifar() train_data, train_label, test_data, test_label = dataset.getdata() test_indices = np.arange(len(test_data)) np.random.shuffle(test_indices) test_indices = test_indices[0:1000] path = "ConvAE_2dim/50000" saver = NNutils.save(path, sess) writer, writer_test, merged = NNutils.graph(path, sess) step = sess.run(self.global_step) while step < step_limit: print("step :", step) for start, end in zip( range(0, len(train_data), self.batch_size), range(self.batch_size, len(train_data), self.batch_size)): summary, \ _, \ loss, \ step = sess.run([merged, self.training, self.cost, self.global_step], feed_dict={self.x: train_data[start:end], self.y: train_label[start:end], self.dropout_conv: 0.8, self.dropout_normal: 0.5}) if step % 50 == 0: writer.add_summary(summary, step) print(step, datetime.now(), loss) summary, loss = sess.run( [merged, self.cost], feed_dict={ self.x: test_data[test_indices], self.y: test_label[test_indices], self.dropout_conv: 1.0, self.dropout_normal: 1.0 }) writer_test.add_summary(summary, step) print("test results : ", loss) saver.save(sess, path + "/model.ckpt", step)
import cifar from matplotlib import pyplot as plt num_train_images = 10 num_test_images = 10 width = 32 height = 32 channels = 3 c = cifar.Cifar(num_train_images, num_test_images) ((train_images, train_labels), (test_images, test_labels)) = c.load_data() print('train_labels = %s' % \ (','.join([c.labels[i] for i in c.train_labels]))) plt.imshow(train_images[0]) plt.show() print('test_labels = %s' % \ (','.join([c.labels[i] for i in c.test_labels]))) plt.imshow(test_images[0]) plt.show()
self.training, self.cost, self.global_step], feed_dict={self.x: train_data[start:end], self.y: train_label[start:end], self.dropout_conv: 0.8, self.dropout_normal: 0.5}) if step % 50 == 0: writer.add_summary(summary, step) print(step, datetime.now(), loss) summary, loss = sess.run( [merged, self.cost], feed_dict={ self.x: test_data[test_indices], self.y: test_label[test_indices], self.dropout_conv: 1.0, self.dropout_normal: 1.0 }) writer_test.add_summary(summary, step) print("test results : ", loss) saver.save(sess, path + "/model.ckpt", step) dataset = cifar.Cifar() train_data, train_label, test_data, test_label = dataset.getdata() model = Network() model.run(50000)