[英]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. 根据训练后的网络模型(假设一个人理解),您如何不但可以传递[8,3]的张量,还可以传递[1,3]的[0,0,1]之类的东西。 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. 不幸的是,TensorFlow不允许更改图,这意味着要求输入(和中间)张量具有恒定的大小。 To distinguish between training and testing, you can use shared variables as explained here: https://www.tensorflow.org/guide/variables#sharing_variables
为了区分训练和测试,您可以按照以下说明使用共享变量: https : //www.tensorflow.org/guide/variables#sharing_variables
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.