简体   繁体   中英

XOR MULTILAYER PERCEPTRON: how can one pass a subset of the training data as an argument to get there prediction value

#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}))

Based on the network model(assuming one understands) after it has been trained, How could you pass tensors of not just [8,3] but be able to pass [1,3] such as [0,0,1] or something. I guess I'm rephrasing my question.

Unfortunately, TensorFlow doesn't allow the graph to change, that means that the input (and intermediate) tensors are required to have constant size. To distinguish between training and testing, you can use shared variables as explained here: https://www.tensorflow.org/guide/variables#sharing_variables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM