TensorFlow (Google) framework học máy mã nguồn mở, dùng Python, tối ưu NumPy/tensor. Hỗ trợ mạng nơ-ron sâu, scalable, GPU, nhiều thư viện.
Cài đặt: pip install tensorflow
Tensor: mảng đa chiều trong đồ thị luồng dữ liệu.
Ví dụ
def linear_reg(x):
return A * x + b
def mean_square_error(y_pred, y_true):
return tf.reduce_mean(tf.square(y_pred - y_true))
optimizer = tf.optimizers.SGD(learning_rate)
def run_optimization():
with tf.GradientTape() as g:
pred = linear_reg(X)
loss = mean_square_error(pred, Y)
gradients = g.gradient(loss, [A, b])
optimizer.apply_gradients(zip(gradients, [A, b]))
Nguồn mã: TensorFlow Examples
Đầu ra
Hàm hồi quy tuyến tính được gọi trên dữ liệu. Sau khi tính điểm tối ưu, hàm MSE được tính. Gradient descent tìm trọng số tối ưu. Giá trị hiển thị trên console.
Giải thích
- Khởi tạo ngẫu nhiên
weight (A)vàbias (b), cập nhật tối ưu sau train. - Phương trình tuyến tính:
Ax + b(A=weight, b=bias). - Định nghĩa hàm Mean Squared Error (MSE).
- Optimizer: Stochastic Gradient Descent (SGD).
- Hàm tối ưu: tính gradient, cập nhật weight/bias.
- Huấn luyện số bước xác định.