Я пытаюсь создать простую нейронную сеть, используя тензор потока в качестве учебного упражнения. Это детали NN, который я создал ..
def multilayer_perceptron(x, weights, biases, keep_prob):
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
layer_1 = tf.nn.dropout(layer_1, keep_prob)
out_layer = tf.matmul(layer_1, weights['out']) + biases['out']
return out_layer
n_hidden_1 = 38
n_input = train_x.shape[1]
n_classes = train_y.shape[1]
weights = {
'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
'out': tf.Variable(tf.random_normal([n_hidden_1, n_classes]))
}
biases = {
'b1': tf.Variable(tf.random_normal([n_hidden_1])),
'out': tf.Variable(tf.random_normal([n_classes]))
}
keep_prob = tf.placeholder("float")
training_epochs = 500
display_step = 100
batch_size = 320
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])
predictions = multilayer_perceptron(x, weights, biases, 1)
cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=predictions, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=1).minimize(cost)
Это мой код для tf.session
from tensorflow import keras
with tf.Session() as sess:
# Step 1. Initializing the session
init = tf.global_variables_initializer()
writer = tf.summary.FileWriter('/home/dileep/Desktop', sess.graph)
sess.run(init)
# Step 2. Dividing x and y to batches
for epoch in range(training_epochs):
avg_cost = 0.0
total_batch = int(len(train_x)//batch_size)
x_batches = np.array_split(train_x, total_batch)
y_batches = np.array_split(train_y, total_batch)
# Step 3. Run session, calculate cost.
for i in range(total_batch):
batch_x, batch_y = x_batches[i], y_batches[i]
_, c = sess.run([optimizer, cost], feed_dict={
x:batch_x,
y:batch_y, keep_prob:0.4})
avg_cost += c/total_batch
# Step 4. Print the outputs
if epoch % display_step == 0:
print("Epoch:", '%0d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost))
cost_summary = tf.summary.scalar(name='cost_summary', tensor=avg_cost)
summary = sess.run(cost_summary)
writer.add_summary(summary, epoch)
print("Optimization finished!")
correct_prediction = tf.equal(tf.argmax(predictions, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
print('Accuracy:', accuracy.eval({x: test_x, y: test_y, keep_prob:0.8}))
Когда я запускаю это, функция стоимости приятно уменьшается с эпохи, но точность теста показывает как 1. Я попытался заменить test_x и test_y случайными числами, и все же он дает точность 1, так что это, очевидно, неправильно. Но я не могу стрелять в неприятности. Может ли кто-нибудь показать мне, где проблема? Спасибо.
Это график затрат, который я мог построить из приведенного выше кода.