Tensorflow_keras实战(六):自定义求导(tf.GradientTape)

本文探讨了TensorFlow中tf.GradientTape与tf.keras的结合使用,实现自定义求导流程,并通过MeanSquaredError指标监测模型训练效果。在加州房价预测任务中,采用SGD优化器和MSE损失函数,展示了从数据预处理到模型训练的全过程。

目录

1.metric使用
2.tf.GradientTape与tf.keras结合实现自定义求导

1.metric使用
metric = keras.metric.MeanSquaredError()
print(metric([5.],[2.]))
print(metric([0.],[1.]))
print(metric.result())

metric.reset_states()
metric([1.],[3.])
print(metric.resulr())
2.tf.GradientTape与tf.keras结合实现自定义求导
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import pandas as pd
import os
import sklearn
import sys
import time
import tensorflow as tf

from tensorflow import keras

print(tf.__version__)
print(sys.version_info)
for module in mpl,np,pd,sklearn,tf,keras:
    print(module.__name__, module.__version__)


from sklearn.datasets import fetch_california_housing

housing = fetch_california_housing()

print(housing.DESCR)
print(housing.data.shape)
print(housing.target.shape)


from sklearn.model_selection import train_test_split

x_train_all, x_test, y_train_all, y_test = train_test_split(
    housing.data, housing.target, random_state = 7)
x_train, x_valid, y_train, y_valid = train_test_split(
    x_train_all, y_train_all, random_state = 11)

print(x_valid.shape, y_valid.shape)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)


from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(x_train)
x_valid_scaled = scaler.fit_transform(x_valid)
x_test_scaled = scaler.fit_transform(x_test)


#1. batch 遍历训练集 metric
#    1.1 自动求导
#2. epoch 验证集 metric

epochs = 100
batch_size = 32
steps_per_epoch = len(x_train_scaled) // batch_size
optimizer = keras.optimizers.SGD()
metric = keras.metrics.MeanSquaredError()

def random_batch(x,y,batch_size = 32):
    idx = np.random.ranint(0,len(x),size = batch_size)
    return x[idx],y[idx]

model = keras.models.Sequential([
    keras.layers.Dense(30,activation = 'relu',
                      input_shape = x_train.shape[1:]),
    keras.layers.Dense(1),
])

for epoch in range(epochs):
    metric.reset_states()
    for step in range(steps_per_epoch):
        x_batch,y_batch = random_batch(x_train_scaled,y_train,batch_size)
        with tf.GradientTape() as tape:
            y_pred = model(x_batch)
            loss = tf.reduce_mean(
                keras.losses.mean_squared_error(y_batch,y_pred))
            metric(y_batch,y_pred)
        grads = tape.gradient(loss,model.variables)
        grads_and_vars = zip(grads,model.variables)
        optimizer.apply_gradients(grads_and_vars)
        print("\rEpoch",epoch,"train mse:",
             metric.result().numpy(),end="")
    y_valid_pred = model(x_valid_scaled)
    valid_loss = tf.reduce_mean(
        keras.losses.mean_squared_error(y_valid_pred,y_valid))
    print("\t","valid mse: ",valid_loss.nump())
    # 效果并不太好
   
``` !mkdir -p ~/.keras/datasets !cp work/mnist.npz ~/.keras/datasets/ import warnings warnings.filterwarnings("ignore") from keras.datasets import mnist (train_images, train_labels), (test_images, test_labels) = mnist.load_data() print(f"训练数据形状: {train_images.shape}") print(f"训练标签长度: {len(train_labels)}") print(f"测试数据形状: {test_images.shape}") print(f"测试标签长度: {len(test_labels)}") from keras import models from keras import layers # 构建神经网络模型 network = models.Sequential() network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,))) # 隐藏层:512个神经元,激活函数为ReLU network.add(layers.Dense(10, activation='softmax')) # 输出层:10个分类,激活函数为Softmax # 编译模型 network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # 数据预处理 train_images = train_images.reshape((60000, 28 * 28)) # 将图像展平成一维向量 train_images = train_images.astype('float32') / 255 # 归一化到[0,1] test_images = test_images.reshape((10000, 28 * 28)) test_images = test_images.astype('float32') / 255 # 标签编码 from keras.utils import to_categorical train_labels = to_categorical(train_labels) test_labels = to_categorical(test_labels) # 训练模型 network.fit(train_images, train_labels, epochs=5, batch_size=128) # 测试模型性能 test_loss, test_acc = network.evaluate(test_images, test_labels) print('Test accuracy:', test_acc)```WARNING: Logging before flag parsing goes to stderr. W0402 07:43:05.362896 140490082953024 module_wrapper.py:139] From /opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead. W0402 07:43:05.367048 140490082953024 module_wrapper.py:139] From /opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead. W0402 07:43:05.371049 140490082953024 module_wrapper.py:139] From /opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.帮我指定出需要改哪里,要明确的位置
04-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值