当前位置: 首页 > news >正文

黑龙江网站开发公司贵州省住房和城乡建设厅官网

黑龙江网站开发公司,贵州省住房和城乡建设厅官网,wordpress教程视频 下载,wordpress超级排版器插件01、Tensorflow实现二元手写数字识别#xff08;二分类问题#xff09; 开始学习机器学习啦#xff0c;已经把吴恩达的课全部刷完了#xff0c;现在开始熟悉一下复现代码。对这个手写数字实部比较感兴趣#xff0c;作为入门的素材非常合适。 基于Tensorflow 2.10.0 1、…01、Tensorflow实现二元手写数字识别二分类问题 开始学习机器学习啦已经把吴恩达的课全部刷完了现在开始熟悉一下复现代码。对这个手写数字实部比较感兴趣作为入门的素材非常合适。 基于Tensorflow 2.10.0 1、识别目标 识别手写仅仅是为了区分手写的0和1所以实际上是一个二分类问题。 2、Tensorflow算法实现 STEP1导入相关包 import numpy as np import tensorflow as tf from keras.models import Sequential from keras.layers import Dense from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt import warnings import logging from sklearn.metrics import accuracy_scoreimport numpy as np:这是引入numpy库并为其设置一个缩写np。Numpy是Python中用于大规模数值计算的库它提供了多维数组对象及一系列操作这些数组的函数。 import tensorflow as tf:这是引入tensorflow库并为其设置一个缩写tf。TensorFlow是一个开源的深度学习框架它被广泛用于各种深度学习应用。 from keras.models import Sequential:这是从Keras库中引入Sequential模型。Keras是一个高级神经网络API它可以运行在TensorFlow之上。Sequential模型是Keras中的线性堆栈模型允许你简单地堆叠多个网络层。 from keras.layers import Dense:这是从Keras库中引入Dense层。Dense层是神经网络中的全连接层每个输入节点与输出节点都是连接的。 from sklearn.model_selection import train_test_split:这是从scikit-learn库中引入train_test_split函数。这个函数用于将数据分割为训练集和测试集。 import matplotlib.pyplot as plt:这是引入matplotlib的pyplot模块并为其设置一个缩写plt。Matplotlib是Python中的绘图库而pyplot是其中的一个模块用于绘制各种图形和图像。 import warnings:这是引入Python的标准警告库它可以用来发出警告或者过滤掉不需要的警告。 import logging:这是引入Python的标准日志库用于记录日志信息方便追踪和调试代码。 from sklearn.metrics import accuracy_score:这是从scikit-learn库中引入accuracy_score函数。这个函数用于计算分类准确率常用于评估分类模型的性能。 STEP2:屏蔽无用警告并允许中文 logging.getLogger(tensorflow).setLevel(logging.ERROR) tf.autograph.set_verbosity(0) warnings.simplefilter(actionignore, categoryFutureWarning) # 支持中文显示 plt.rcParams[font.sans-serif][SimHei] plt.rcParams[axes.unicode_minus]Falselogging.getLogger(“tensorflow”).setLevel(logging.ERROR)这行代码用于设置 TensorFlow 的日志级别为 ERROR。这意味着只有当 TensorFlow 中发生错误时才会在日志中输出相关信息。较低级别的日志信息如 WARNING、INFO、DEBUG将被忽略。 tf.autograph.set_verbosity(0)这行代码用于设置 TensorFlow 的自动图形Autograph日志的冗长级别为 0。这意味着在将 Python 代码转换为 TensorFlow 图形代码时将不会输出任何日志信息。这有助于减少日志噪音使日志更加干净。 warnings.simplefilter(action‘ignore’,categoryFutureWarning)这行代码用于忽略所有 FutureWarning 类型的警告。在 Python中当使用某些即将过时或未来版本中可能发生变化的特性时通常会发出 FutureWarning。通过设置action‘ignore’代码将不会输出这类警告使控制台输出更加干净。 plt.rcParams[‘font.sans-serif’][‘SimHei’]这行代码用于设置 matplotlib 中的默认无衬线字体为 SimHei。SimHei 是一种常用于显示中文的字体这样设置后matplotlib 将在绘图时使用 SimHei 字体来显示中文从而避免中文乱码问题。 plt.rcParams[‘axes.unicode_minus’]False这行代码用于解决 matplotlib 中负号显示异常的问题。默认情况下matplotlib 可能无法正确显示负号将其设置为 False 可以使用 ASCII字符作为负号从而正常显示。 STEP3:导入并划分数据集 划分10%作为测试 X, y load_data() print(The shape of X is: str(X.shape)) print(The shape of y is: str(y.shape)) X_train, X_test, y_train, y_test train_test_split(X, y, test_size0.1, random_state42)STEP4:模型构建与训练 # 构建模型三层模型进行分类第一层输入100个神经元... model Sequential([tf.keras.Input(shape(400,)), #specify input size### START CODE HERE ###Dense(100, activationsigmoid),Dense(10, activationsigmoid),Dense(1, activationsigmoid)### END CODE HERE ###], name my_model ) # 打印三层模型的参数 model.summary() # 模型设定学习率0.001因为是分类使用BinaryCrossentropy损失函数 model.compile(losstf.keras.losses.BinaryCrossentropy(),optimizertf.keras.optimizers.Adam(0.001), ) # 开始训练训练循环20 model.fit(X_train,y_train,epochs20 ) STEP5:结果可视化与打印准确度信息 原始的输入的数据集是400 * 1000的数组共包含1000个手写数字的数据其中400为20*20像素的图片因此对每个400的数组进行reshape((20, 20))可以得到原始的图片进而绘图。 # 绘制测试集的预测结果绘制64个 fig, axes plt.subplots(8, 8, figsize(8, 8)) fig.tight_layout(pad0.1, rect[0, 0.03, 1, 0.92]) # [left, bottom, right, top] for i, ax in enumerate(axes.flat):# Select random indicesrandom_index np.random.randint(X_test.shape[0])# Select rows corresponding to the random indices and# reshape the imageX_random_reshaped X_test[random_index].reshape((20, 20)).T# Display the imageax.imshow(X_random_reshaped, cmapgray)# Predict using the Neural Networkprediction model.predict(X_test[random_index].reshape(1, 400))if prediction 0.5:yhat 1else:yhat 0# Display the label above the imageax.set_title(f{y_test[random_index, 0]},{yhat})ax.set_axis_off() fig.suptitle(真实标签, 预测的标签, fontsize16) plt.show()# 给出预测的测试集误差 y_predmodel.predict(X_test) print(测试数据集准确率为, accuracy_score(y_test, np.round(y_pred)))3、运行结果 按照最初的划分数据集包含1000个数据划分10%为测试集也就是100个数据。结果可视化随机选择其中的64个数据绘图每个图像的上方标明了其真实标签和预测的结果这个是一个非常简单的示例准确度还是非常高的。 4、工程下载与全部代码 工程链接Tensorflow实现二元手写数字识别二分类问题 import numpy as np import tensorflow as tf from keras.models import Sequential from keras.layers import Dense from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt import warnings import logging from sklearn.metrics import accuracy_scorelogging.getLogger(tensorflow).setLevel(logging.ERROR) tf.autograph.set_verbosity(0) warnings.simplefilter(actionignore, categoryFutureWarning) # 支持中文显示 plt.rcParams[font.sans-serif][SimHei] plt.rcParams[axes.unicode_minus]False# load dataset def load_data():X np.load(Handwritten_Digit_Recognition_data/X.npy)y np.load(Handwritten_Digit_Recognition_data/y.npy)X X[0:1000]y y[0:1000]return X, y# 加载数据集查看数据集大小可以看到有1000个数据集每个输入是20*20400大小的图片 X, y load_data() print(The shape of X is: str(X.shape)) print(The shape of y is: str(y.shape)) X_train, X_test, y_train, y_test train_test_split(X, y, test_size0.1, random_state42)# # 下面画图随便从原数据取出几个画图可以注释 # m, n X.shape # fig, axes plt.subplots(8, 8, figsize(8, 8)) # fig.tight_layout(pad0.1) # for i, ax in enumerate(axes.flat): # # Select random indices # random_index np.random.randint(m) # # Select rows corresponding to the random indices and # # 将1*400的数据转换为20*20的图像格式 # X_random_reshaped X[random_index].reshape((20, 20)).T # # Display the image # ax.imshow(X_random_reshaped, cmapgray) # # Display the label above the image # ax.set_title(y[random_index, 0]) # ax.set_axis_off() # plt.show()# 构建模型三层模型进行分类第一层输入25个神经元... model Sequential([tf.keras.Input(shape(400,)), #specify input size### START CODE HERE ###Dense(100, activationsigmoid),Dense(10, activationsigmoid),Dense(1, activationsigmoid)### END CODE HERE ###], name my_model ) # 打印三层模型的参数 model.summary() # 模型设定学习率0.001因为是分类使用BinaryCrossentropy损失函数 model.compile(losstf.keras.losses.BinaryCrossentropy(),optimizertf.keras.optimizers.Adam(0.001), ) # 开始训练训练循环20 model.fit(X_train,y_train,epochs20 )# 绘制测试集的预测结果绘制64个 fig, axes plt.subplots(8, 8, figsize(8, 8)) fig.tight_layout(pad0.1, rect[0, 0.03, 1, 0.92]) # [left, bottom, right, top] for i, ax in enumerate(axes.flat):# Select random indicesrandom_index np.random.randint(X_test.shape[0])# Select rows corresponding to the random indices and# reshape the imageX_random_reshaped X_test[random_index].reshape((20, 20)).T# Display the imageax.imshow(X_random_reshaped, cmapgray)# Predict using the Neural Networkprediction model.predict(X_test[random_index].reshape(1, 400))if prediction 0.5:yhat 1else:yhat 0# Display the label above the imageax.set_title(f{y_test[random_index, 0]},{yhat})ax.set_axis_off() fig.suptitle(真实标签, 预测的标签, fontsize16) plt.show()# 给出预测的测试集误差 y_predmodel.predict(X_test) print(测试数据集准确率为, accuracy_score(y_test, np.round(y_pred)))
http://www.sadfv.cn/news/397868/

相关文章:

  • 分析网站建设发展措施wordpress 手机端模板
  • 五莲网站建设报价开江建设局网站
  • 最全网站源码分享上海做网站制作
  • 临沂网站建设推广怎么样进行网络推广
  • 苏州网站排名方案wordpress 连接丢失
  • 网站seo工程师怎么做专门做网站的公司 南阳
  • 湘潭响应式网站建设 速来磐石网络WordPress获取用户的标签
  • 做网站用html怎么用外国的服务器做网站
  • 网上哪里有辅导高考生做难题的网站培训机构 网站建设
  • 网站建设预算表样本购物网站配色怎么设计
  • 门户网站程序最新国际新闻 大事件
  • 福州网站优化公司商城网站建设合同书
  • 建站推广外包苏醒wordpress
  • 义乌网站建设费用多少跨境网站有哪些
  • 桂林做网站电话号码阜新建设工程信息网站
  • 翔云白云手机网站建设做设计找图有哪些网站
  • 优惠券网站cms建设与别人相比自己网站建设优势
  • 婚纱网站源码在线探测网站开发语言
  • 社区网站建设方案书做网站 给源代码
  • 网站换服务器要怎么做网络营销课程总结范文
  • 东莞企业网站推广怎么做濮阳高端网站建设
  • 网站建设客户分析调查问卷给一个公司做网站维护
  • 局域网怎么建设网站营销型网站的建设规划
  • 昆明建网站公司长春阿凡达网络公司
  • 重庆网站建设雪奥科技网页案例图片
  • 上海企业网站模板建站哪家好wordpress面包屑导航
  • 中关村网站建设公司福田网站建设罗湖网站建设
  • 景安建网站赣州晒房网
  • 响应页手机网站源码怎么做个人公众号
  • 永定区建设局网站厦门网站开发网络公司