tensorflow2.0建立神经网络模型,tensorflow近似求导与keras.optimizers结合使用,实现自定义求导,使得模型训练更加灵活。tensorflow2.0学习笔记:应用tensorflow近似求导介绍tensorflow求导的基本用法。
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
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_train.shape, y_train.shape)
print(x_valid.shape, y_valid.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.transform(x_valid)
x_test_scaked = scaler.transform(x_test)
# metric 使用
# 输入的是两个列表
# 累加数据特性,具有累加功能
metric = keras.metrics.MeanSquaredError()
# MeanSquaredError:差的平方的均值
print(metric([5.],[2.])) # (5-2)*2 = 9
print(metric([0.],[1.])) # (0-1)*2 = 1
print(metric.result()) # (9+1)/2 = 5
metric.reset_states() # 清空metric之前记录的结果
metric([1.],[3.])
print(metric.result())
#fit 函数做了什么?
# 1. 按照 batch 遍历训练集,记录metric
# 1.1 自动求导
# 2.epoch 结束 验证集 记录metric
epochs = 10
batch_size = 32
step_per_epoch = len(x_train_scaled)//batch_size #整除batch_size
# 随机取32个数据,随机取32个索引
def random_batch(x,y,batch_size = 32):
idx = np.random.randint(0,len(x),size = batch_size)
x_idx= tf.cast(x[idx],dtype=tf.float32) # 取对应索引的数据
y_idx= tf.cast(y[idx],dtype=tf.float32)
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),
])
optimizer = keras.optimizers.SGD()
metric = keras.metrics.MeanSquaredError()
for epoch in range(epochs): #遍历epochs
metric.reset_states() # set metric
for step in range(step_per_epoch):
x_batch,y_batch = random_batch(x_train_scaled,y_train,batch_size) # 取数据,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) #累计计算metric
grads = tape.gradient(loss,model.variables) # 求梯度
grads_and_vars = zip(grads,model.variables) # 多个变量,需要通过zip绑定
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.numpy())