栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

tensorflow 2.x 学习笔记

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

tensorflow 2.x 学习笔记

tensorflow 2.x 的特点:
  • tensorflow 2.x 默认使用Eager execution模式,简化了运行和调试。在tensorflow 1.x 中需要先构建完整的静态计算图,然后打开Session运行。
  • tensorflow 2.x 合并了Keras,简化了模型的构建。
  • 删除了冗余的API。placeholder, session, tf.layers, tf.contrib。
构建模型的三种方式
  • tf.keras.Sequential()
  • API构建模型
  • Model子类
保存模型
  • model.save() 或 tf.keras.models.save_model() 保存完整的模型
  • model.save_weights() 只保存网络模型的权重
常用的激活函数
  • tf.nn.relu
  • tf.nn.tanh
  • tf.nn.sigmoid
  • tf.nn.softmax
自动微分
  • tf.GradientTape()
示例:tensorflow实现MLP
import os
import tensorflow as tf
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split

tf.random.set_seed(2)

def load_data():
    data = load_boston()
    X_data, y_data = data.data, data.target
    y_data = y_data.reshape(y_data.shape[0], 1)
    X_train, X_test, y_train, y_test = train_test_split(X_data, y_data, test_size=0.2)
    return tf.cast(X_train, tf.float32), 
           tf.cast(X_test, tf.float32), 
           tf.cast(y_train, tf.float32), 
           tf.cast(y_test, tf.float32)

def load_weights(model, filepath):
    dirname = os.path.dirname(filepath)
    if not os.path.exists(dirname):
        os.mkdir(dirname)
        print("load weights failed")
        return False
    else:
        model.load_weights(filepath)
        print("load weights successfully")
        return True

class MLP(tf.keras.Model):
    def __init__(self):
        super(MLP, self).__init__()
        self.dense1 = tf.keras.layers.Dense(30, activation=tf.nn.relu)
        self.dropout1 = tf.keras.layers.Dropout(rate=0.5)
        self.dense2 = tf.keras.layers.Dense(20, activation=tf.nn.relu)
        self.dropout2 = tf.keras.layers.Dropout(rate=0.5)
        self.dense3 = tf.keras.layers.Dense(1)
    def call(self, inputs, training=False):
        x = self.dense1(inputs)
        if training:
            x = self.dropout1(x, training=training)
        x = self.dense2(x)
        if training:
            x = self.dropout2(x, training=training)
        outputs = self.dense3(x)
        return outputs

# 加载数据
X_train, X_test, y_train, y_test = load_data()
# 创建模型
model = MLP()
# 加载网络模型的全总
filepath = os.path.join(os.getcwd(), 'saved_models', 'mlp')
skip_train = load_weights(model, filepath)
# 优化器
optimizer = tf.keras.optimizers.Adam(learning_rate=0.005)
"""
======================================
train
======================================
"""
if skip_train:
    steps = 0
else:
    steps = 8000
    # tensorboard
    logdir = os.path.join(os.getcwd(), 'logs')
    writer = tf.summary.create_file_writer(logdir=logdir)
for step in range(1,steps+1):
    # 注意tape的作用域
    with tf.GradientTape() as tape:
        y_pred = model(X_train, training=True)
        loss = tf.reduce_mean(tf.square(y_pred - y_train))
        print('step: {}/{}, loss: {}'.format(step, steps, loss))
        with writer.as_default():
            tf.summary.scalar('loss', loss, step=step)
            writer.flush()
    grads = tape.gradient(loss, model.variables)
    optimizer.apply_gradients(zip(grads, model.variables))
"""
======================================
test
======================================
"""
y_pred = model(X_test)
mse = tf.reduce_mean(tf.square(y_pred-y_test))
print(mse)
"""
======================================
save weights
======================================
"""
model.save_weights(filepath, save_format="tf")
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1030083.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号