关于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 tf

from 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 /= 255
    return image, label

mnist_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 = 5

for 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.13200378417969

Process 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.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值