Reputation: 97
I am attempting to run a standard NN on the Iris dataset. The labels are a single column that can have values 0,1,2, depending on the species. I transpose the features onto the x-axis and the examples on the y.
Areas of concern: the cost function - everyone seems to use a prebuilt one, but since my data isn't one-hot encoded I'm using standard loss. the optimizer- I'm using it as a black box and am not sure if I'm getting it to properly update the cost.
Thanks in advance for your help.
import tensorflow as tf
import numpy as np
import pandas as pd
import tensorflow as tf
def create_layer(previous_layer, weight, bias, activation_function=None):
z = tf.add(tf.matmul(weight, previous_layer), bias)
if activation_function is None:
return z
a = activation_function(z)
return a
def cost_compute(prediction, correct_values):
return tf.nn.softmax_cross_entropy_with_logits(logits = prediction, labels = correct_values)
input_features = 4
n_hidden_units1 = 10
n_hidden_units2 = 14
n_hidden_units3 = 12
n_hidden_units4 = 1
rate = .000001
weights = dict(
w1=tf.Variable(tf.random_normal([n_hidden_units1, input_features])),
w2=tf.Variable(tf.random_normal([n_hidden_units2, n_hidden_units1])),
w3=tf.Variable(tf.random_normal([n_hidden_units3, n_hidden_units2])),
w4=tf.Variable(tf.random_normal([n_hidden_units4, n_hidden_units3]))
)
biases = dict(
b1=tf.Variable(tf.zeros([n_hidden_units1, 1])),
b2=tf.Variable(tf.zeros([n_hidden_units2, 1])),
b3=tf.Variable(tf.zeros([n_hidden_units3, 1])),
b4=tf.Variable(tf.zeros([n_hidden_units4, 1]))
)
train = pd.read_csv("/Users/yazen/Desktop/datasets/iris_training.csv")
test = pd.read_csv("/Users/yazen/Desktop/datasets/iris_test.csv")
train.columns = ['sepal length', 'sepal width', 'petal length', 'petal width', 'species']
test.columns = ['sepal length', 'sepal width', 'petal length', 'petal width', 'species']
train_labels = np.expand_dims(train['species'].as_matrix(), 1)
test_labels = np.expand_dims(test['species'].as_matrix(), 1)
train_features = train.drop('species', axis=1)
test_features = test.drop('species', axis=1)
test_labels = test_labels.transpose()
train_labels = train_labels.transpose()
test_features = test_features.transpose()
train_features = train_features.transpose()
x = tf.placeholder("float32", [4, None], name="asdfadsf")
y = tf.placeholder("float32", [1, None], name="asdfasdf2")
layer = create_layer(x, weights['w1'], biases['b1'], tf.nn.relu)
layer = create_layer(layer, weights['w2'], biases['b2'], tf.nn.relu)
layer = create_layer(layer, weights['w3'], biases['b3'], tf.nn.relu)
Z4 = create_layer(layer, weights['w4'], biases['b4'])
cost = cost_compute(Z4, y)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for iteration in range(1,50):
optimizer = tf.train.GradientDescentOptimizer(learning_rate=rate).minimize(cost)
_, c = sess.run([optimizer, cost], feed_dict={x: train_features, y: train_labels})
print("Iteration " + str(iteration) + " cost: " + str(c))
prediction = tf.equal(Z4, y)
accuracy = tf.reduce_mean(tf.cast(prediction, "float"))
print(sess.run(Z4, feed_dict={x: train_features, y: train_labels}))
print(accuracy.eval({x: train_features, y: train_labels}))
Upvotes: 1
Views: 246
Reputation: 19634
Since you have a classification problem, you need to transform your labels into one-hot form. You can use tf.one_hot
for this purpose. In addition, you may also apply tf.reduce_mean
on cost, as is done in the example below (taken from here). In addition, your learning rate seems too small to me.
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.matmul(x, W) + b
# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
# Train
for _ in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
# Test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images,
y_: mnist.test.labels}))
Upvotes: 1