我被卡住的時間太長,需要一些幫助(對於tensorflow等非常新)。我根據自己的數據修改了一個MNIST示例,但即使在2個紀元後仍保持100%的準確性。
我的X是(類似於MNIST)a [18,1] - 向量和y是一個float32。
變量:Tensorflow:獲取NN的正確精度
n_nodes_hl1 = 100
n_nodes_hl2 = 100
n_nodes_hl3 = 50
x = tf.placeholder(shape=[None, 18], dtype=tf.float32)
y = tf.placeholder(shape=[None, 1], dtype=tf.float32)
x_vals_train = np.array([])
y_vals_train = np.array([])
x_vals_test = np.array([])
y_vals_test = np.array([])
loss_vec = []
我的模型:
def neural_net_model(data):
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([18,n_nodes_hl1])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1,n_nodes_hl2])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2,n_nodes_hl3])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}
output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3,1])),
'biases':tf.Variable(tf.random_normal([1]))}
l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']),hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']),hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']),hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3, output_layer['weights']) + output_layer['biases']
return output
會議:
def train_neural_network(x):
prediction = neural_net_model(x)
cost = tf.reduce_mean(tf.abs(y - prediction))
optimizer = tf.train.AdamOptimizer(0.01).minimize(cost)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(10):
temp_loss = 0
rand_index = np.random.choice(len(x_vals_train), 50)
rand_x = x_vals_train[rand_index]
rand_y = np.transpose([y_vals_train[rand_index]])
_, temp_loss = sess.run(optimizer, feed_dict={x: rand_x, y: rand_y})
if (i+1)%100==0:
print('Generation: ' + str(i+1) + '. Loss = ' + str(temp_loss))
# evaluate accuracy
correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print "accuracy %.5f'" % accuracy.eval(feed_dict={x: x_vals_test, y: np.transpose([y_vals_test])})
問題是primarely爲什麼我總是得到100%的準確率,這是顯然是假的。提前致謝!
現在一切看起來更清晰:)謝謝。我的輸出是一個浮點數(查詢的執行時間) - 所以如果你有任何提示,我全部都是耳朵。但你清楚地回答了原來的問題..所以我接受 – dv3
平均方差是實際價值輸出的最佳指標。你也可以使用平均絕對誤差,就像你在損失中一樣。 – indraforyou