2016-12-24 78 views
1

"Expected binary or unicode string"此代码发生错误。我只是一名初学者,并在Anaconda 4.2.0中编写了此代码,该代码安装在Windows上并从教程中编写。在该教程中,它运行良好并打印出所有答案, 他在Mac OS X中安装的Python中运行此代码,并在其上导入tensorflow预期的二进制或Unicode字符串

我认为Windows不会让程序运行,并且有一个错误,并且它不能正常工作。任何人都可以帮助我解决这个错误?

import tensorflow as tf 
import numpy as np 

def add_layer(inputs, in_size , out_size , activation_function = None): 

    Weights = tf.Variable(tf.random_normal([in_size , out_size])) 
    biases = tf.Variable(tf.zeros([1,out_size])+0.1) 
    Wx_plus_b = tf.matmul(input , Weights)+biases 
    if activation_function is None : 
     output = Wx_plus_b 
    else: 
     output = activation_function(Wx_plus_b) 
    return output   
####### make up some real data ######### 
x_data = np.linspace(-1,1,100)[:,np.newaxis] 
noise = np.random.normal(0,0.05,x_data.shape) 
y_data = np.square(x_data) - 0.5 + noise 
###### define placeholder for inputs to network ############# 
xs = tf.placeholder(tf.float32,[None,1]) 
ys = tf.placeholder(tf.float32,[None,1]) 
###### add hidden leyer ######## 
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu) 
###### add output layer ######## 
prediction = add_layer(l1, 10, 1, activation_function = None) 
###### the error between prediction and real data ###### 
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),reduction_indices=[1])) 
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) 
###### important step ########### 
init = tf.initialize_all_variables() 
sess = tf.Session() 
sess.run(init) 
for i in range(1000): 
    ### training 
    sess.run(train_step, feed_dict={xs: x_data,ys: y_data}) 
    if i % 50 == 0: 
     #to see the step improvment  
     print(sess.run(loss,feed_dict={xs: x_data ,ys: y_data})) 
+3

总是添加有问题的完整错误消息(Traceback)。还有其他有用的信息 - 即。哪一行出问题。 – furas

+1

你用什么Python版本?教程中使用了哪些Python版本?您应该添加指向本教程的链接。 – furas

+1

另外,本教程中TensorFlow的版本是什么,您使用的是什么? –

回答

1

add_layer,键入input,而不是inputsinput是一个内置函数。

+1

tnx,这是工作。我写错了 –

相关问题