TensorFlow 上的一个简单网络

A Simple Network on TensorFlow

我试图在 TensorFlow 上训练一个非常简单的模型。模型将单个浮点数作为输入,returns 输入大于 0 的概率。我使用了 1 个隐藏层和 10 个隐藏单元。完整代码如下:

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]]})   

问题是,我得到了非常不相关的结果。模型不学习任何东西和 returns 不相关的概率。我尝试调整学习率并更改变量初始化,但没有得到任何有用的信息。你有什么建议吗?

您只计算一个概率,您想要的是有两个 classes:

  • greater/equal 比零。
  • 小于零。

因此网络的输出将是一个形状为 2 的张量,其中包含每个 class 的概率。我在您的示例中将 y_ 重命名为 labels:

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

接下来我们计算网络结果与预期 class化之间的交叉熵。正数的 classes 为 [1.0, 0],负数的 es 为 [0.0, 1.0]。 损失函数变为:

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

我将 y 重命名为 logits 因为这是一个更具描述性的名称。

训练此网络 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]]})