Tensorflow学习具有线性函数的XOR,即使它不应该

时间:2017-02-12 18:41:10

标签: tensorflow neural-network

我在tensorflow和教学目的中实现一个简单的网络,我试图表明线性转换:

<?php 
date_default_timezone_set('Asia/Kolkata');

session_start();
include"../includes/config.php"; //connects to the database
global $con;

DEFINE('EMAIL', 'testemail@email.com');
DEFINE('WEBSITE_URL', 'http://website.com/');

if (isset($_POST['btn-send'])){

    //echo $_POST['user_email'];
    //exit;

    $error = array();

     if (empty($_POST['user_email'])) { 
        $error[] = 'You forgot to enter  your Email ';
        $msg_error_email_empty= 'You forgot to enter  your Email';
    } else {
            $useremail_recov = mysqli_real_escape_string($con, $_POST['user_email']);        
            $useremail_recov = stripslashes($_POST['user_email']);        
    }

    $query = "SELECT * FROM profile_details WHERE email='$useremail_recov'";
    $result = mysqli_query($con, $query);
    $count = mysqli_num_rows($result);

    if($count==0){
        //echo "Email id is not registered";die();
        $error[] = 'Sorry ! Email id is not registered ';
        $msg_error_email= 'Sorry ! Email id is not registered';
    }

    // If the count is equal to one, we will send message other wise display an error message.
        if($count==1)
        {
            $ip = $_SERVER['REMOTE_ADDR'];
            $httpref = $_SERVER['HTTP_REFERER'];
            $httpagent = $_SERVER['HTTP_USER_AGENT'];
            $date = date('F j, Y H:i:s'); 
            $token = md5(uniqid(rand(), true));

            $query = "UPDATE profile_details SET tokenCode='$token' WHERE email='$useremail_recov'";

            $result_pass_reset = mysqli_query($con, $query) or die (mysqli_error($con));

            if (!$result_pass_reset) {
                echo 'Query Failed ';
            }

            if (mysqli_affected_rows($con) == 1) { //If the Insert Query was successfull.

            // Send the email for account activation:

            $to = "$useremail_recov";
            $subject = "Reset Password";
            $from = "Alert@website.com";
            $message = "<br /><br /><b style='font-size:100%;'>
                Hello $useremail_recov,
                <br /><br /></b>

                Welcome to website !!,<br />
                Somebody recently asked to reset your password, if you do this then just click the following link,<br /> 
                If you didn't request a new password, let us know immediately.<br /><br /><br />

                Click Following Link To RESET Your Password <br /><br />

                <a href='". WEBSITE_URL ."/reset_password.php?email=" . urlencode($useremail_recov) . "&key=$token'><b style='#fff:Blue; border-style: dotted; background-color: green;'><button>REST YOUR PASSWORD</button></b></a> <br /><br />


                Ip Address is : <b> $ip </b><br /><br />
                Browser is : <b> $httpagent </b><br /><br />
                Referral : <b> $httpref </b><br /><br />
                Time : <b> $date </b><br /><br />


                For further assistent contact on <b style='color:red'> info@website.com (+91 99213 50058). </b> <br />
                Keep Your account safe and secure from others.
                <br /><br /><br /><br />
                Thank you <br />
                Sincerely,<br />
                Team website.<br />

                <br />
                <br />
                <br />
        -------------------------------------------------------------------------------------------------------------------------------------------------<br />
                <b><u>Note:</u></b> This is password reset mail hence, Please <b>do not reply </b>to this email. <br />
                Emails sent to this address will not be answered.<br />
                Copyright © 2015-2017 website. All rights reserved.     
            ";

            $headers = "From: " . strip_tags($from) . "\r\n";
            $headers .= "Reply-To: ". strip_tags($useremail_recov) . "\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

            $sentmail = mail($to,$subject,$message,$headers);

                // Finish the page:
                $msg = "
                    <div class='alert alert-success'>
                        <button class='close' data-dismiss='alert'>&times;</button>
                        <strong>Success !</strong>  We've sent an email to $useremail_recov.
                    Please click on the confirmation link in the email to reset your password. 
                    </div>
                    ";

                    }
                 else { // If it did not run OK.
                $msg_fail = "
                    <div class='alert alert-danger'>
                        <button class='close' data-dismiss='alert'>&times;</button>
                        <strong>OOPS !</strong>  You could not be reset your password due to a system
                        error. We apologize for any inconvenience. 
                    </div>
                    ";
                }

        }

    mysqli_close($con);//Close the DB Connection

} // End of the main Submit conditional.

?>

无法学习异或。但现在的问题是,在目前的实施中,它确实存在!这表明代码中存在错误。请澄清一下?

yhat = w(Wx + c) + b

现在,准确度为############################################################ ''' dummy data ''' x_data = [[0.,0.],[0.,1.],[1.,0.],[1.,1.]] y_data = [[0],[1],[1],[0]] ############################################################ ''' Input and output ''' X = tf.placeholder(tf.float32, shape = [4,2], name = 'x') Y = tf.placeholder(tf.float32, shape = [4,1], name = 'y') ''' Network parameters ''' W = tf.Variable(tf.random_uniform([2,2],-1,1), name = 'W') c = tf.Variable(tf.zeros([2]) , name = 'c') w = tf.Variable(tf.random_uniform([2,1],-1,1), name = 'w') b = tf.Variable(tf.zeros([1]) , name = 'b') ############################################################ ''' Network 1: function: Yhat = (w (x'W + c) + b) loss : \sum_i Y * log Yhat ''' H1 = tf.matmul(X, W) + c Yhat1 = tf.matmul(H1, w) + b cross_entropy1 = -tf.reduce_sum( Y*tf.log( tf.clip_by_value(Yhat1,1e-10,1.0) ) ) step1 = tf.train.AdamOptimizer(0.01).minimize(cross_entropy1) ''' Train ''' writer = tf.train.SummaryWriter("./logs/xor_logs.graph_def") graph1 = tf.initialize_all_variables() sess1 = tf.Session() sess1.run(graph1) for i in range(100): sess1.run(step1, feed_dict={X: x_data, Y: y_data}) ''' Evaluation ''' corrects = tf.equal(tf.argmax(Y,1), tf.argmax(Yhat1,1)) accuracy = tf.reduce_mean(tf.cast(corrects, tf.float32)) r = sess1.run(accuracy, feed_dict={X: x_data, Y: y_data}) print ('accuracy: ' + str(r * 100) + '%') ,即使它应该在100%

1 个答案:

答案 0 :(得分:1)

tf.argmax(Y,1)将返回[0,0,0,0]。这不是你想要的。