【发布时间】:2018-08-15 04:18:02
【问题描述】:
我正在使用 Tensorflow 构建一个简单的单层神经网络。
对于输入,每行数据对应 10 个答案。每行的前 2 个元素是正确的,即与地面实况标签相同。相比之下,最后 8 个元素与地面实况标签相反。
例如,
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0], correct is 1
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1], correct is 0
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1], correct is 0
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0], correct is 1
我希望我的神经网络知道前两个元素/特征总是给出正确的结果。因此,我希望网络对前两个特征给予更大的权重。但是,网络总是会卡在一些损失值上。
更有趣的是,准确率被认为是正确预测的标签占标签总数的比例。损失函数使用 sigmoid 函数计算,即 $y * log(logit) + (1-y) * log(1-logit))$。有时,随着损失的减少,准确性会提高。例如,
epoch is: 0 loss is: 7.661093 accuracy value is: 1.0
epoch is: 100 loss is: 7.579134 accuracy value is: 0.54545456
epoch is: 200 loss is: 7.5791006 accuracy value is: 0.54545456
我认为网络可以不断增加前两个元素的权重,直到它可以完全预测正确的标签。
谁能告诉我我应该怎么做才能促进网络正确预测标签,而不是卡住?
我的代码在这里:
import tensorflow as tf
import numpy as np
class SigmoidNeuralNetwork():
def __init__(self, learning_rate, training_data, correct_labels, epoch_number):
self.learning_rate = learning_rate
self.training_data = training_data
self.correct_labels = correct_labels
self.X = tf.placeholder(tf.float32)
self.y = tf.placeholder(tf.float32)
self.feature_num = len(self.training_data[0])
self.sample_num = len(self.training_data)
self.W = tf.Variable(tf.random_uniform([self.feature_num, 1], -1.0, 1.0), dtype=tf.float32)
self.b = tf.Variable([0.0])
self.epoch_number = epoch_number
def launch_network(self):
db = tf.matmul(self.X, tf.reshape(self.W, [-1, 1])) + self.b
hyp = tf.sigmoid(db)
cost0 = self.y * tf.log(tf.clip_by_value(hyp, 1e-10, 1.0))
cost1 = (1 - self.y) * tf.log(tf.clip_by_value((1 - hyp), 1e-10, 1.0))
cost = (cost0 + cost1) / float(self.sample_num)
loss = -tf.reduce_sum(cost)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=self.learning_rate)
train = optimizer.minimize(loss)
#
new_train_X = self.training_data.astype(np.float32)
output = tf.add(tf.matmul(new_train_X, self.W), self.b)
prediction = tf.sigmoid(output)
predicted_class = tf.greater(prediction, 0.5)
ground_labels = tf.reshape(tf.equal(self.y, 1.0), predicted_class.shape)
correct = tf.equal(predicted_class, ground_labels)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
#
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for epoch in range(self.epoch_number):
_, loss_val, accuracy_val = sess.run([train, loss, accuracy], {self.X: self.training_data, self.y: self.correct_labels})
if epoch % 100 == 0:
print "epoch is: ", epoch, "loss is: ", loss_val, " accuracy value is: ", accuracy_val
# print "weight is: ", sess.run(self.W).flatten()
train_data = np.array([
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
])
correct_answers = np.array([1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1])
sigmoid_network = SigmoidNeuralNetwork(learning_rate=0.01, training_data=train_data, correct_labels=correct_answers,
epoch_number=10000)
sigmoid_network.launch_network()
【问题讨论】:
标签: python tensorflow machine-learning neural-network