关于tensorflow2.0-alpha0版本,尝鲜

2023-12-06 09:58

本文主要是介绍关于tensorflow2.0-alpha0版本,尝鲜,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

关于主要更新的一些情况,可以参看这篇博客
我在这里只是想要跑一个mnist的小demo
主要代码如下(官网代码)

from __future__ import absolute_import, division, print_function
# -*- coding: utf-8 -*-
#  !pip install tensorflow-gpu==2.0.0-alpha0 ## 这行代码是安装的,如果已经安装,可以注释
import tensorflow_datasets as tfds  # 这个包需要单独安装 pip install tensorflow_datasets
import tensorflow as tffrom tensorflow.keras.layers import Dense, Flatten, Conv2D
from tensorflow.keras import Model"""Load and prepare the [MNIST dataset](http://yann.lecun.com/exdb/mnist/). Convert the samples from integers to floating-point numbers:"""
dataset, info = tfds.load('mnist', with_info=True, as_supervised=True)
mnist_train, mnist_test = dataset['train'], dataset['test']def convert_types(image, label):image = tf.cast(image, tf.float32)image /= 255return image, labelmnist_train = mnist_train.map(convert_types).shuffle(10000).batch(32)
mnist_test = mnist_test.map(convert_types).batch(32)"""Build the `tf.keras` model using the Keras 
[model subclassing API](https://www.tensorflow.org/guide/keras#model_subclassing):"""
class MyModel(Model):def __init__(self):super(MyModel, self).__init__()self.conv1 = Conv2D(32, 3, activation='relu')self.flatten = Flatten()self.d1 = Dense(128, activation='relu')self.d2 = Dense(10, activation='softmax')def call(self, x):x = self.conv1(x)x = self.flatten(x)x = self.d1(x)return self.d2(x)model = MyModel()"""Choose an optimizer and loss function for training:"""
loss_object = tf.keras.losses.SparseCategoricalCrossentropy()
optimizer = tf.keras.optimizers.Adam()"""Select metrics to measure the loss and the accuracy of the model. These metrics accumulate the values over epochs and then print the overall result."""
train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')"""Train the model using `tf.GradientTape`:"""
@tf.function  # 这个装饰器的作用,目前不是很清楚
def train_step(image, label):with tf.GradientTape() as tape:predictions = model(image)loss = loss_object(label, predictions)gradients = tape.gradient(loss, model.trainable_variables)optimizer.apply_gradients(zip(gradients, model.trainable_variables))train_loss(loss)train_accuracy(label, predictions)"""Now test the model:"""
@tf.function
def test_step(image, label):predictions = model(image)t_loss = loss_object(label, predictions)test_loss(t_loss)test_accuracy(label, predictions)EPOCHS = 5for epoch in range(EPOCHS):for image, label in mnist_train:train_step(image, label)for test_image, test_label in mnist_test:test_step(test_image, test_label)template = 'Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, Test Accuracy: {}'print (template.format(epoch+1,train_loss.result(),train_accuracy.result()*100,test_loss.result(),test_accuracy.result()*100))"""The image classifier is now trained to ~98% accuracy on this dataset. To learn more, read the [TensorFlow tutorials](https://www.tensorflow.org/alpha/tutorials/keras)."""

输出结果:

D:\Anaconda3\envs\tensorflow\python.exe E:/pythonProgram/Tensorflow/tensorflow2/advanced.py
Downloading / extracting dataset mnist (11.06 MiB) to C:\Users\Administrator\tensorflow_datasets\mnist\1.0.0...
Dl Completed...: 0 url [00:00, ? url/s]
Dl Size...: 0 MiB [00:00, ? MiB/s]Dl Completed...:   0%|          | 0/1 [00:00<?, ? url/s]
Dl Size...: 0 MiB [00:00, ? MiB/s]Dl Completed...:   0%|          | 0/2 [00:00<?, ? url/s]
Dl Size...: 0 MiB [00:00, ? MiB/s]Dl Completed...:   0%|          | 0/3 [00:00<?, ? url/s]
Dl Size...: 0 MiB [00:00, ? MiB/s]Dl Completed...:   0%|          | 0/4 [00:00<?, ? url/s]
Dl Size...: 0 MiB [00:00, ? MiB/s]Dl Completed...:   0%|          | 0/4 [00:01<?, ? url/s]
Dl Size...:   0%|          | 0/9 [00:01<?, ? MiB/s]Dl Completed...:   0%|          | 0/4 [00:01<?, ? url/s]
Dl Size...:   0%|          | 0/9 [00:01<?, ? MiB/s]Dl Completed...:  25%|██▌       | 1/4 [00:01<00:05,  1.89s/ url]
Dl Size...:   0%|          | 0/9 [00:01<?, ? MiB/s]Dl Completed...:  25%|██▌       | 1/4 [00:02<00:05,  1.89s/ url]
Dl Size...:   0%|          | 0/9 [00:02<?, ? MiB/s]Extraction completed...:   0%|          | 0/1 [00:02<?, ? file/s]Dl Completed...:  25%|██▌       | 1/4 [00:02<00:05,  1.89s/ url]
Dl Size...:   0%|          | 0/9 [00:02<?, ? MiB/s]Dl Completed...:  25%|██▌       | 1/4 [00:02<00:05,  1.89s/ url]
Dl Size...:   0%|          | 0/10 [00:02<?, ? MiB/s]Dl Completed...:  25%|██▌       | 1/4 [00:02<00:05,  1.89s/ url]
Dl Size...:   0%|          | 0/10 [00:02<?, ? MiB/s]Dl Completed...:  50%|█████     | 2/4 [00:02<00:03,  1.65s/ url]
Dl Size...:   0%|          | 0/10 [00:02<?, ? MiB/s]Dl Completed...:  50%|█████     | 2/4 [00:03<00:03,  1.65s/ url]
Dl Size...:   0%|          | 0/10 [00:03<?, ? MiB/s]Extraction completed...:  50%|█████     | 1/2 [00:03<00:02,  2.12s/ file]Dl Completed...:  50%|█████     | 2/4 [00:03<00:03,  1.65s/ url]
Dl Size...:   0%|          | 0/10 [00:03<?, ? MiB/s]Extraction completed...: 100%|██████████| 2/2 [00:03<00:00,  1.83s/ file]
Dl Completed...:  50%|█████     | 2/4 [00:30<00:03,  1.65s/ url]
Dl Size...:  10%|█         | 1/10 [00:30<04:31, 30.14s/ MiB]Extraction completed...: 100%|██████████| 2/2 [00:30<00:00,  1.83s/ file]
Dl Completed...:  50%|█████     | 2/4 [01:00<00:03,  1.65s/ url]
Dl Size...:  20%|██        | 2/10 [01:00<04:01, 30.18s/ MiB]Extraction completed...: 100%|██████████| 2/2 [01:00<00:00,  1.83s/ file]
Dl Completed...:  50%|█████     | 2/4 [01:18<00:03,  1.65s/ url]
Dl Size...:  30%|███       | 3/10 [01:18<03:05, 26.51s/ MiB]Dl Completed...:  75%|███████▌  | 3/4 [01:18<00:23, 23.78s/ url]
Dl Size...:  30%|███       | 3/10 [01:18<03:05, 26.51s/ MiB]Dl Completed...:  75%|███████▌  | 3/4 [01:18<00:23, 23.78s/ url]
Dl Size...:  30%|███       | 3/10 [01:18<03:05, 26.51s/ MiB]Extraction completed...:  67%|██████▋   | 2/3 [01:18<00:01,  1.83s/ file]Dl Completed...:  75%|███████▌  | 3/4 [01:19<00:23, 23.78s/ url]
Dl Size...:  30%|███       | 3/10 [01:19<03:05, 26.51s/ MiB]Extraction completed...: 100%|██████████| 3/3 [01:19<00:00, 24.03s/ file]
Dl Completed...:  75%|███████▌  | 3/4 [02:01<00:23, 23.78s/ url]
Dl Size...:  40%|████      | 4/10 [02:01<03:08, 31.48s/ MiB]Extraction completed...: 100%|██████████| 3/3 [02:01<00:00, 24.03s/ file]
Dl Completed...:  75%|███████▌  | 3/4 [02:44<00:23, 23.78s/ url]
Dl Size...:  50%|█████     | 5/10 [02:44<02:55, 35.02s/ MiB]Extraction completed...: 100%|██████████| 3/3 [02:44<00:00, 24.03s/ file]
Dl Completed...:  75%|███████▌  | 3/4 [03:32<00:23, 23.78s/ url]
Dl Size...:  60%|██████    | 6/10 [03:32<02:35, 38.89s/ MiB]Extraction completed...: 100%|██████████| 3/3 [03:32<00:00, 24.03s/ file]
Dl Completed...:  75%|███████▌  | 3/4 [04:10<00:23, 23.78s/ url]
Dl Size...:  70%|███████   | 7/10 [04:10<01:56, 38.68s/ MiB]Extraction completed...: 100%|██████████| 3/3 [04:10<00:00, 24.03s/ file]
Dl Completed...:  75%|███████▌  | 3/4 [04:57<00:23, 23.78s/ url]
Dl Size...:  80%|████████  | 8/10 [04:57<01:22, 41.09s/ MiB]Extraction completed...: 100%|██████████| 3/3 [04:57<00:00, 24.03s/ file]
Dl Completed...:  75%|███████▌  | 3/4 [05:37<00:23, 23.78s/ url]
Dl Size...:  90%|█████████ | 9/10 [05:37<00:40, 40.71s/ MiB]Extraction completed...: 100%|██████████| 3/3 [05:37<00:00, 24.03s/ file]
Dl Completed...:  75%|███████▌  | 3/4 [06:24<00:23, 23.78s/ url]
Dl Size...: 100%|██████████| 10/10 [06:24<00:00, 42.62s/ MiB]Dl Completed...: 100%|██████████| 4/4 [06:45<00:00, 114.80s/ url]
Dl Size...: 100%|██████████| 10/10 [06:45<00:00, 42.62s/ MiB]Dl Completed...: 100%|██████████| 4/4 [06:45<00:00, 114.80s/ url]
Dl Size...: 100%|██████████| 10/10 [06:45<00:00, 42.62s/ MiB]Extraction completed...:  75%|███████▌  | 3/4 [06:45<00:24, 24.03s/ file]Dl Completed...: 100%|██████████| 4/4 [06:49<00:00, 114.80s/ url]
Dl Size...: 100%|██████████| 10/10 [06:49<00:00, 42.62s/ MiB]Extraction completed...: 100%|██████████| 4/4 [06:49<00:00, 115.90s/ file]60000 examples [00:20, 2938.42 examples/s]
Shuffling...:   0%|          | 0/10 [00:00<?, ? shard/s]WARNING: Logging before flag parsing goes to stderr.
W0330 15:32:55.947226  7452 deprecation.py:323] From D:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_datasets\core\file_format_adapter.py:249: tf_record_iterator (from tensorflow.python.lib.io.tf_record) is deprecated and will be removed in a future version.
Instructions for updating:
Use eager execution and: 
`tf.data.TFRecordDataset(path)`Reading...: 0 examples [00:00, ? examples/s]
Reading...: 6000 examples [00:00, 109084.63 examples/s]
Writing...:   0%|          | 0/6000 [00:00<?, ? examples/s]
Writing...:  59%|█████▉    | 3549/6000 [00:00<00:00, 35488.00 examples/s]
Shuffling...:  10%|█         | 1/10 [00:00<00:05,  1.63 shard/s]
Reading...: 0 examples [00:00, ? examples/s]
Reading...: 6000 examples [00:00, 146333.35 examples/s]
Writing...:   0%|          | 0/6000 [00:00<?, ? examples/s]
Writing...:  58%|█████▊    | 3506/6000 [00:00<00:00, 35058.03 examples/s]
Shuffling...:  20%|██        | 2/10 [00:01<00:05,  1.58 shard/s]
Reading...: 0 examples [00:00, ? examples/s]
Reading...: 6000 examples [00:00, 127651.34 examples/s]
Writing...:   0%|          | 0/6000 [00:00<?, ? examples/s]
Writing...:  58%|█████▊    | 3500/6000 [00:00<00:00, 34997.95 examples/s]
Shuffling...:  30%|███       | 3/10 [00:01<00:04,  1.73 shard/s]
Reading...: 0 examples [00:00, ? examples/s]
Reading...: 6000 examples [00:00, 122441.77 examples/s]
Writing...:   0%|          | 0/6000 [00:00<?, ? examples/s]
Writing...:  57%|█████▋    | 3435/6000 [00:00<00:00, 34348.07 examples/s]
Shuffling...:  40%|████      | 4/10 [00:02<00:03,  1.79 shard/s]
Reading...: 0 examples [00:00, ? examples/s]
Reading...: 6000 examples [00:00, 139527.20 examples/s]
Writing...:   0%|          | 0/6000 [00:00<?, ? examples/s]
Writing...:  58%|█████▊    | 3509/6000 [00:00<00:00, 35088.03 examples/s]
Shuffling...:  50%|█████     | 5/10 [00:02<00:02,  1.85 shard/s]
Reading...: 0 examples [00:00, ? examples/s]
Reading...: 6000 examples [00:00, 153838.49 examples/s]
Writing...:   0%|          | 0/6000 [00:00<?, ? examples/s]
Writing...:  60%|█████▉    | 3581/6000 [00:00<00:00, 35807.99 examples/s]
Shuffling...:  60%|██████    | 6/10 [00:03<00:02,  1.81 shard/s]
Reading...: 0 examples [00:00, ? examples/s]
Reading...: 6000 examples [00:00, 130428.01 examples/s]
Writing...:   0%|          | 0/6000 [00:00<?, ? examples/s]
Writing...:  60%|█████▉    | 3588/6000 [00:00<00:00, 35877.90 examples/s]
Shuffling...:  70%|███████   | 7/10 [00:03<00:01,  1.89 shard/s]
Reading...: 0 examples [00:00, ? examples/s]
Reading...: 6000 examples [00:00, 124992.92 examples/s]
Writing...:   0%|          | 0/6000 [00:00<?, ? examples/s]
Writing...:  60%|██████    | 3621/6000 [00:00<00:00, 36207.96 examples/s]
Shuffling...:  80%|████████  | 8/10 [00:04<00:01,  1.92 shard/s]
Reading...: 0 examples [00:00, ? examples/s]
Reading...: 6000 examples [00:00, 166655.57 examples/s]
Writing...:   0%|          | 0/6000 [00:00<?, ? examples/s]
Writing...:  60%|█████▉    | 3582/6000 [00:00<00:00, 35817.98 examples/s]
Shuffling...:  90%|█████████ | 9/10 [00:04<00:00,  1.97 shard/s]
Reading...: 0 examples [00:00, ? examples/s]
Reading...: 6000 examples [00:00, 149992.10 examples/s]
Writing...:   0%|          | 0/6000 [00:00<?, ? examples/s]
Writing...:  59%|█████▊    | 3521/6000 [00:00<00:00, 35207.94 examples/s]
Shuffling...: 100%|██████████| 10/10 [00:05<00:00,  1.94 shard/s]
10000 examples [00:03, 3030.13 examples/s]
Shuffling...:   0%|          | 0/1 [00:00<?, ? shard/s]
Reading...: 0 examples [00:00, ? examples/s]
Reading...: 10000 examples [00:00, 181807.72 examples/s]
Writing...:   0%|          | 0/10000 [00:00<?, ? examples/s]
Writing...:  34%|███▍      | 3407/10000 [00:00<00:00, 34068.08 examples/s]
Writing...:  69%|██████▉   | 6922/10000 [00:00<00:00, 34385.01 examples/s]
Shuffling...: 100%|██████████| 1/1 [00:00<00:00,  1.32 shard/s]
2019-03-30 15:33:07.617894: W .\tensorflow/core/framework/model.h:202] Encountered a stop event that was not preceded by a start event.
Epoch 1, Loss: 0.1331276297569275, Accuracy: 95.9949951171875, Test Loss: 0.0622599795460701, Test Accuracy: 97.94999694824219
2019-03-30 15:34:36.883000: W .\tensorflow/core/framework/model.h:202] Encountered a stop event that was not preceded by a start event.
Epoch 2, Loss: 0.087999127805233, Accuracy: 97.3316650390625, Test Loss: 0.05821216478943825, Test Accuracy: 98.04999542236328
2019-03-30 15:35:55.022469: W .\tensorflow/core/framework/model.h:202] Encountered a stop event that was not preceded by a start event.
Epoch 3, Loss: 0.06530821323394775, Accuracy: 98.00444030761719, Test Loss: 0.05837463214993477, Test Accuracy: 98.15333557128906
2019-03-30 15:37:11.460841: W .\tensorflow/core/framework/model.h:202] Encountered a stop event that was not preceded by a start event.
Epoch 4, Loss: 0.052207402884960175, Accuracy: 98.39083862304688, Test Loss: 0.06129618361592293, Test Accuracy: 98.15250396728516
2019-03-30 15:38:27.955216: W .\tensorflow/core/framework/model.h:202] Encountered a stop event that was not preceded by a start event.
Epoch 5, Loss: 0.04351149871945381, Accuracy: 98.65533447265625, Test Loss: 0.06512587517499924, Test Accuracy: 98.13200378417969Process finished with exit code 0

以上代码相比较于tensorflow1.x来说,较为清爽

  • 数据流在网络中的流向,可以像PyTorch那样,直接加断点,查看x在经过各个网络层的前后value和shape的变化
  • 有一点我想说明的是,以上代码中的 train_step()test_step() 函数,在pycharm中,不可以通过加断点的方式,进入查看,具体运算情况(比较纳闷)。
  • 再有,就是代码中的train_loss,train_accuracy,test_loss, test_accuracy这四个对象,很神奇,我甚至没有看到它们的运算过程,最后竟然可以输出train_loss.result()

关于tensorflow2.0的初体验,就到这里。
目前来看,我还是倾向于PyTorch.

这篇关于关于tensorflow2.0-alpha0版本,尝鲜的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/461420

相关文章

Ubuntu如何升级Python版本

《Ubuntu如何升级Python版本》Ubuntu22.04Docker中,安装Python3.11后,使用update-alternatives设置为默认版本,最后用python3-V验证... 目China编程录问题描述前提环境解决方法总结问题描述Ubuntu22.04系统自带python3.10,想升级

更改linux系统的默认Python版本方式

《更改linux系统的默认Python版本方式》通过删除原Python软链接并创建指向python3.6的新链接,可切换系统默认Python版本,需注意版本冲突、环境混乱及维护问题,建议使用pyenv... 目录更改系统的默认python版本软链接软链接的特点创建软链接的命令使用场景注意事项总结更改系统的默

Linux升级或者切换python版本实现方式

《Linux升级或者切换python版本实现方式》本文介绍在Ubuntu/Debian系统升级Python至3.11或更高版本的方法,通过查看版本列表并选择新版本进行全局修改,需注意自动与手动模式的选... 目录升级系统python版本 (适用于全局修改)对于Ubuntu/Debian系统安装后,验证Pyt

MySQL 升级到8.4版本的完整流程及操作方法

《MySQL升级到8.4版本的完整流程及操作方法》本文详细说明了MySQL升级至8.4的完整流程,涵盖升级前准备(备份、兼容性检查)、支持路径(原地、逻辑导出、复制)、关键变更(空间索引、保留关键字... 目录一、升级前准备 (3.1 Before You Begin)二、升级路径 (3.2 Upgrade

Nginx进行平滑升级的实战指南(不中断服务版本更新)

《Nginx进行平滑升级的实战指南(不中断服务版本更新)》Nginx的平滑升级(也称为热升级)是一种在不停止服务的情况下更新Nginx版本或添加模块的方法,这种升级方式确保了服务的高可用性,避免了因升... 目录一.下载并编译新版Nginx1.下载解压2.编译二.替换可执行文件,并平滑升级1.替换可执行文件

在macOS上安装jenv管理JDK版本的详细步骤

《在macOS上安装jenv管理JDK版本的详细步骤》jEnv是一个命令行工具,正如它的官网所宣称的那样,它是来让你忘记怎么配置JAVA_HOME环境变量的神队友,:本文主要介绍在macOS上安装... 目录前言安装 jenv添加 JDK 版本到 jenv切换 JDK 版本总结前言China编程在开发 Java

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

MySQL版本问题导致项目无法启动问题的解决方案

《MySQL版本问题导致项目无法启动问题的解决方案》本文记录了一次因MySQL版本不一致导致项目启动失败的经历,详细解析了连接错误的原因,并提供了两种解决方案:调整连接字符串禁用SSL或统一MySQL... 目录本地项目启动报错报错原因:解决方案第一个:第二种:容器启动mysql的坑两种修改时区的方法:本地

conda安装GPU版pytorch默认却是cpu版本

《conda安装GPU版pytorch默认却是cpu版本》本文主要介绍了遇到Conda安装PyTorchGPU版本却默认安装CPU的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录一、问题描述二、网上解决方案罗列【此节为反面方案罗列!!!】三、发现的根本原因[独家]3.1 p

Redis指南及6.2.x版本安装过程

《Redis指南及6.2.x版本安装过程》Redis是完全开源免费的,遵守BSD协议,是一个高性能(NOSQL)的key-value数据库,Redis是一个开源的使用ANSIC语言编写、支持网络、... 目录概述Redis特点Redis应用场景缓存缓存分布式会话分布式锁社交网络最新列表Redis各版本介绍旧