我正在尝试建立图像分类网络(2类,猫和非猫)。 NN的定义如下:
def construct_network(X, params):
Z1 = tf.add(tf.matmul(params["W1"], X), params["b1"])
A1 = tf.nn.relu(Z1)
Z2 = tf.add(tf.matmul(params["W2"], A1), params["b2"])
A2 = tf.nn.relu(Z2)
Z3 = tf.add(tf.matmul(params["W3"], A2), params["b3"])
return Z3
成本函数:
def calc_cost(Z3, Y):
logits = tf.transpose(Z3)
labels = tf.transpose(Y)
return tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
然后:
Z3 = construct_network(X_p, params)
cost = calc_cost(Z3, Y_p)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
典型结果如下:
Cost for epoch 0: 49.456378
Cost for epoch 100: 12.760913
Cost for epoch 200: 10.387191
Cost for epoch 300: 13.810563
Cost for epoch 400: 9.316090
Cost for epoch 500: 8.210016
Cost for epoch 600: 7.901745
Cost for epoch 700: 8.416944
Cost for epoch 800: 5.621855
Cost for epoch 900: 5.904790
Cost for epoch 1000: 4.743062
Cost for epoch 1100: 8.970100
Cost for epoch 1200: 6.074375
Cost for epoch 1300: 12.980518
Cost for epoch 1400: 6.193832
我尝试过使用这样训练过的模型:
(files, X, Y) = generate_minibatch(test_samples)
result = sess.run(Y_predict, feed_dict={X_p: X})
# result = Y_predict.eval(feed_dict={X_p: X})
result = (tf.argmax(result, 0)).eval()
actual_result = tf.argmax(Y, 0).eval()
difference = tf.equal(result, actual_result).eval()
但实际结果似乎只是随机的。有时它会正确猜出一些图像。有时它会为所有测试样本输出相同的值。
所以我有几个问题:
感谢。