2016-03-05 226 views
0

我試圖在TensorFlow上訓練一個非常簡單的模型。模型將一個浮點數作爲輸入,並返回輸入的概率大於0.我使用了一個帶有10個隱藏單元的隱藏層。完整的代碼如下所示:TensorFlow上的簡單網絡

import tensorflow as tf 
import random 

# Graph construction 

x = tf.placeholder(tf.float32, shape = [None,1]) 
y_ = tf.placeholder(tf.float32, shape = [None,1]) 

W = tf.Variable(tf.random_uniform([1,10],0.,0.1)) 
b = tf.Variable(tf.random_uniform([10],0.,0.1)) 

layer1 = tf.nn.sigmoid(tf.add(tf.matmul(x,W), b)) 

W1 = tf.Variable(tf.random_uniform([10,1],0.,0.1)) 
b1 = tf.Variable(tf.random_uniform([1],0.,0.1)) 

y = tf.nn.sigmoid(tf.add(tf.matmul(layer1,W1),b1)) 

loss = tf.square(y - y_) 

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss) 

# Training 

with tf.Session() as sess: 
    sess.run(tf.initialize_all_variables()) 
    N = 1000 
    while N != 0: 
     batch = ([],[]) 
     u = random.uniform(-10.0,+10.0) 
     if u >= 0.: 
      batch[0].append([u]) 
      batch[1].append([1.0]) 
     if u < 0.: 
      batch[0].append([u]) 
      batch[1].append([0.0]) 

     sess.run(train_step, feed_dict = {x : batch[0] , y_ : batch[1]}) 
     N -= 1 

    while(True): 
     u = raw_input("Give an x\n") 
     print sess.run(y, feed_dict = {x : [[u]]}) 

的問題是,我得到非常無關的結果。模型不會學習任何東西並返回不相關的概率。我試圖調整學習速度並更改變量初始化,但我沒有得到任何有用的東西。你有什麼建議嗎?

回答

2

您計算只有一個可能性,你想要的是有兩類:

  • 大於/小於等於零。
  • 小於零。

因此,網絡的輸出將是形狀2的張量,其將包含每個類的概率。我改名Y_在你的榜樣,以labels

labels = tf.placeholder(tf.float32, shape = [None,2]) 

接下來我們計算網絡的結果和預期的分類之間的交叉熵。正數的類別爲[1.0, 0],負數的類別爲[0.0, 1.0]。 損失函數變爲:

cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, labels) 
loss = tf.reduce_mean(cross_entropy) 

我改名爲ylogits因爲這是一個更具描述性的名稱。

訓練這個網絡爲10000個步驟得出:

Give an x 
3.0 
[[ 0.96353203 0.03686807]] 
Give an x 
200 
[[ 0.97816485 0.02264325]] 
Give an x 
-20 
[[ 0.12095013 0.87537241]] 

全碼:

import tensorflow as tf 
import random 

# Graph construction 

x = tf.placeholder(tf.float32, shape = [None,1]) 
labels = tf.placeholder(tf.float32, shape = [None,2]) 

W = tf.Variable(tf.random_uniform([1,10],0.,0.1)) 
b = tf.Variable(tf.random_uniform([10],0.,0.1)) 

layer1 = tf.nn.sigmoid(tf.add(tf.matmul(x,W), b)) 

W1 = tf.Variable(tf.random_uniform([10, 2],0.,0.1)) 
b1 = tf.Variable(tf.random_uniform([1],0.,0.1)) 

logits = tf.nn.sigmoid(tf.add(tf.matmul(layer1,W1),b1)) 

cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, labels) 

loss = tf.reduce_mean(cross_entropy) 

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss) 

# Training 

with tf.Session() as sess: 
    sess.run(tf.initialize_all_variables()) 
    N = 1000 
    while N != 0: 
     batch = ([],[]) 
     u = random.uniform(-10.0,+10.0) 
     if u >= 0.: 
      batch[0].append([u]) 
      batch[1].append([1.0, 0.0]) 
     if u < 0.: 
      batch[0].append([u]) 
      batch[1].append([0.0, 1.0]) 

     sess.run(train_step, feed_dict = {x : batch[0] , labels : batch[1]}) 

     N -= 1 

    while(True): 
     u = raw_input("Give an x\n") 
     print sess.run(logits, feed_dict = {x : [[u]]})