TensorFlow - 单层感知器

TensorFlow - 单层感知器 首页 / TensorFlow入门教程 / TensorFlow - 单层感知器

对于理解单层感知器,重要的是理解人工神经网络(ANN),人工神经网络是一种信息处理系统,其机制受到生物神经电路功能的启发,人工神经网络拥有许多相互连接的处理单元。以下是人工神经网络的示意图-

Schematic Representation

该图显示隐藏的单元与外部层通信,输入和输出单元仅通过网络的隐藏层进行通信。

有两种类型的体系结构。这些类型专注于人工神经网络的功能,如下所示-

无涯教程网

  • 单层感知器
  • 多层感知器

单层感知器

单层感知器是创建的第一个提出的神经模型。神经元局部记忆的内容由权重向量组成。单层感知器的计算是在输入向量之和与每个值乘以权重向量的相应元素的总和的计算上执行的。输出中显示的值将是激活功能的输入。

单层感知器

让无涯教程专注于使用TensorFlow解决图像分类问题的单层感知器的实现。表示单层感知器的最佳示例是通过"逻辑回归"的表示。

Logistic Regression

现在,考虑训练逻辑回归的以下基本步骤-

  • 在训练开始时,将使用随机值初始化权重。

  • 对于训练集的每个元素,均使用所需输出与实际输出之间的差来计算误差,计算出的误差用于调整权重。

  • 重复此过程,直到整个训练集上的错误不小于指定的阈值,直到达到最大迭代次数为止。

下面提到用于判断逻辑回归的完整代码-

# Import MINST data 
from tensorflow.examples.Learnfk.mnist import input_data 
mnist = input_data.read_data_sets("/tmp/data/", one_hot = True) 

import tensorflow as tf 
import matplotlib.pyplot as plt 

# 参数 
learning_rate = 0.01 
training_epochs = 25 
batch_size = 100 
display_step = 1 

# tf 图形输入
x = tf.placeholder("float", [None, 784]) # mnist data image of shape 28*28=784 
y = tf.placeholder("float", [None, 10]) # 0-9 digits recognition => 10 classes 

# 创建模型
# 设置模型权重
W = tf.Variable(tf.zeros([784, 10])) 
b = tf.Variable(tf.zeros([10])) 

# 构建模型
activation = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax 

# 使用交叉熵最小化错误
cross_entropy = y*tf.log(activation) 
cost = tf.reduce_mean\(-tf.reduce_sum\(cross_entropy,reduction_indices = 1)) 

optimizer = tf.train.\GradientDescentOptimizer(learning_rate).minimize(cost) 

#绘图设置
avg_set = [] 
epoch_set = [] 

# 初始化变量 init=tf.initialize_all_variables()
# 启动图表
with tf.Session() as sess:
   sess.run(init)
   
   # 训练周期
   for epoch in range(training_epochs):
      avg_cost = 0.
      total_batch = int(mnist.train.num_examples/batch_size)
      
      # 循环遍历所有批次
      for i in range(total_batch):
         batch_xs, batch_ys =\mnist.train.next_batch(batch_size)
         # 使用批量数据进行拟合训练 sess.run(optimizer,\feed_dict={
            x: batch_xs, y: batch_ys}) 
         # 计算平均损失 avg_cost += sess.run(cost,\feed_dict={
            x: batch_xs,\y: batch_ys})/total_batch
      # 显示每个纪元步骤的日志
      if epoch % display_step == 0:
         print ("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost))
            avg_set.append(avg_cost) epoch_set.append(epoch+1)
   print ("Training phase finished")
    
   plt.plot(epoch_set,avg_set, 'o', label = 'Logistic Regression Training phase') 
   plt.ylabel('cost') 
   plt.xlabel('epoch') 
   plt.legend() 
   plt.show() 
    
   # Test model 
   correct_prediction = tf.equal(tf.argmax(activation, 1), tf.argmax(y, 1)) 
   
   # 计算精度
   accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) print 
      ("Model accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

上面的代码生成以下输出-

Evaluation of Logistic Regression

逻辑回归被认为是一种预测分析。 Logistic回归用于描述数据并解释一个因变量和一个或多个名义或自变量之间的关系。

链接:https://www.learnfk.comhttps://www.learnfk.com/tensorflow/tensorflow-single-layer-perceptron.html

来源:LearnFk无涯教程网

Independent Variables

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

技术与商业案例解读 -〔徐飞〕

程序员的数学基础课 -〔黄申〕

OpenResty从入门到实战 -〔温铭〕

分布式协议与算法实战 -〔韩健〕

Spring编程常见错误50例 -〔傅健〕

大厂设计进阶实战课 -〔小乔〕

Web 3.0入局攻略 -〔郭大治〕

Vue 3 企业级项目实战课 -〔杨文坚〕

B端体验设计入门课 -〔林远宏(汤圆)〕

好记忆不如烂笔头。留下您的足迹吧 :)