2017-05-05 56 views
-1

上輸入[[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]]此神經網絡的火車用標記的輸出:[[0.0], [1.0], [1.0], [0.0]]返回所有可能的預測值

import numpy as np 
import tensorflow as tf 

sess = tf.InteractiveSession() 
sess.run(init) 
# a batch of inputs of 2 value each 
inputs = tf.placeholder(tf.float32, shape=[None, 2]) 

# a batch of output of 1 value each 
desired_outputs = tf.placeholder(tf.float32, shape=[None, 1]) 

# [!] define the number of hidden units in the first layer 
HIDDEN_UNITS = 4 
weights_1 = tf.Variable(tf.truncated_normal([2, HIDDEN_UNITS])) 

biases_1 = tf.Variable(tf.zeros([HIDDEN_UNITS])) 

# connect 2 inputs to every hidden unit. Add bias 
layer_1_outputs = tf.nn.sigmoid(tf.matmul(inputs, weights_1) + biases_1) 

print layer_1_outputs 

NUMBER_OUTPUT_NEURONS = 1 

biases_2 = tf.Variable(tf.zeros([NUMBER_OUTPUT_NEURONS])) 
weights_2 = tf.Variable(tf.truncated_normal([HIDDEN_UNITS, NUMBER_OUTPUT_NEURONS])) 
finalLayerOutputs = tf.nn.sigmoid(tf.matmul(layer_1_outputs, weights_2) + biases_2) 

tf.global_variables_initializer().run() 

logits = tf.nn.sigmoid(tf.matmul(layer_1_outputs, weights_2) + biases_2) 

training_inputs = [[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]] 
training_outputs = [[0.0], [1.0], [1.0], [0.0]] 

error_function = 0.5 * tf.reduce_sum(tf.sub(logits, desired_outputs) * tf.sub(logits, desired_outputs)) 
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(error_function) 

for i in range(15): 
    _, loss = sess.run([train_step, error_function], 
         feed_dict={inputs: np.array(training_inputs), 
            desired_outputs: np.array(training_outputs)}) 

print(sess.run(logits, feed_dict={inputs: np.array([[0.0, 1.0]])})) 

在訓練該網絡爲值[[0.0, 1.0]]返回[[ 0.61094815]]

[[ 0.61094815]]是具有最高概率值的訓練該網絡之後分配給輸入值[[0.0,1.0]]?更低的概率值是否也可以被訪問,而不僅僅是最可能的?

如果我增加訓練時期的數量,我會得到更好的預測,但在這種情況下,我只想訪問所有潛在值和它們對給定輸入的概率。

更新:

已更新代碼以使用softmax的多類分類。但[[0.0, 1.0, 0.0, 0.0]]的預測是[array([0])]。我更新了嗎?

import numpy as np 
import tensorflow as tf 

init = tf.global_variables_initializer() 
sess = tf.InteractiveSession() 
sess.run(init) 
# a batch of inputs of 2 value each 
inputs = tf.placeholder(tf.float32, shape=[None, 4]) 

# a batch of output of 1 value each 
desired_outputs = tf.placeholder(tf.float32, shape=[None, 3]) 

# [!] define the number of hidden units in the first layer 
HIDDEN_UNITS = 4 
weights_1 = tf.Variable(tf.truncated_normal([4, HIDDEN_UNITS])) 

biases_1 = tf.Variable(tf.zeros([HIDDEN_UNITS])) 

# connect 2 inputs to every hidden unit. Add bias 
layer_1_outputs = tf.nn.softmax(tf.matmul(inputs, weights_1) + biases_1) 

biases_2 = tf.Variable(tf.zeros([3])) 
weights_2 = tf.Variable(tf.truncated_normal([HIDDEN_UNITS, 3])) 
finalLayerOutputs = tf.nn.softmax(tf.matmul(layer_1_outputs, weights_2) + biases_2) 

tf.global_variables_initializer().run() 

logits = tf.nn.softmax(tf.matmul(layer_1_outputs, weights_2) + biases_2) 

training_inputs = [[0.0, 0.0 , 0.0, 0.0], [0.0, 1.0 , 0.0, 0.0], [1.0, 0.0 , 0.0, 0.0], [1.0, 1.0 , 0.0, 0.0]] 
training_outputs = [[0.0,0.0,0.0], [1.0,0.0,0.0], [1.0,0.0,0.0], [0.0,0.0,1.0]] 

error_function = 0.5 * tf.reduce_sum(tf.sub(logits, desired_outputs) * tf.sub(logits, desired_outputs)) 
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(error_function) 

for i in range(15): 
    _, loss = sess.run([train_step, error_function], 
         feed_dict={inputs: np.array(training_inputs), 
            desired_outputs: np.array(training_outputs)}) 

prediction=tf.argmax(logits,1) 
best = sess.run([prediction],feed_dict={inputs: np.array([[0.0, 1.0, 0.0, 0.0]])}) 
print(best) 

它打印[array([0])]

更新2:

更換

prediction=tf.argmax(logits,1) 
best = sess.run([prediction],feed_dict={inputs: np.array([[0.0, 1.0, 0.0, 0.0]])}) 
print(best) 

有了:

prediction=tf.nn.softmax(logits) 
best = sess.run([prediction],feed_dict={inputs: np.array([[0.0, 1.0, 0.0, 0.0]])}) 
print(best) 

看似解決問題。

所以現在完整的源代碼是:

import numpy as np 
import tensorflow as tf 

init = tf.global_variables_initializer() 
sess = tf.InteractiveSession() 
sess.run(init) 
# a batch of inputs of 2 value each 
inputs = tf.placeholder(tf.float32, shape=[None, 4]) 

# a batch of output of 1 value each 
desired_outputs = tf.placeholder(tf.float32, shape=[None, 3]) 

# [!] define the number of hidden units in the first layer 
HIDDEN_UNITS = 4 
weights_1 = tf.Variable(tf.truncated_normal([4, HIDDEN_UNITS])) 

biases_1 = tf.Variable(tf.zeros([HIDDEN_UNITS])) 

# connect 2 inputs to every hidden unit. Add bias 
layer_1_outputs = tf.nn.softmax(tf.matmul(inputs, weights_1) + biases_1) 

biases_2 = tf.Variable(tf.zeros([3])) 
weights_2 = tf.Variable(tf.truncated_normal([HIDDEN_UNITS, 3])) 
finalLayerOutputs = tf.nn.softmax(tf.matmul(layer_1_outputs, weights_2) + biases_2) 

tf.global_variables_initializer().run() 

logits = tf.nn.softmax(tf.matmul(layer_1_outputs, weights_2) + biases_2) 

training_inputs = [[0.0, 0.0 , 0.0, 0.0], [0.0, 1.0 , 0.0, 0.0], [1.0, 0.0 , 0.0, 0.0], [1.0, 1.0 , 0.0, 0.0]] 
training_outputs = [[0.0,0.0,0.0], [1.0,0.0,0.0], [1.0,0.0,0.0], [0.0,0.0,1.0]] 

error_function = 0.5 * tf.reduce_sum(tf.sub(logits, desired_outputs) * tf.sub(logits, desired_outputs)) 
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(error_function) 

for i in range(1500): 
    _, loss = sess.run([train_step, error_function], 
         feed_dict={inputs: np.array(training_inputs), 
            desired_outputs: np.array(training_outputs)}) 

prediction=tf.nn.softmax(logits) 
best = sess.run([prediction],feed_dict={inputs: np.array([[0.0, 1.0, 0.0, 0.0]])}) 
print(best) 

它打印

[array([[ 0.49810624, 0.24845563, 0.25343812]], dtype=float32)] 

回答

1

您當前的網絡做(邏輯)迴歸,而不是真的分類:給定的輸入x,它試圖評估f(x)(這裏的f(x) = x1 XOR x2這裏,但網絡不知道那訓練前),這是迴歸。爲此,它會學習一個函數f1(x),並嘗試在所有訓練樣本上儘可能接近f(x)[[ 0.61094815]]只是f1([[0.0, 1.0]])的值。在這種情況下,由於沒有階級,所以「沒有階級的概率」就沒有了。只有用戶(您)選擇將f1(x)解釋爲輸出爲1的概率。由於您只有2個類,所以會告訴您其他類的概率爲1-0.61094815(也就是說,您正在進行分類與網絡的輸出,但它本身並沒有真正做到這一點)。這種用作分類的方法在某種程度上是一種(廣泛使用的)執行分類的技巧,但僅適用於有兩類的分類。

建立一個真實的分類網絡會有點不同:你的logits的形狀是(batch_size, number_of_classes) - 你的情況如此(1,2),你對它們應用sofmax,然後預測爲argmax(softmax),概率max(softmax)。然後你也可以得到每個輸出的概率,根據網絡:probability(class i) = softmax[i]。在這裏,網絡真正得到了訓練,可以學習每個班級中的x的概率。

對不起,如果我的解釋是模糊的,或者0和1之間的迴歸和分類之間的區別在2類的設置中似乎是哲學的,但是如果添加更多的類,您可能會看到我的意思。

編輯 回答你的2更新。

  • 您的訓練樣本中,標籤(training_outputs)必須是概率分佈,即,它們必須具有總和1對於每個樣品(它們的形式爲(1,0的99%的時間,0), (0,1,0)或(0,0,1)),所以你的第一個輸出[0.0,0.0,0.0]無效。如果要在兩個第一個輸入上學習XOR,則第一個輸出應與最後一個輸出相同:[0.0,0.0,1.0]。

  • prediction=tf.argmax(logits,1) = [array([0])]是完全正常:logins包含您的概率,並prediction是預測,這是最大的概率,這是你的情況類0類:在你的訓練集,[0.0, 1.0, 0.0, 0.0]與輸出[1.0, 0.0, 0.0]相關,即它是0級的概率爲1,其他級別的概率爲0.經過足夠的訓練後,print(best)prediction=tf.argmax(logits,1)輸入[1.0, 1.0 , 0.0, 0.0]應該給你[array([2])],2是這個類的索引輸入你的訓練集。

+0

感謝您的建議,我試圖將其納入神經網絡。你能在我的問題更新中注意到神經網絡的問題嗎? –

+0

我認爲我解決了問題並更新了問題。 –

+0

我編輯了我的答案 – gdelab