开发手册 欢迎您!
软件开发者资料库

TensorFlow - XOR实施

TensorFlow XOR实施 - 从简单和简单的步骤学习TensorFlow,从基本到高级概念,包括简介,安装,理解人工智能,数学基础,机器学习和深度学习,基础知识,卷积神经网络,递归神经网络,TensorBoard可视化,单词嵌入,单层感知器,线性回归,TFLearn及其安装,CNN和RNN差异,Keras,分布式计算,TensorFlow导出,多层感知器学习,感知器隐藏层,TensorFlow中的优化器,XOR实现,梯度下降优化,形成图,使用TensorFlow的图像识别,神经网络训练的建议。

在本章中,我们将了解使用TensorFlow的XOR实现.在开始使用TensorFlow中的XOR之前,让我们看一下XOR表值.这将有助于我们了解加密和解密过程.

ABA XOR B
000
011
101
110

XOR密码加密方法主要用于加密数据使用强力方法很难破解,即通过生成与适当密钥匹配的随机加密密钥.

使用XOR密码实现的概念是定义XOR加密密钥然后执行使用此密钥对指定字符串中的字符进行XOR操作,用户尝试对其进行加密.现在我们将重点关注使用TensorFlow的XOR实现,下面提到 : 去;

#Declaring necessary modulesimport tensorflow as tfimport numpy as np"""A simple numpy implementation of a XOR gate to understand the backpropagationalgorithm"""x = tf.placeholder(tf.float64,shape = [4,2],name = "x")#declaring a place holder for input xy = tf.placeholder(tf.float64,shape = [4,1],name = "y")#declaring a place holder for desired output ym = np.shape(x)[0]#number of training examplesn = np.shape(x)[1]#number of featureshidden_s = 2 #number of nodes in the hidden layerl_r = 1#learning rate initializationtheta1 = tf.cast(tf.Variable(tf.random_normal([3,hidden_s]),name = "theta1"),tf.float64)theta2 = tf.cast(tf.Variable(tf.random_normal([hidden_s+1,1]),name = "theta2"),tf.float64)#conducting forward propagationa1 = tf.concat([np.c_[np.ones(x.shape[0])],x],1)#the weights of the first layer are multiplied by the input of the first layerz1 = tf.matmul(a1,theta1)#the input of the second layer is the output of the first layer, passed through the    activation function and column of biases is addeda2 = tf.concat([np.c_[np.ones(x.shape[0])],tf.sigmoid(z1)],1)#the input of the second layer is multiplied by the weightsz3 = tf.matmul(a2,theta2)#the output is passed through the activation function to obtain the final probabilityh3 = tf.sigmoid(z3)cost_func = -tf.reduce_sum(y*tf.log(h3)+(1-y)*tf.log(1-h3),axis = 1)#built in tensorflow optimizer that conducts gradient descent using specified    learning rate to obtain theta valuesoptimiser = tf.train.GradientDescentOptimizer(learning_rate = l_r).minimize(cost_func)#setting required X and Y values to perform XOR operationX = [[0,0],[0,1],[1,0],[1,1]]Y = [[0],[1],[1],[0]]#initializing all variables, creating a session and running a tensorflow sessioninit = tf.global_variables_initializer()sess = tf.Session()sess.run(init)#running gradient descent for each iteration and printing the hypothesis    obtained using the updated theta valuesfor i in range(100000):   sess.run(optimiser, feed_dict = {x:X,y:Y})#setting place holder values using feed_dict   if i%100==0:      print("Epoch:",i)      print("Hyp:",sess.run(h3,feed_dict = {x:X,y:Y}))

以上行代码生成输出,如下面的屏幕截图所示 :

使用TensorFlow实现XOR