Я новичок в machine learning
и tensorflow
.Я пытаюсь реализовать XOR gate в тензорном потоке. Я пришел с этим кодом.
import numpy as np
import tensorflow as tf
tf.reset_default_graph()
learning_rate = 0.01
n_epochs = 1000
n_inputs = 2
n_hidden1 = 2
n_outputs = 2
arr1, target = [[0, 0], [0, 1], [1, 0], [1,1]], [0, 1, 1, 0]
X_data = np.array(arr1).astype(np.float32)
y_data = np.array(target).astype(np.int)
X = tf.placeholder(tf.float32, shape=(None, n_inputs), name="X")
y = tf.placeholder(tf.int64, shape=(None), name="y")
with tf.name_scope("dnn_tf"):
hidden1 = tf.layers.dense(X, n_hidden1, name="hidden1", activation=tf.nn.relu)
logits = tf.layers.dense(hidden1, n_outputs, name="outputs")
with tf.name_scope("loss"):
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits)
loss = tf.reduce_mean(xentropy, name="loss")
with tf.name_scope("train"):
optimizer = tf.train.MomentumOptimizer(learning_rate, momentum=0.9)
training_op = optimizer.minimize(loss)
with tf.name_scope("eval"):
correct = tf.nn.in_top_k(logits, y, 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
init = tf.global_variables_initializer()
with tf.Session() as sess:
init.run()
for epoch in range(n_epochs):
if epoch % 100 == 0:
print("Epoch: ", epoch, " Train Accuracy: ", acc_train)
sess.run(training_op, feed_dict={X:X_data, y:y_data})
acc_train = accuracy.eval(feed_dict={X:X_data, y:y_data})
Код работает нормально, но я получаю разные выходные данные при каждом запуске
Run-1
Epoch: 0 Train Accuracy: 0.75
Epoch: 100 Train Accuracy: 1.0
Epoch: 200 Train Accuracy: 1.0
Epoch: 300 Train Accuracy: 1.0
Epoch: 400 Train Accuracy: 1.0
Epoch: 500 Train Accuracy: 1.0
Epoch: 600 Train Accuracy: 1.0
Epoch: 700 Train Accuracy: 1.0
Epoch: 800 Train Accuracy: 1.0
Epoch: 900 Train Accuracy: 1.0
Выполнить -2
Epoch: 0 Train Accuracy: 1.0
Epoch: 100 Train Accuracy: 0.75
Epoch: 200 Train Accuracy: 0.75
Epoch: 300 Train Accuracy: 0.75
Epoch: 400 Train Accuracy: 0.75
Epoch: 500 Train Accuracy: 0.75
Epoch: 600 Train Accuracy: 0.75
Epoch: 700 Train Accuracy: 0.75
Epoch: 800 Train Accuracy: 0.75
Epoch: 900 Train Accuracy: 0.75
Выполнить3-
Epoch: 0 Train Accuracy: 1.0
Epoch: 100 Train Accuracy: 0.5
Epoch: 200 Train Accuracy: 0.5
Epoch: 300 Train Accuracy: 0.5
Epoch: 400 Train Accuracy: 0.5
Epoch: 500 Train Accuracy: 0.5
Epoch: 600 Train Accuracy: 0.5
Epoch: 700 Train Accuracy: 0.5
Epoch: 800 Train Accuracy: 0.5
Epoch: 900 Train Accuracy: 0.5
Я не могу понять, что я делаю не так и почему мое решение не сходится.