使用单输出神经元tensorflow的神经网络时loss和accuracy都是0
Loss and accuracy are 0 when using a neural network with a single output neuron tensorflow
我正在写一个二进制 classifier,用于某个任务,而不是在输出层使用 2 个神经元,我只想使用一个带有 sigmoid 函数的神经元,并且基本上输出 class如果低于 0.5 则为 0,否则为 1。
图像已加载,调整为 64x64 并展平,以创建问题的复制品)。数据加载的代码将出现在最后。我创建占位符。
x = tf.placeholder('float',[None, 64*64])
y = tf.placeholder('float',[None, 1])
并定义模型如下。
def create_model_linear(data):
fcl1_desc = {'weights': weight_variable([4096,128]), 'biases': bias_variable([128])}
fcl2_desc = {'weights': weight_variable([128,1]), 'biases': bias_variable([1])}
fc1 = tf.nn.relu(tf.matmul(data, fcl1_desc['weights']) + fcl1_desc['biases'])
fc2 = tf.nn.sigmoid(tf.matmul(fc1, fcl2_desc['weights']) + fcl2_desc['biases'])
return fc2
函数weight_variable
和bias_variable
只是return给定形状的tf.Variable()
。 (他们的代码也在最后。)
那我定义训练函数如下。
def train(x, hm_epochs):
prediction = create_model_linear(x)
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits = prediction, labels = y) )
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
batch_size = 100
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
i = 0
while i < len(train_x):
start = i
end = i + batch_size
batch_x = train_x[start:end]
batch_y = train_y[start:end]
_, c = sess.run([optimizer, cost], feed_dict = {x:batch_x, y:batch_y})
epoch_loss += c
i+=batch_size
print('Epoch', epoch+1, 'completed out of', hm_epochs,'loss:',epoch_loss)
correct = tf.greater(prediction,[0.5])
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
i = 0
acc = []
while i < len(train_x):
acc +=[accuracy.eval({x:train_x[i:i+1000], y:train_y[i:i + 1000]})]
i+=1000
print sum(acc)/len(acc)
train(x, 10)
的输出是
('Epoch', 1, 'completed out of', 10, 'loss:', 0.0)
('Epoch', 2, 'completed out of', 10, 'loss:', 0.0)
('Epoch', 3, 'completed out of', 10, 'loss:', 0.0)
('Epoch', 4, 'completed out of', 10, 'loss:', 0.0)
('Epoch', 5, 'completed out of', 10, 'loss:', 0.0)
('Epoch', 6, 'completed out of', 10, 'loss:', 0.0)
('Epoch', 7, 'completed out of', 10, 'loss:', 0.0)
('Epoch', 8, 'completed out of', 10, 'loss:', 0.0)
('Epoch', 9, 'completed out of', 10, 'loss:', 0.0)
('Epoch', 10, 'completed out of', 10, 'loss:', 0.0)
0.0
What am I missing?
这里是所有实用函数的承诺代码:
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def getLabel(wordlabel):
if wordlabel == 'Class_A':
return [1]
elif wordlabel == 'Class_B':
return [0]
else:
return -1
def loadImages(pathToImgs):
images = []
labels = []
filenames = os.listdir(pathToImgs)
imgCount = 0
for i in tqdm(filenames):
wordlabel = i.split('_')[1]
oneHotLabel = getLabel(wordlabel)
img = cv2.imread(pathToImgs + i,cv2.IMREAD_GRAYSCALE)
if oneHotLabel != -1 and type(img) is np.ndarray:
images += [cv2.resize(img,(64,64)).flatten()]
labels += [oneHotLabel]
imgCount+=1
print imgCount
return (images,labels)
我认为你应该使用 tf.nn.sigmoid_cross_entropy_with_logits
而不是 tf.nn.softmax_cross_entropy_with_logits
因为你在输出层使用了 sigmoid 和 1 个神经元。
此外,您还需要从 create_model_linear
中的最后一层移除 sigmoid
并且,您没有使用 y
标签,准确性必须采用以下形式。
correct = tf.equal(tf.greater(tf.nn.sigmoid(prediction),[0.5]),tf.cast(y,'bool'))
我正在写一个二进制 classifier,用于某个任务,而不是在输出层使用 2 个神经元,我只想使用一个带有 sigmoid 函数的神经元,并且基本上输出 class如果低于 0.5 则为 0,否则为 1。
图像已加载,调整为 64x64 并展平,以创建问题的复制品)。数据加载的代码将出现在最后。我创建占位符。
x = tf.placeholder('float',[None, 64*64])
y = tf.placeholder('float',[None, 1])
并定义模型如下。
def create_model_linear(data):
fcl1_desc = {'weights': weight_variable([4096,128]), 'biases': bias_variable([128])}
fcl2_desc = {'weights': weight_variable([128,1]), 'biases': bias_variable([1])}
fc1 = tf.nn.relu(tf.matmul(data, fcl1_desc['weights']) + fcl1_desc['biases'])
fc2 = tf.nn.sigmoid(tf.matmul(fc1, fcl2_desc['weights']) + fcl2_desc['biases'])
return fc2
函数weight_variable
和bias_variable
只是return给定形状的tf.Variable()
。 (他们的代码也在最后。)
那我定义训练函数如下。
def train(x, hm_epochs):
prediction = create_model_linear(x)
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits = prediction, labels = y) )
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
batch_size = 100
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
i = 0
while i < len(train_x):
start = i
end = i + batch_size
batch_x = train_x[start:end]
batch_y = train_y[start:end]
_, c = sess.run([optimizer, cost], feed_dict = {x:batch_x, y:batch_y})
epoch_loss += c
i+=batch_size
print('Epoch', epoch+1, 'completed out of', hm_epochs,'loss:',epoch_loss)
correct = tf.greater(prediction,[0.5])
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
i = 0
acc = []
while i < len(train_x):
acc +=[accuracy.eval({x:train_x[i:i+1000], y:train_y[i:i + 1000]})]
i+=1000
print sum(acc)/len(acc)
train(x, 10)
的输出是
('Epoch', 1, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 2, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 3, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 4, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 5, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 6, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 7, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 8, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 9, 'completed out of', 10, 'loss:', 0.0) ('Epoch', 10, 'completed out of', 10, 'loss:', 0.0)
0.0 What am I missing?
这里是所有实用函数的承诺代码:
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def getLabel(wordlabel):
if wordlabel == 'Class_A':
return [1]
elif wordlabel == 'Class_B':
return [0]
else:
return -1
def loadImages(pathToImgs):
images = []
labels = []
filenames = os.listdir(pathToImgs)
imgCount = 0
for i in tqdm(filenames):
wordlabel = i.split('_')[1]
oneHotLabel = getLabel(wordlabel)
img = cv2.imread(pathToImgs + i,cv2.IMREAD_GRAYSCALE)
if oneHotLabel != -1 and type(img) is np.ndarray:
images += [cv2.resize(img,(64,64)).flatten()]
labels += [oneHotLabel]
imgCount+=1
print imgCount
return (images,labels)
我认为你应该使用 tf.nn.sigmoid_cross_entropy_with_logits
而不是 tf.nn.softmax_cross_entropy_with_logits
因为你在输出层使用了 sigmoid 和 1 个神经元。
此外,您还需要从 create_model_linear
中的最后一层移除 sigmoid
并且,您没有使用 y
标签,准确性必须采用以下形式。
correct = tf.equal(tf.greater(tf.nn.sigmoid(prediction),[0.5]),tf.cast(y,'bool'))