

Để lập trình một con trí tuệ nhân tạo (AI) bằng Python, bạn có thể sử dụng các thư viện như TensorFlow, Keras hoặc PyTorch để xây dựng và huấn luyện mô hình AI. Dưới đây là một ví dụ đơn giản về việc tạo một mô hình AI sử dụng thư viện TensorFlow:
```python
Import tensorflow as tf
Xây dựng mô hình AI
Model = tf. Keras. Sequential ([
Tf. Keras. Layers. Dense (64, activation='relu', input_shape= (input_dim)) ,
Tf. Keras. Layers. Dense (64, activation='relu'),
Tf. Keras. Layers. Dense (output_dim, activation='softmax')
] )
Biên dịch mô hình
Model.compile (optimizer='adam',
Loss='categorical_crossentropy',
Metrics=['accuracy'] )
Huấn luyện mô hình
Model. Fit (x_train, y_train, epochs=10, batch_size=32)
Đánh giá mô hình
Loss, accuracy = model. Evaluate (x_test, y_test)
Print ('Loss: ', loss)
Print ('Accuracy: ', accuracy)
Sử dụng mô hình để dự đoán
Predictions = model. Predict (x_test)
```
Lưu ý rằng đoạn mã trên chỉ là một ví dụ cơ bản và có thể cần điều chỉnh phù hợp với bài toán cụ thể của bạn. Bạn cũng cần chuẩn bị dữ liệu huấn luyện (x_train, y_train) và dữ liệu kiểm tra (x_test, y_test) phù hợp.
```python
Import tensorflow as tf
Xây dựng mô hình AI
Model = tf. Keras. Sequential ([
Tf. Keras. Layers. Dense (64, activation='relu', input_shape= (input_dim)) ,
Tf. Keras. Layers. Dense (64, activation='relu'),
Tf. Keras. Layers. Dense (output_dim, activation='softmax')
] )
Biên dịch mô hình
Model.compile (optimizer='adam',
Loss='categorical_crossentropy',
Metrics=['accuracy'] )
Huấn luyện mô hình
Model. Fit (x_train, y_train, epochs=10, batch_size=32)
Đánh giá mô hình
Loss, accuracy = model. Evaluate (x_test, y_test)
Print ('Loss: ', loss)
Print ('Accuracy: ', accuracy)
Sử dụng mô hình để dự đoán
Predictions = model. Predict (x_test)
```
Lưu ý rằng đoạn mã trên chỉ là một ví dụ cơ bản và có thể cần điều chỉnh phù hợp với bài toán cụ thể của bạn. Bạn cũng cần chuẩn bị dữ liệu huấn luyện (x_train, y_train) và dữ liệu kiểm tra (x_test, y_test) phù hợp.