Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在TensorFlow中获得一个简单的MLP来建模XOR_Python_Machine Learning_Neural Network_Xor_Tensorflow - Fatal编程技术网

Python 在TensorFlow中获得一个简单的MLP来建模XOR

Python 在TensorFlow中获得一个简单的MLP来建模XOR,python,machine-learning,neural-network,xor,tensorflow,Python,Machine Learning,Neural Network,Xor,Tensorflow,我试图构建一个简单的MLP,它有一个输入层(2个神经元)、一个隐藏层(5个神经元)和一个输出层(1个神经元)。我计划用[0,0.]、[0,1.]、[1,0.]、[1,1.]来训练它,并为它提供所需的[0,1,1,0.](元素级)输出 不幸的是,我的代码拒绝运行。不管我怎么努力,我总是会得到维度错误。非常令人沮丧:/我想我遗漏了什么,但我不知道是什么错了 为了更好的可读性,我还将代码上传到pastebin: 有什么想法吗 import tensorflow as tf ############

我试图构建一个简单的MLP,它有一个输入层(2个神经元)、一个隐藏层(5个神经元)和一个输出层(1个神经元)。我计划用
[0,0.]、[0,1.]、[1,0.]、[1,1.]
来训练它,并为它提供所需的
[0,1,1,0.]
(元素级)输出

不幸的是,我的代码拒绝运行。不管我怎么努力,我总是会得到维度错误。非常令人沮丧:/我想我遗漏了什么,但我不知道是什么错了

为了更好的可读性,我还将代码上传到pastebin:

有什么想法吗

import tensorflow as tf


#####################
# preparation stuff #
#####################

# define input and output data
input_data = [[0., 0.], [0., 1.], [1., 0.], [1., 1.]]  # XOR input
output_data = [0., 1., 1., 0.]  # XOR output

# create a placeholder for the input
# None indicates a variable batch size for the input
# one input's dimension is [1, 2]
n_input = tf.placeholder(tf.float32, shape=[None, 2])

# number of neurons in the hidden layer
hidden_nodes = 5


################
# hidden layer #
################
b_hidden = tf.Variable(0.1)  # hidden layer's bias neuron
W_hidden = tf.Variable(tf.random_uniform([hidden_nodes, 2], -1.0, 1.0))  # hidden layer's weight matrix
                                                                         # initialized with a uniform distribution
hidden = tf.sigmoid(tf.matmul(W_hidden, n_input) + b_hidden)  # calc hidden layer's activation


################
# output layer #
################
W_output = tf.Variable(tf.random_uniform([hidden_nodes, 1], -1.0, 1.0))  # output layer's weight matrix
output = tf.sigmoid(tf.matmul(W_output, hidden))  # calc output layer's activation


############
# learning #
############
cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(output, n_input)  # calc cross entropy between current
                                                                          # output and desired output
loss = tf.reduce_mean(cross_entropy)  # mean the cross_entropy
optimizer = tf.train.GradientDescentOptimizer(0.1)  # take a gradient descent for optimizing with a "stepsize" of 0.1
train = optimizer.minimize(loss)  # let the optimizer train


####################
# initialize graph #
####################
init = tf.initialize_all_variables()

sess = tf.Session()  # create the session and therefore the graph
sess.run(init)  # initialize all variables

# train the network
for epoch in xrange(0, 201):
    sess.run(train)  # run the training operation
    if epoch % 20 == 0:
        print("step: {:>3} | W: {} | b: {}".format(epoch, sess.run(W_hidden), sess.run(b_hidden)))
编辑:我仍然收到错误:/

hidden = tf.sigmoid(tf.matmul(n_input, W_hidden) + b_hidden)
输出
第27行(…)值错误:尺寸(2)和尺寸(5)不兼容
。将行更改为:

hidden = tf.sigmoid(tf.matmul(W_hidden, n_input) + b_hidden)
似乎正在工作,但错误出现在:

output = tf.sigmoid(tf.matmul(hidden, W_output))
告诉我:
第34行(…)值错误:维度(2)和维度(5)不兼容

将声明改为:

output = tf.sigmoid(tf.matmul(W_output, hidden))
还引发异常:
第34行(…)值错误:维度(1)和维度(5)不兼容

EDIT2:我真的不明白这一点。
隐藏
不应该是
W\u hidden x n\u input.t
,因为在维度中这将是
(5,2)x(2,1)
?如果我转置
n_input
hidden仍在工作(我甚至不明白为什么它在没有转置的情况下工作)。但是,
output
会不断抛出错误,但是维度中的此操作应该是
(1,5)x(5,1)

(0)包含错误输出是很有帮助的——它也是一个很有用的东西,因为它确实可以准确地识别出您在哪里遇到了形状问题

(1) 出现形状错误是因为在两个matmul中都有向后matmul的参数,并且tf.变量向后。一般规则是,具有
input\u size,output\u size
的层的权重应该是
[input\u size,output\u size]
,matmul应该是
tf.matmul(input\u to\u layer,weights\u for\u layer)
(然后添加形状为
[output\u size]
)的偏差)

所以用你的代码

W_hidden = tf.Variable(tf.random_uniform([hidden_nodes, 2], -1.0, 1.0))
应该是:

W_hidden = tf.Variable(tf.random_uniform([2, hidden_nodes], -1.0, 1.0))
sess.run(train, feed_dict={n_input: input_data}) 

应该是
tf.matmul(n_输入,W_隐藏)
;及

output = tf.sigmoid(tf.matmul(W_output, hidden))
应该是
tf.matmul(隐藏,W_输出)

(2) 一旦你修复了这些错误,你的跑步需要有一个反馈:

sess.run(train)
应该是:

W_hidden = tf.Variable(tf.random_uniform([2, hidden_nodes], -1.0, 1.0))
sess.run(train, feed_dict={n_input: input_data}) 

至少,我认为这是您试图实现的目标。

编辑2是错误的。我将编辑我的答案,指出所有向后的地方——您的W_hidden的tf.Variable声明也是向后的。它们的格式都必须是
[InputLayerSize,OutputLayerSize]
。所以第一层应该是
[2,隐藏的节点]
。MATMUL应该是
输入\u layer x这个\u layer\u weights
。非常感谢!现在一切都在运转。然而,网络学习不正常,所有输入产生的输出接近1.0。我想我的交叉熵有点不对劲。。