tensorflow 测量工具,与自定义训练
# 新建测量器
m = tf.keras.metrics.Accuracy()
# 写入测量器
m.update_state([0,1,1],[0,1,2])
# 读取统计信息
m.result() # 准确率为0.66
# 清除
m.reset_states()
acc_meter = tf.keras.metrics.Accuracy()
loss_meter = tf.keras.metrics.Mean() # 求平均loss
op = tf.keras.optimizers.Adam(0.01)
import datetime
current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
log_dir = "logs/"+current_time
summary_writer = tf.summary.create_file_writer(logdir)
for epoch in range(10):
for step,(x,y) in enumerate(train_data):
with tf.GradientTape() as tape:
loss = tf.losses.categorical_crossentropy(y,model(x))
loss_meter.update_state(loss) # 准确率
grads = tape.gradient(loss,model.train_variables) # 求梯度
op.apply_gradients(zip(grads,model.train_variables)) # 更新梯度 w = w - delta
with summary_writer.as_default()
tf.summary.scalar(name="loss",data=loss_meter.result().numpy(),step=xxxx)
print(epoch,step,loss,loss_meter.result().numpy()) # numpy() 将tensor转化为变量
loss_meter.reset_states()
for step,(x,y) in enumerate(test_data):
out = model(x)
pred = tf.argmax(out,axis=-1)
pred = tf.cast(pred,dtype=tf.int32)
y = tf.cast(tf.argmax(y,axis=-1),dtype=tf.int32)
acc_meter.update_state(y,pred)
with summary_writer.as_default()
tf.summary.scalar(name="acc",data=acc_meter.result().numpy(),step=xxxx)
print(epoch,acc_meter.result().numpy())
acc_meter.reset_states()
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,如有问题, 可评论咨询.