如果两个网络流量不一样的话?

2024-06-02 02:04:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python和tensorflow(以及机器学习)的新手,这是我的第一个程序。训练后,它应该接受两个数字,如果两个数字不相等,则返回[1,0],如果相等,则返回[0,1]。在

为了这个目的,我修改了位于https://www.tensorflow.org/get_started/mnist/beginners(在图像数组中查找数字)的初学者tensorflow教程。我在1000000对上训练它,包括[0,100]范围内的等号和不等对。在

问题-训练可使准确度提高到0.5,但不高于0.5。它不是返回[0,1]或[1,0],而是返回[x,-x](其中x是浮点数)。我不确定问题是出在python代码还是tensorflow的使用上,但我怀疑这是一个愚蠢的初学者的错误。我做错什么了?在

以下是我的代码-

sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[None, 2])
y_ = tf.placeholder(tf.float32, shape=[None, 2])
W = tf.Variable(tf.zeros([2,2]))
b = tf.Variable(tf.zeros([2]))
sess.run(tf.global_variables_initializer())
y = tf.matmul(x,W) + b
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(cross_entropy)(0.2).minimize(cross_entropy)#
xinput = [[1, 2], [4, 4]]
yinput = [[1, 0], [0, 1]]
for _ in range(0, 1000000): #Generates more random training data with a mix of equal and unequal pairs        
        temp = random.randint(0, 2)
        if(temp == 0):
            t1 = random.randint(0, 100)
            xinput.append([t1, t1])
            yinput.append([0, 1])
        else:
            t1 = random.randint(0, 100)
            t2 = random.randint(0, 100)
            if(t1 != t2):
                xinput.append([t1, t2])
                yinput.append([1, 0])
train_step.run(feed_dict={x: xinput, y_: yinput})

然后我用一些测试数据来测试-

^{pr2}$

它总是打印0.5。在

以下是更多变量的输出-

print(y.eval(feed_dict={x: xeval, y_: yeval}))
Output - [[-0.0584463   0.0584463 ][-0.14445792  0.14445792][-0.289756    0.289756 ][-1.17759383  1.17759383]]

print(tf.argmax(y,1).eval(feed_dict={x: xeval, y_: yeval}))
Output - [1 1 1 1]

for var in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
    print('{0} - {1}'.format(var.name, sess.run(var)))
Output - Variable:0 - [[-0.02964323  0.02964323] [-0.0281842   0.0281842 ]]
Variable_1:0 - [-0.00061888  0.00061888]

Tags: runtftensorflowtrain数字randomvariablesess