TensorFlow第一次尝试,结果不好

时间:2016-08-31 13:51:20

标签: tensorflow

我无法解决我的问题,请帮助我。这是我神经网络的第一次尝试,我试图制作nn,它可以检查是否为(3:6)之间的数字。我在互联网上使用了几个文档并进行了一些列表。但它没有取得成效。它总是"不在(3:6)"。我无法理解我做错了什么。

#Is number between (3:6)
import tensorflow as tf
import numpy as np
import random

def is_num_between(num):
    right_border = 6
    left_border = 3
    if num < right_border and num > left_border:
        return 1
    return 0

def is_num_around(num):
    right_border = 6
    left_border = 3
    if num <= left_border or num >= right_border:
        return 1
    return 0

def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))

def model(X, w_h, w_o):
    h = tf.nn.tanh(tf.matmul(X, w_h))
    return tf.nn.sigmoid(tf.matmul(h, w_o))

def included_or_not(i, prediction):
    return [str(i) + " is in (3:6)", str(i) + " not in (3:6)"][prediction]

NUM_COUNT = 2
NUM_HIDDEN = 10
BATCH_SIZE = 10000

pre_trX = [np.random.random_sample() * 10 for i in range(100000)]
pre_trY1 = [is_num_between(i) for i in pre_trX]
pre_trY2 = [is_num_around(i) for i in pre_trX]

trX = np.array([np.array([pre_trX[i], 1]) for i in range(len(pre_trX))])
trY = np.array([np.array([pre_trY1[i], pre_trY2[i]]) for i in range(len(pre_trX))])


# print(type(trX))
# print(pre_trX)
# print(pre_trY1)
# print(pre_trY2)
# print(trX[0])
# exit()

X = tf.placeholder("float", [None, NUM_COUNT])
Y = tf.placeholder("float", [None, 2])

w_h = init_weights([NUM_COUNT, NUM_HIDDEN])
w_o = init_weights([NUM_HIDDEN, 2])

py_X = model(X, w_h, w_o)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_X, Y))
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

predict_op = tf.argmax(py_X, 1)


with tf.Session() as sess:
    tf.initialize_all_variables().run()

    for epoch in range(200):
        p = np.random.permutation(range(len(trX)))
        trX, trY = trX[p], trY[p]

        for start in range(0, len(trX), BATCH_SIZE):
            end = start + BATCH_SIZE
            sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})

        print(epoch, np.mean(np.argmax(trY, axis=1) ==
                         sess.run(predict_op, feed_dict={X: trX, Y: trY})))


    # Tipo natrenirovana, nado ee potestit
    def check_nnetwork():
        numbers = [np.array([np.random.random_sample()*10, 1])]
        teX = np.array(numbers)
        teY = sess.run(predict_op, feed_dict={X: teX})
        output = np.vectorize(included_or_not)("%.3f" % numbers[0][0], teY)
        print(output)

    for i in range(40):
        check_nnetwork()

1 个答案:

答案 0 :(得分:0)

你的损失功能是什么样的? 与负面例子相比,还有多少正面例子?如果数据过于偏斜,它可能会学会总是预测为负数,因为这会使损失函数最小化。

另一个问题可能是您的架构存在一个基本问题,即您希望单级神经网络学习一种实际上不可能的非线性函数。