Hồi quy tuyến tính

Nhập môn TensorFlow bằng Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Hồi quy tuyến tính là gì?

Biểu đồ phân tán log tự nhiên của diện tích nhà (ft²) so với log tự nhiên của giá nhà (USD).

Nhập môn TensorFlow bằng Python

Hồi quy tuyến tính là gì?

Đường hồi quy khớp với biểu đồ phân tán log tự nhiên diện tích nhà (ft²) so với log tự nhiên giá nhà (USD).

Nhập môn TensorFlow bằng Python

Mô hình hồi quy tuyến tính

  • Mô hình hồi quy tuyến tính giả định quan hệ tuyến tính:
    • $price = intercept + size*slope + error$
  • Đây là hồi quy đơn biến.
    • Chỉ có một đặc trưng, size.
  • Hồi quy đa biến có nhiều đặc trưng.
    • Ví dụ: sizelocation
Nhập môn TensorFlow bằng Python

Hồi quy tuyến tính trong TensorFlow

# Define the targets and features
price = np.array(housing['price'], np.float32)
size = np.array(housing['sqft_living'], np.float32)

# Define the intercept and slope
intercept = tf.Variable(0.1, np.float32)
slope = tf.Variable(0.1, np.float32)
# Define a linear regression model
def linear_regression(intercept, slope, features = size):
    return intercept + features*slope
# Compute the predicted values and loss
def loss_function(intercept, slope, targets = price, features = size):
    predictions = linear_regression(intercept, slope)
    return tf.keras.losses.mse(targets, predictions)
Nhập môn TensorFlow bằng Python

Hồi quy tuyến tính trong TensorFlow

# Define an optimization operation
opt = tf.keras.optimizers.Adam()
# Minimize the loss function and print the loss
for j in range(1000):
    opt.minimize(lambda: loss_function(intercept, slope),\
    var_list=[intercept, slope])
    print(loss_function(intercept, slope))
tf.Tensor(10.909373, shape=(), dtype=float32)
...
tf.Tensor(0.15479447, shape=(), dtype=float32)
# Print the trained parameters
print(intercept.numpy(), slope.numpy())
Nhập môn TensorFlow bằng Python

Ayo berlatih!

Nhập môn TensorFlow bằng Python

Preparing Video For Download...