如何在张量流中找到训练网络的随机输入输出?

时间:2017-04-06 19:27:26

标签: python tensorflow neural-network

所以我正在尝试编写一个预测输入数字是正数还是负数的NN,因此我对此进行了建模并进行了训练,并检查了它的准确性。但是我无法使用这个模型来明确检查一个数字是正数还是负数。我只能,检查准确性,我不能将它用于个人输入,如功能。

所以这是我的尝试;

这会创建培训数据

import numpy as np
import random
import pickle
import bitstring
from collections import Counter

def binary(num):
    f1 = bitstring.BitArray(float=num, length=32)
    return f1.bin
def num2bin(num):
    return [int(x) for x in binary(num)[0:]]

pos=10*np.random.rand(1000)
pos_test=10*np.random.rand(1000)

neg=-10*np.random.rand(1000)
neg_test=-10*np.random.rand(1000)

这会将训练数据转换为32位形式并标记它

def create_label_feature(pos,pos_test,ned,neg_test,test_size=0.1):
featuresp=[]
labelsp=[]
for x in pos:
    featuresp +=[num2bin(x)]
    labelsp +=[[1,0]]
featuresn=[]
labelsn=[]
for x in neg:
    featuresn +=[num2bin(x)]
    labelsn +=[[0,1]]
featurespt=[]
labelspt=[]
for x in pos_test:
    featurespt +=[num2bin(x)]
    labelspt +=[[1,0]]
featuresnt=[]
labelsnt=[]
for x in neg_test:
    featuresnt +=[num2bin(x)]
    labelsnt +=[[0,1]]
test_x=featuresp+featuresn
test_y=labelsp+labelsn
train_x=featurespt+featuresnt
train_y=labelspt+labelsnt

return train_x, train_y, test_x, test_y

train_x ,train_y ,test_x, test_y=create_label_feature(pos,pos_test,neg,neg_test)

这会训练NN,然后尝试确定-5是正还是负

import tensorflow as tf
#from tensorflow.examples.tutorials.mnist import input_data
import pickle
import numpy as np



n_nodes_hl1 = 1500
n_nodes_hl2 = 1500
n_nodes_hl3 = 1500

n_classes = 2
batch_size = 100
hm_epochs = 10

x = tf.placeholder('float',shape=[None,32])
y = tf.placeholder('float')

hidden_1_layer = {'f_fum':n_nodes_hl1,
                  'weight':tf.Variable(tf.random_normal([len(train_x[0]), n_nodes_hl1])),
                  'bias':tf.Variable(tf.random_normal([n_nodes_hl1]))}

# hidden_2_layer = {'f_fum':n_nodes_hl2,
#                   'weight':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
#                   'bias':tf.Variable(tf.random_normal([n_nodes_hl2]))}

# hidden_3_layer = {'f_fum':n_nodes_hl3,
#                   'weight':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
#                   'bias':tf.Variable(tf.random_normal([n_nodes_hl3]))}

output_layer = {'f_fum':None,
                'weight':tf.Variable(tf.random_normal([n_nodes_hl1, n_classes])),
                'bias':tf.Variable(tf.random_normal([n_classes])),}


# Nothing changes
def neural_network_model(data):

    l1 = tf.add(tf.matmul(data,hidden_1_layer['weight']), hidden_1_layer['bias'])
    l1 = tf.nn.relu(l1)

#     l2 = tf.add(tf.matmul(l1,hidden_2_layer['weight']), hidden_2_layer['bias'])
#     l2 = tf.nn.relu(l2)

#     l3 = tf.add(tf.matmul(l2,hidden_3_layer['weight']), hidden_3_layer['bias'])
#     l3 = tf.nn.relu(l3)

    output = tf.matmul(l1,output_layer['weight']) + output_layer['bias']

    return output

def train_neural_network(x):
    prediction = neural_network_model(x)
    cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y) )
    optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

    with tf.Session() as sess:
        saver = tf.train.Saver() 
        sess.run(tf.initialize_all_variables())

        for epoch in range(hm_epochs):
            epoch_loss = 0
            i=0
            while i < len(train_x):
                start = i
                end = i+batch_size
                batch_x = np.array(train_x[start:end])
                batch_y = np.array(train_y[start:end])

                _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
                                                              y: batch_y})
                epoch_loss += c
                i+=batch_size

            print('Epoch', epoch+1, 'completed out of',hm_epochs,'loss:',epoch_loss)
        correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))

        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))

        print('Accuracy:',accuracy.eval({x:test_x, y:test_y}))
        a=num2bin(-5)        
        a=np.reshape(a,(1,32))
#       a=a[0,:]

#       print(sess.run(prediction, {x:np.array(num2bin(5))}))
        print(sess.run(prediction, {x:a}))


train_neural_network(x)

代码的最后一部分

    a=num2bin(-5)        
    a=np.reshape(a,(1,32))
    print(sess.run(prediction, {x:a}))

所以我想看到wheter -5是正面还是负面,我希望[0,1]作为输出,因为这就是我将负数加起来的方式。

但我得到了

[[-29.49657059 123.97122192]]

那么这里有什么问题?

修改

我将以下部分添加到我的代码中;

        for k in [-9,3, 5,-8,-77,-16,54.3]:
                a=num2bin(k)        
                a=np.reshape(a,(1,32))
                a=sess.run(prediction, {x:a}) 
                prediction_tensor = tf.sigmoid(a)      
                print(sess.run(prediction_tensor))

然后我的输出是

Accuracy: 0.999933
[[ 0.          0.85150468]]
[[  8.66709650e-01   8.56608536e-32]]
[[  7.24581242e-01   8.87260485e-37]]
[[ 0.          0.66523373]]
[[ 0.  1.]]
[[  0.00000000e+00   8.08775063e-16]]
[[ 1.  0.]]

所以我的代码给出了两个组件输出,如果第一个元素大于第二个元素,则意味着输入是正数,如果不是,则意味着它是负数。

1 个答案:

答案 0 :(得分:0)

您没有对输出进行sigmoid,这是未缩放的值,看起来很正常。损失函数在应用交叉熵之前将sigmoid函数应用于这些值。您在此处看到的值是您提供给损失函数的未缩放值。如果你在不使用sigmoid的情况下查看数字,那么所有负数预测0并接近0,因为它们变得更负,并且所有正数预测1和接近1它们变得越正。这当然正是sigmoid函数正在为你做的事情。您对网络进行了培训,以便在对结果充满信心时输出大的负数或大数字。

所以,如果你对这些值应用了sigmoid你得到了你所期望的:〜[[0.00000001,0.9999999999999999999]]或者如果你舍入,[[0,1]]。

顺便提一下,对于二进制类案例,您不需要2个输出,您可以只使用一个输出,它可以是正数也可以是负数。对于一个输出而言,网络可能会比两个稍微好一点。并不是说它会很难预测正数和负数。 :)

您可以通过定义另一个张量来获得缩放(0,1)值:

prediction_tensor = tf.sigmoid(a)