我試圖在Iris數據集上運行標準NN。標籤是一個單一的列,可以有值0,1,2,這取決於物種。我將這些特徵轉置到x軸上,並將這些例子轉換爲y。Tensorflow Iris數據集永遠不會收斂
關注領域:成本函數 - 每個人似乎都使用預編譯函數,但由於我的數據不是單編碼編碼,因此我使用標準損失。優化器 - 我將它用作黑盒子,我不確定是否能夠正確更新成本。
在此先感謝您的幫助。
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}))
您是否嘗試過使用更高的學習率? –
@JakubBartczuk嗨Jakub非常感謝。更高的學習率讓我收斂,但似乎我所有的價值觀都不正確。我不確定我會做錯什麼(準確率爲0%)。你對如何改進這個模型有什麼建議嗎? – user3204416