Tensorflow(二十四) —— keras实用API

Tensorflow(二十四) —— keras实用API

1. Metrics

import tensorflow as tf
from tensorflow.keras import optimizers,layers,Sequential,metrics,datasets
# ************** Metrics
1、build a meter
acc_meter = metrics.Accuracy()
loss_meter = metrics.Mean()

2、update data
acc_meter.update_state(y,pred)
loss_meter.update_state(loss)

3get average data
print(step,"loss:",loss_meter.result().numpy())
print(step,"evaulate acc:",acc_meter.result().numpy())

4、clear buffer
loss_meter.reset_states()
acc_meter.reset_states()

2. compile & fit

# compile & fit
(x,y),(x_test,y_test) = datasets.mnist.load_data()
def preprocess(x,y):
    x = tf.cast(x,dtype=tf.float32)/255.
    x = tf.reshape(x,[-1,28*28])
    y = tf.cast(y,dtype=tf.int32)
    y = tf.one_hot(y,depth=10)
    return x,y
db = tf.data.Dataset.from_tensor_slices((x,y))
db_test = tf.data.Dataset.from_tensor_slices((x_test,y_test))
db = db.map(preprocess).shuffle(10000).batch(128)
db_test = db_test.map(preprocess).shuffle(10000).batch(128)

model = Sequential([
                    layers.Dense(256,activation=tf.nn.relu),
                    layers.Dense(128,activation=tf.nn.relu),
                    layers.Dense(64,activation=tf.nn.relu),
                    layers.Dense(10)
])
model.build(input_shape=[None,28*28])
model.compile(optimizer = optimizers.Adam(lr = 1e-3),
              loss = tf.losses.CategoricalCrossentropy(from_logits = True),
             )
model.fit(db,epochs = 10)

3. evaluate

# evaluate
(x,y),(x_test,y_test) = datasets.mnist.load_data()
def preprocess(x,y):
    # x是一张照片
    x = tf.cast(x,dtype=tf.float32)/255.
    x = tf.reshape(x,[28*28])
    y = tf.cast(y,dtype=tf.int32)
    y = tf.one_hot(y,depth=10)
    return x,y

db = tf.data.Dataset.from_tensor_slices((x,y))
db_test = tf.data.Dataset.from_tensor_slices((x_test,y_test))
db = db.map(preprocess).shuffle(10000).batch(128)
db_test = db_test.map(preprocess).shuffle(10000).batch(128)

model = Sequential([
                    layers.Dense(256,activation=tf.nn.relu),
                    layers.Dense(128,activation=tf.nn.relu),
                    layers.Dense(64,activation=tf.nn.relu),
                    layers.Dense(10)
])
model.build(input_shape=[None,28*28])
model.compile(optimizer= optimizers.Adam(lr = 1e-3),
              loss = tf.losses.CategoricalCrossentropy(from_logits=True),
              metrics = ["accuracy"]
)
model.fit(db,epochs=10,validation_data=db_test,validation_freq=2)
model.evaluate(db_test)

4. predict & predict_classes

# predict & predict_classes
y_pre = model.predict(x_test.reshape([-1,28*28]))
print(y_pre)
y_pre_label = model.predict_classes(x_test.reshape([-1,28*28]))
print(y_pre_label)

本文为参考龙龙老师的“深度学习与TensorFlow 2入门实战“课程书写的学习笔记

by CyrusMay 2022 04 17

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值