Python神经网络代码'语法错误:语法无效'

时间:2017-12-07 15:36:05

标签: python python-3.x tensorflow neural-network syntax-error

每当我尝试运行它时,我都会看到源代码错误。我已经尝试过很多次修复,但我找不到错误。控制台继续显示:

File "C:\Users\My Documents\LiClipse Workspace\ANN.py", line 51
print session.run(cost, feed_dict={X: x_data, Y: y_data})
            ^
SyntaxError: invalid syntax

但是,我仍然看不到任何错误..

以下是代码块:

import tensorflow as tf
import numpy as np
from pip._vendor.requests.packages.urllib3.connectionpool import xrange

x_data = np.array([
[0,0], [0,1], [1,0], [1,1]
])

y_data = np.array([
[0],[1], [1], [0]
])

n_input = 2
n_hidden = 10
n_output = 1
learning_rate = 0.1
epochs = 1000

X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

W1 = tf.Variable(tf.random_uniform([n_input, n_hidden], -1.0, 1.0))
W2 = tf.Variable(tf.random_uniform([n_hidden, n_output], -1.0, 1.0))

b1 = tf.Variable(tf.zeros([n_hidden]), name="Bias1")
b2= tf.Variable(tf.zeros([n_output]), name="Bias2")

L2 = tf.sigmoid(tf.matmul(X, W1) + b1)
hy = tf.sigmoid(tf.matmul(L2, W2) + b2)

cost = tf.reduce_mean(-Y*tf.log(hy) - (1-Y)*tf.log(1-hy))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

init = tf.initialize_all_variables()

with tf.Session() as session:
  session.run(init)

    for step in xrange(epochs):
        session.run(optimizer, feed_dict={X: x_data, Y: y_data})


        if step % 100 == 0:
            #print (cost)
            print session.run(cost, feed_dict={X: x_data, Y: y_data})


        answer = tf.equal(tf.floor(hy + 0.5), Y)
        accuracy = tf.reduce_mean(tf.cast(answer, "float"))

        print session.run([hy], feed_dict={X: x_data, Y: y_data})
        print "Accuracy: ", accuracy.eval({X: x_data, Y: y_data}) * 100, "%"

谁能告诉我错误地在哪里?提前谢谢!

0 个答案:

没有答案