简单理解Tensorflow2.x基础(一)

一、简单数学计算

import tensorflow as tf
import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"
tf.compat.v1.disable_eager_execution()
a=tf.constant(2)
b=tf.constant(3)
with tf.compat.v1.Session() as sess:
    print("a:%i" % sess.run(a),"b:%i" % sess.run(b))
    print("Addition with constants: %i" % sess.run(a+b))
    print("Multiplication with constant:%i" % sess.run(a*b))

或者使用函数add(a,b)和matmul(a,b)

# -!- coding: utf-8 -!-
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
tf.compat.v1.enable_eager_execution()
a = tf.constant(1)
b = tf.constant(1)
c = tf.add(a, b) # 也可以直接写 c = a + b,两者等价
print(c)
A = tf.constant([[1, 2], [3, 4]])
B = tf.constant([[5, 6], [7, 8]])
C = tf.matmul(A, B)
print(C)

二、计算梯度

# -!- coding: utf-8 -!-
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'


import tensorflow.compat.v1 as tf
tf.enable_eager_execution()
m = tf.constant(5.)
x = tf.get_variable('x', shape=[1], initializer=tf.constant_initializer(3.))
with tf.GradientTape() as tape: # 在 tf.GradientTape() 的上下文内,所有计算步骤都会被记录以用于求导
    y = tf.square(x) + m*x
    y_grad = tape.gradient(y, x) # 计算 y 关于 x 的导数
    print([y.numpy(), y_grad.numpy()])

三、计算正则化常用损失范数

# -!- coding: utf-8 -!-
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'


import tensorflow.compat.v1 as tf
tf.enable_eager_execution()
# m = tf.constant(5.)
# x = tf.get_variable('x', shape=[1], initializer=tf.constant_initializer(3.))
# with tf.GradientTape() as tape: # 在 tf.GradientTape() 的上下文内,所有计算步骤都会被记录以用于求导
#     y = tf.square(x) + m*x
#     y_grad = tape.gradient(y, x) # 计算 y 关于 x 的导数
#     print([y.numpy(), y_grad.numpy()])
X = tf.constant([[1., 2.], [3., 4.]])
y = tf.constant([[1.], [2.]])
w = tf.get_variable('w', shape=[2, 1], initializer=tf.constant_initializer([[1.], [2.]]))
b = tf.get_variable('b', shape=[1], initializer=tf.constant_initializer([1.]))
with tf.GradientTape() as tape:
    L = 0.5 * tf.reduce_sum(tf.square(tf.matmul(X, w) + b - y))
    w_grad, b_grad = tape.gradient(L, [w, b]) # 计算 L(w, b) 关于 w, b 的偏导数
    print([L.numpy(), w_grad.numpy(), b_grad.numpy()])

四、回归模型

在这里插入图片描述
简单训练一下损失

# -!- coding: utf-8 -!-
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import numpy as np
X_raw = np.array([2013, 2014, 2015, 2016, 2017])
y_raw = np.array([12000, 14000, 15000, 16500, 17500])
X = (X_raw - X_raw.min()) / (X_raw.max() - X_raw.min())
Y = (y_raw - y_raw.min()) / (y_raw.max() - y_raw.min())

a, b = 0, 0
num_epoch = 10000
learning_rate = 1e-3
for e in range(num_epoch):
    # 手动计算损失函数关于自变量(模型参数)的梯度
    y_pred = a * X + b
    grad_a, grad_b = (y_pred - Y).dot(X), (y_pred - Y).sum()
    # 更新参数
    a, b = a - learning_rate * grad_a, b - learning_rate * grad_b
    print(a, b)

整理

# -!- coding: utf-8 -!-
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import matplotlib.pyplot as plt
import tensorflow.compat.v1 as tf
tf.enable_eager_execution()

import numpy as np
X_raw = np.array([2013, 2014, 2015, 2016, 2017])
y_raw = np.array([12000, 14000, 15000, 16500, 17500])
X = (X_raw - X_raw.min()) / (X_raw.max() - X_raw.min())
Y = (y_raw - y_raw.min()) / (y_raw.max() - y_raw.min())

# a, b = 0, 0
# num_epoch = 10000
# learning_rate = 1e-3
# for e in range(num_epoch):
#     # 手动计算损失函数关于自变量(模型参数)的梯度
#     y_pred = a * X + b
#     grad_a, grad_b = (y_pred - Y).dot(X), (y_pred - Y).sum()
#     # 更新参数
#     a, b = a - learning_rate * grad_a, b - learning_rate * grad_b
#     print(a, b)


a = tf.get_variable('a', dtype=tf.float32, shape=[], initializer=tf.zeros_initializer)
b = tf.get_variable('b', dtype=tf.float32, shape=[], initializer=tf.zeros_initializer)
variables = [a, b]
num_epoch = 10000
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-3)
for e in range(num_epoch):
    # 使用 tf.GradientTape() 记录损失函数的梯度信息
    with tf.GradientTape() as tape:
        y_pred = a * X + b
        loss = 0.5 * tf.reduce_sum(tf.square(y_pred - Y))
        # TensorFlow 自动计算损失函数关于自变量(模型参数)的梯度
    grads = tape.gradient(loss, variables)
        # TensorFlow 自动根据梯度更新参数
    optimizer.apply_gradients(grads_and_vars=zip(grads, variables))


plt.figure()
Xs = X
Ys = a*Xs + b
plt.plot(Xs,Ys)     #线性回归曲线
plt.plot(X,Y,"*") #绘制点

plt.title('regression model')
plt.xlabel('x')
plt.ylabel('y')
plt.show()

在这里插入图片描述
参考文献:
https://mp.weixin.qq.com/

五、模型(Model)与层(Layer)

层可以视为比模型粒度更细的组件单位,将计算流程和变量进行
了封装。我们可以使用层来快速搭建模型。

# -!- coding: utf-8 -!-
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import matplotlib.pyplot as plt
import tensorflow.compat.v1 as tf

# import tensorflow as tf
tf.enable_eager_execution()
X = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
y = tf.constant([[10.0], [20.0]])
print(tf.keras.Model)
class Linear(tf.keras.Model):
    def __init__(self):
        super().__init__()  #继承了tf.keras.Model里面的方法
        #初始化全连接层
        self.dense = tf.keras.layers.Dense(units=1, kernel_initializer=tf.zeros_initializer(),
            bias_initializer=tf.zeros_initializer())
    def call(self, input):
        output = self.dense(input)
        return output

model = Linear()
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
for i in range(500):
    with tf.GradientTape() as tape:
        y_pred = model(X) # 调用模型
        loss = tf.reduce_mean(tf.square(y_pred - y))
    grads = tape.gradient(loss, model.variables)
    optimizer.apply_gradients(grads_and_vars=zip(grads, model.variables))
# print(model.variables)

super方法参考:
https://blog.csdn.net/weixin_41194129/article/details/112755438

https://tf.wiki/zh_hans/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源代码杀手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值