基于autokeras的简单天气预报例子和运行输出
import numpy as np
from autokeras import StructuredDataClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# 模拟数据
np.random.seed(0)
n_samples = 1000
X = np.random.rand(n_samples, 3) # 假设有三个特征:温度、湿度、气压
y = (X[:, 0] * 0.5 + X[:, 1] * 0.3 + np.random.randn(n_samples) * 0.1 > 0.5).astype(int) # 简单的逻辑来模拟是否下雨
# 数据预处理
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
# 初始化并训练模型
clf = StructuredDataClassifier(overwrite=True, max_trials=1) # 设置max_trials限制搜索次数
clf.fit(X_train, y_train, epochs=10) # epochs控制训练时间
# 评估模型
print('Test accuracy:', clf.evaluate(X_test, y_test))
test@ubuntu:~/python$ python3 test-ak-weather.py
Using TensorFlow backend
2024-09-24 20:45:27.725676: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2024-09-24 20:45:27.803135: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2024-09-24 20:45:27.803943: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-09-24 20:45:28.765172: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRTSearch: Running Trial #1
Value |Best Value So Far |Hyperparameter
True |True |structured_data_block_1/normalize
2 |2 |structured_data_block_1/dense_block_1/num_layers
False |False |structured_data_block_1/dense_block_1/use_batchnorm
0 |0 |structured_data_block_1/dense_block_1/dropout
32 |32 |structured_data_block_1/dense_block_1/units_0
32 |32 |structured_data_block_1/dense_block_1/units_1
0 |0 |classification_head_1/dropout
adam |adam |optimizer
0.001 |0.001 |learning_rateEpoch 1/10
20/20 [==============================] - 1s 17ms/step - loss: 0.6362 - accuracy: 0.6875 - val_loss: 0.5740 - val_accuracy: 0.7937
Epoch 2/10
20/20 [==============================] - 0s 7ms/step - loss: 0.5203 - accuracy: 0.8281 - val_loss: 0.4791 - val_accuracy: 0.8375
Epoch 3/10
20/20 [==============================] - 0s 7ms/step - loss: 0.4325 - accuracy: 0.8609 - val_loss: 0.4038 - val_accuracy: 0.8562
Epoch 4/10
20/20 [==============================] - 0s 7ms/step - loss: 0.3708 - accuracy: 0.8625 - val_loss: 0.3559 - val_accuracy: 0.8750
Epoch 5/10
20/20 [==============================] - 0s 7ms/step - loss: 0.3361 - accuracy: 0.8562 - val_loss: 0.3321 - val_accuracy: 0.8875
Epoch 6/10
20/20 [==============================] - 0s 6ms/step - loss: 0.3198 - accuracy: 0.8562 - val_loss: 0.3216 - val_accuracy: 0.8875
Epoch 7/10
20/20 [==============================] - 0s 6ms/step - loss: 0.3121 - accuracy: 0.8578 - val_loss: 0.3167 - val_accuracy: 0.8875
Epoch 8/10
20/20 [==============================] - 0s 6ms/step - loss: 0.3077 - accuracy: 0.8578 - val_loss: 0.3140 - val_accuracy: 0.8813
Epoch 9/10
20/20 [==============================] - 0s 6ms/step - loss: 0.3046 - accuracy: 0.8594 - val_loss: 0.3125 - val_accuracy: 0.8813
Epoch 10/10
20/20 [==============================] - 0s 6ms/step - loss: 0.3023 - accuracy: 0.8594 - val_loss: 0.3117 - val_accuracy: 0.8813Trial 1 Complete [00h 00m 04s]
val_accuracy: 0.887499988079071Best val_accuracy So Far: 0.887499988079071
Total elapsed time: 00h 00m 04s
Epoch 1/10
25/25 [==============================] - 1s 3ms/step - loss: 0.5459 - accuracy: 0.7638
Epoch 2/10
25/25 [==============================] - 0s 3ms/step - loss: 0.4548 - accuracy: 0.8138
Epoch 3/10
25/25 [==============================] - 0s 4ms/step - loss: 0.3885 - accuracy: 0.8525
Epoch 4/10
25/25 [==============================] - 0s 3ms/step - loss: 0.3459 - accuracy: 0.8587
Epoch 5/10
25/25 [==============================] - 0s 3ms/step - loss: 0.3237 - accuracy: 0.8600
Epoch 6/10
25/25 [==============================] - 0s 3ms/step - loss: 0.3135 - accuracy: 0.8587
Epoch 7/10
25/25 [==============================] - 0s 3ms/step - loss: 0.3081 - accuracy: 0.8600
Epoch 8/10
25/25 [==============================] - 0s 4ms/step - loss: 0.3048 - accuracy: 0.8625
Epoch 9/10
25/25 [==============================] - 0s 3ms/step - loss: 0.3024 - accuracy: 0.8612
Epoch 10/10
25/25 [==============================] - 0s 4ms/step - loss: 0.3005 - accuracy: 0.8637
7/7 [==============================] - 0s 2ms/step - loss: 0.2812 - accuracy: 0.8900
Test accuracy: [0.28117990493774414, 0.8899999856948853]
/home/test/.local/lib/python3.8/site-packages/keras/src/engine/training.py:3000: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
Model saved successfully.