TensorFlow:多层感知器解决异或问题

import numpy as np
import tensorflow as tf
data_x=np.array([[0,0],[0,1],[1,0],[1,1]],dtype=np.float32)
data_y=np.array([[0],[1],[1],[0]],dtype=np.float32)

x=tf.placeholder(tf.float32,shape=[None,2],name='input')
w1=tf.Variable(tf.random_uniform([2,4],-1,1),name='w1')
b1=tf.Variable(tf.zeros(4,dtype=np.float32),name='b1')
w2=tf.Variable(tf.random_uniform([4,1],-1,1),name='w2')
b2=tf.Variable(tf.zeros(1,dtype=np.float32),name='b2')

z1=tf.sigmoid(tf.matmul(x,w1)+b1)
z2=tf.sigmoid(tf.matmul(z1,w2)+b2)

y=tf.placeholder(tf.float32,shape=[None,1],name='output')

loss=tf.nn.l2_loss(z2-y)
opt=tf.train.GradientDescentOptimizer(0.05)
train_op=opt.minimize(loss)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(100000):
        loss_val,_=sess.run([loss,train_op],feed_dict={x:data_x,y:data_y})
        if i%10000==0:
            print('{}:loss={}'.format(i,loss_val))
    graph=tf.get_default_graph()
    print(graph)

展开阅读全文

没有更多推荐了,返回首页