2016-03-05 182 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]]})