简体   繁体   English

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

[英]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.

相关问题 使用多层感知器进行异或分类 - XOR classification using multilayer perceptron 多层感知器,目标变量为数组而不是单个值 - Multilayer perceptron with target variable as array instead of a single value 多层感知器无法收敛 - Multilayer Perceptron Fails to Converge Keras Multilayer Perceptron train data show loss = nan - Keras Multilayer Perceptron train data show loss = nan 如何在 sklearn 中更改 mlp(多层感知器)损失 function? - How to change mlp (multilayer perceptron) loss function in sklearn? 如何使用Keras训练和调整人工多层感知器神经网络? - How to train and tune an artificial multilayer perceptron neural network using Keras? 反向传播算法停留在MultiLayer Perceptron中 - Backpropagation algorithm is stuck in MultiLayer Perceptron 用于多类分类任务的多层感知器 - Multilayer Perceptron for multiclass classification task 如何将矩阵拆分为训练测试数据,同时确保训练矩阵的行和列中至少存在一个值? - how can I split matrix into training testing data whilst ensuring there is at least one value present in the rows and columns of the training matrix? 如何在感知器的批量训练期间计算偏差 - How to calculate bias during batch training of a Perceptron
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM