# Tensorflow 1 + 1
import tensorflow as tf
import numpy as np
# random_float = tf.random.uniform(shape=())
# zero_vec = tf.zeros(shape=(2))
#
#
# A = tf.constant([[1., 2.], [3., 4.]])
# B = tf.constant([[5., 6.], [7., 8.]])
#
# print(A.shape)
# print(A.dtype)
# print(A.numpy())
# print(B.numpy())
#
#
# C = tf.add(A, B)
# D = tf.matmul(A, B)
# print("C = ", C.numpy())
# print("D = ", D.numpy())
# a=1
# 自动求导机制
# x = tf.Variable(initial_value=3.)
# with tf.GradientTape() as tape:
# y = tf.square(x)
#
# y_grad = tape.gradient(y, x)
# print(y, y_grad)
# 自动求导机制-多元函数求偏导
# X = tf.constant([[1., 2.], [3., 4.]])
# y = tf.constant([[1.], [2.]])
#
# w = tf.Variable(initial_value=[[1.], [2.]])
# print(w.shape)
# b = tf.Variable(initial_value=1.)
# print(tf.matmul(X, w))
# print(tf.matmul(X, w) + b)
# print(tf.matmul(X, w) + b-y)
# print(tf.square(tf.matmul(X, w) + b - y))
#
# with tf.GradientTape() as tape:
# L = tf.reduce_sum(tf.square(tf.matmul(X, w) + b - y))
#
# w_grad, b_grad = tape.gradient(L, [w, b]) # 计算(w, b) 关于w, b的偏导
# print("L:", L)
# print("w_grad:", w_grad)
# print("b_grad:", b_grad)
# 线性回归示例 - numpy
X_raw = np.array([2013, 2014, 2015, 2016, 2017], dtype=np.float32)
y_raw = np.array([12000, 14000, 15000, 16500, 17500], dtype=np.float32)
X = (X_raw - X_raw.min()) / (X_raw.max() - X_raw.min())
y = (y_raw - y_raw.min()) / (y_raw.max() - y_raw.min())
print("X = ", X)
print("y = ", y)
# a, b = 0, 0
# num_epoch = 10000
# learning_rate = 5e-4
# for e in range(num_epoch):
# y_pred = a * X + b
# grad_a, grad_b = 2*(y_pred-y).dot(X), 2*(y_pred-y).sum() # 手动偏导
# a, b = a-learning_rate*grad_a, b-learning_rate*grad_b
# if e % 500 == 0:
# print("e = ", e, "a = ", a, "b = ", b)
# tensorflow-线性回归
X = tf.constant(X)
Y = tf.constant(y)
print(X)
print(y)
a = tf.Variable(initial_value=0.)
b = tf.Variable(initial_value=0.)
variables = [a, b]
num_epoch = 10000
optimizer = tf.keras.optimizers.SGD(learning_rate=5e-4)
for e in range(num_epoch):
with tf.GradientTape() as tape:
y_pred = a * X + b
loss = tf.reduce_sum(tf.square(y_pred - y))
grads = tape.gradient(loss, variables)
optimizer.apply_gradients(grads_and_vars=zip(grads, variables))
if e % 500 == 0:
print("----"*25)
print("e=", e, "\na=", a, "\nb=", b)