import tensorflow as tf
def get_weights(n_features, n_labels):
"""
Return TensorFlow weights
:param n_features: Number of features
:param n_labels: Number of labels
:return: TensorFlow weights
"""
return tf.Variable(tf.truncated_normal((n_features, n_labels)))
def get_biases(n_labels):
"""
Return TensorFlow bias
:param n_labels: Number of labels
:return: TensorFlow bias
"""
return tf.Variable(tf.zeros(n_labels))
def linear(input, w, b):
"""
Return linear function in TensorFlow
:param input: TensorFlow input
:param w: TensorFlow weights
:param b: TensorFlow biases
:return: TensorFlow linear function
"""
return tf.add(tf.matmul(input, w), b)
from tensorflow.examples.tutorials.mnist import input_data
def mnist_features_labels(n_labels):
"""
Gets the first <n> labels from the MNIST dataset
:param n_labels: Number of labels to use
:return: Tuple of feature list and label list
"""
mnist_features = []
mnist_labels = []
mnist = input_data.read_data_sets('/datasets/ud730/mnist', one_hot=True)
for mnist_feature, mnist_label in zip(*mnist.train.next_batch(10000)):
if mnist_label[:n_labels].any():
mnist_features.append(mnist_feature)
mnist_labels.append(mnist_label[:n_labels])
return mnist_features, mnist_labels
n_features = 784
n_labels = 3
features = tf.placeholder(tf.float32)
labels = tf.placeholder(tf.float32)
w = get_weights(n_features, n_labels)
b = get_biases(n_labels)
logits = linear(features, w, b)
train_features, train_labels = mnist_features_labels(n_labels)
with tf.Session() as session:
session.run(tf.global_variables_initializer())
prediction = tf.nn.softmax(logits)
cross_entropy = -tf.reduce_sum(labels * tf.log(prediction), reduction_indices=1)
loss = tf.reduce_mean(cross_entropy)
learning_rate = 0.08
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
_, l = session.run(
[optimizer, loss],
feed_dict={features: train_features, labels: train_labels})
print('Loss: {}'.format(l))