def fit(self, training_data_file, training_label_file, num_classes, pixel): train_set = read_data_sets(training_data_file, training_label_file, num_classes=num_classes, one_hot=True) self.session = tf.InteractiveSession() # Create the model self.x = tf.placeholder(tf.float32, [None, pixel]) self.W = tf.Variable(tf.zeros([pixel, num_classes])) self.b = tf.Variable(tf.zeros([num_classes])) self.y = tf.nn.softmax(tf.matmul(self.x, self.W) + self.b) # Define loss and optimizer y_ = tf.placeholder(tf.float32, [None, num_classes]) cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(self.y), reduction_indices=[1])) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) # Train tf.initialize_all_variables().run() for i in range(1000): batch_xs, batch_ys = train_set.next_batch(200) train_step.run({self.x: batch_xs, y_: batch_ys})
def png_to_features(filename): width, height, pngdata, meta = png.Reader(filename).asDirect() image = padding(13, 16, (width, height, numpy.vstack(map(numpy.uint8, pngdata)))) image = image.reshape((1, 13 * 16)) image = image.astype(numpy.float32) image = numpy.multiply(image, 1.0 / 255.0) return image if __name__ == '__main__': num_classes = 9 pixel = 208 train_set = read_data_sets('/home/zhou/train.gz', '/home/zhou/train_label.gz', num_classes=num_classes, one_hot=True) # test_set = read_data_sets('/home/zhou/test.gz') sess = tf.InteractiveSession() # Create the model x = tf.placeholder(tf.float32, [None, pixel]) W = tf.Variable(tf.zeros([pixel, num_classes])) b = tf.Variable(tf.zeros([num_classes])) y = tf.nn.softmax(tf.matmul(x, W) + b) # Define loss and optimizer y_ = tf.placeholder(tf.float32, [None, num_classes]) cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) # Train