深度学习2.0-20.Keras高层API-metrics

1147-柳同学

发表文章数:593

首页 » 算法 » 正文


深度学习2.0-20.Keras高层API-metrics

1.metrics

深度学习2.0-20.Keras高层API-metrics

1.metrics-新建一个评价指标

acc_meter = metrucs.Accuracy()
loss_meter = metrics.Mean()

2.update data- 添加数据

loss_meter.update_state(loss)
acc_meter.update_state(y,pred)

3.result().numpy()-显示结果

print(step,'loss:', loss_meter.result().numpy())
...
print(step,'Evaluate Acc:',total_correct/total, acc_meter.result().numpy())

4.reset_states()-清零

if step % 100 == 0:
	print(step,'loss:', loss_meter.result().numpy())
	# 清除上一个时间戳的数据
	loss_meter.reset_states()

if step % 500 == 0:
	print(step,'Evaluate Acc:',total_correct/total, acc_meter.result().numpy())
	acc_meter.reset_states()

2.metrics实战

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import tensorflow as tf
from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics

# 预处理函数
def preprocess(x, y):
    x = tf.cast(x, dtype=tf.float32) / 255.
    y = tf.cast(y, dtype=tf.int32)

    return x, y


batchsz = 128
(x, y), (x_val, y_val) = datasets.mnist.load_data()
print('datasets:', x.shape, y.shape, x.min(), x.max())

db = tf.data.Dataset.from_tensor_slices((x, y))
db = db.map(preprocess).shuffle(60000).batch(batchsz).repeat(10)

ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val))
ds_val = ds_val.map(preprocess).batch(batchsz)

# 构建多层网络
network = Sequential([layers.Dense(256, activation='relu'),
                      layers.Dense(128, activation='relu'),
                      layers.Dense(64, activation='relu'),
                      layers.Dense(32, activation='relu'),
                      layers.Dense(10)])
network.build(input_shape=(None, 28 * 28))
network.summary()

# 优化器
optimizer = optimizers.Adam(lr=0.01)

# 评价指标-acc,loss
acc_meter = metrics.Accuracy()
loss_meter = metrics.Mean()


for step, (x, y) in enumerate(db):

    with tf.GradientTape() as tape:
        # [b, 28, 28] => [b, 784]
        x = tf.reshape(x, (-1, 28 * 28))
        # [b, 784] => [b, 10]
        out = network(x)
        # [b] => [b, 10]
        y_onehot = tf.one_hot(y, depth=10)
        # [b]
        loss = tf.reduce_mean(tf.losses.categorical_crossentropy(y_onehot, out, from_logits=True))

        loss_meter.update_state(loss)

    grads = tape.gradient(loss, network.trainable_variables)
    optimizer.apply_gradients(zip(grads, network.trainable_variables))

    if step % 100 == 0:
        print(step, 'loss:', loss_meter.result().numpy())
        loss_meter.reset_states()

    # evaluate
    if step % 500 == 0:
        total, total_correct = 0., 0
        acc_meter.reset_states()

        for step, (x, y) in enumerate(ds_val):
            # [b, 28, 28] => [b, 784]
            x = tf.reshape(x, (-1, 28 * 28))
            # [b, 784] => [b, 10]
            out = network(x)

            # [b, 10] => [b]
            pred = tf.argmax(out, axis=1)
            pred = tf.cast(pred, dtype=tf.int32)
            # bool type
            correct = tf.equal(pred, y)
            # bool tensor => int tensor => numpy
            total_correct += tf.reduce_sum(tf.cast(correct, dtype=tf.int32)).numpy()
            total += x.shape[0]

            acc_meter.update_state(y, pred)

        print(step, 'Evaluate Acc:', total_correct / total, acc_meter.result().numpy())

datasets: (60000, 28, 28) (60000,) 0 255
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                multiple                  200960    
_________________________________________________________________
dense_1 (Dense)              multiple                  32896     
_________________________________________________________________
dense_2 (Dense)              multiple                  8256      
_________________________________________________________________
dense_3 (Dense)              multiple                  2080      
_________________________________________________________________
dense_4 (Dense)              multiple                  330       
=================================================================
Total params: 244,522
Trainable params: 244,522
Non-trainable params: 0
_________________________________________________________________
0 loss: 2.3095727
78 Evaluate Acc: 0.1032 0.1032
100 loss: 0.49836162
200 loss: 0.24281283
300 loss: 0.20814449
400 loss: 0.19040857
500 loss: 0.1471103
78 Evaluate Acc: 0.956 0.956
600 loss: 0.15806517
700 loss: 0.13501912
800 loss: 0.13778095
900 loss: 0.13771541
1000 loss: 0.11204889
78 Evaluate Acc: 0.9666 0.9666
1100 loss: 0.10818114
1200 loss: 0.10698662
1300 loss: 0.10993517
1400 loss: 0.10309881
1500 loss: 0.092004016
78 Evaluate Acc: 0.9658 0.9658
1600 loss: 0.09988546
1700 loss: 0.09517718
1800 loss: 0.102653
1900 loss: 0.10128655
2000 loss: 0.084593534
78 Evaluate Acc: 0.9696 0.9696
2100 loss: 0.089395694
2200 loss: 0.084114745
2300 loss: 0.08294669
2400 loss: 0.0765419
2500 loss: 0.07786285
78 Evaluate Acc: 0.9716 0.9716
2600 loss: 0.08739958
2700 loss: 0.08950595
2800 loss: 0.08106578
2900 loss: 0.06466477
3000 loss: 0.077431396
78 Evaluate Acc: 0.9707 0.9707
3100 loss: 0.08382876
3200 loss: 0.076059125
3300 loss: 0.07230227
3400 loss: 0.05853687
3500 loss: 0.07312769
78 Evaluate Acc: 0.9703 0.9703
3600 loss: 0.07384481
3700 loss: 0.08926408
3800 loss: 0.066682965
3900 loss: 0.05534654
4000 loss: 0.073996484
78 Evaluate Acc: 0.9741 0.9741
4100 loss: 0.066883035
4200 loss: 0.070191935
4300 loss: 0.08581101
4400 loss: 0.07324687
4500 loss: 0.056211904
78 Evaluate Acc: 0.9751 0.9751
4600 loss: 0.05384313

未经允许不得转载:作者:1147-柳同学, 转载或复制请以 超链接形式 并注明出处 拜师资源博客
原文地址:《深度学习2.0-20.Keras高层API-metrics》 发布于2020-10-10

分享到:
赞(0) 打赏

评论 抢沙发

评论前必须登录!

  注册



长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

Vieu3.3主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录