XOR MULTILAYER PERCEPTRON:如何将训练数据的子集作为参数传递以获得预测值

问题描述 投票:0回答:1
#imports
import tensorflow as tf
#Variables
hidden_layer1_node= 2
hidden_layer2_node= 1


X = tf.placeholder('float',[8,3])
Y = tf.placeholder('float',[8,1])

#neural model
def neural_model(x):
layer1_weight = {'weight':tf.Variable(tf.random_normal([3,hidden_layer1_node])),
                'bias':tf.Variable(tf.zeros([hidden_layer1_node]))}

layer2_weight = {'weight':tf.Variable(tf.random_normal([2,hidden_layer2_node])),
                'bias':tf.Variable(tf.zeros([hidden_layer2_node]))}


zl1 = tf.add(tf.matmul(x,layer1_weight['weight']), layer1_weight['bias'])
prediction1 = tf.sigmoid(zl1)

zl2 = tf.add(tf.matmul(prediction1,layer2_weight['weight']), layer2_weight['bias'])
return tf.sigmoid(zl2)

prediction = neural_model(X)


#cost function
def cost_function():
loss = tf.reduce_mean(-1*((Y*tf.log(prediction))+((1-Y)*tf.log(1.0-prediction))))
return loss

#Optimization
loss = cost_function()
training = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

#training stage
train_x = [[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]]
train_y = [[0],[1],[1],[0],[1],[0],[0],[1]]
epoch = 10

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for i in range(epoch):
        for _ in range(5000):
            sess.run(training, feed_dict={X:train_x,Y:train_y})

        print(sess.run(loss,feed_dict={X:train_x,Y:train_y}))
    print(sess.run(prediction,feed_dict={X:train_x,Y:train_y}))

在训练之后,基于网络模型(假设一个人理解),你怎么能通过不仅[8,3]的张量,而是能够通过[1,3],如[0,0,1]或其他东西。我想我正在改写我的问题。

python tensorflow neural-network xor
1个回答
0
投票

不幸的是,TensorFlow不允许图形改变,这意味着输入(和中间)张量需要具有恒定的大小。要区分训练和测试,可以使用共享变量,如下所述:https://www.tensorflow.org/guide/variables#sharing_variables

© www.soinside.com 2019 - 2024. All rights reserved.