본문 바로가기
HRDI_AI/[AI특화] AI 기반 실시간 객체탐지 모델 구현

Day2

by Toddler_AD 2025. 12. 16.
  • Iris 인공신경망 모델
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

iris = load_iris()
# print(iris)

model = MLPClassifier(hidden_layer_sizes=(10, 10), max_iter=1000)
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)
model.fit(X_train, y_train)
print("Model accuracy:", model.score(X_test, y_test))
print("Predictions:", model.predict(X_test))
print("Target: ", y_test)
Model accuracy: 0.9666666666666667
Predictions: [1 0 2 1 1 0 1 2 2 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0]
Target:  [1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0]

 

 

from sklearn.metrics import confusion_matrix
print("Confusion Matrix:\n", confusion_matrix(y_test, model.predict(X_test)))
Confusion Matrix:
 [[10  0  0]
 [ 0  8  1]
 [ 0  0 11]]

 

 

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Input

model = Sequential()
model.add(Input(shape=(4,))) # input layer
model.add(Dense(50, activation='sigmoid')) # hidden layer
model.add(Dense(30, activation='sigmoid')) # hidden layer
model.add(Dense(3, activation='softmax')) # output layer
model.summary()
Model: "sequential_1"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Layer (type)                    ┃ Output Shape           ┃       Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ dense_3 (Dense)                 │ (None, 50)             │           250 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense_4 (Dense)                 │ (None, 30)             │         1,530 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense_5 (Dense)                 │ (None, 3)              │            93 │
└─────────────────────────────────┴────────────────────────┴───────────────┘
 Total params: 1,873 (7.32 KB)
 Trainable params: 1,873 (7.32 KB)
 Non-trainable params: 0 (0.00 B)

 

 

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=100, batch_size=50)
Epoch 1/100
3/3 ━━━━━━━━━━━━━━━━━━━━ 3s 664ms/step - accuracy: 0.3250 - loss: 1.9942
Epoch 2/100
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.3250 - loss: 1.8617 
Epoch 3/100
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.3250 - loss: 1.7304 
Epoch 4/100
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.3250 - loss: 1.6214 
Epoch 5/100
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.3250 - loss: 1.5171 
Epoch 6/100
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.3250 - loss: 1.4269 
Epoch 7/100
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - accuracy: 0.3250 - loss: 1.3466 
Epoch 8/100
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.3250 - loss: 1.2909 
Epoch 9/100
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - accuracy: 0.3250 - loss: 1.2333 
Epoch 10/100
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.3250 - loss: 1.1930 
Epoch 11/100
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.3250 - loss: 1.1578 
Epoch 12/100
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.3250 - loss: 1.1313 
Epoch 13/100
...
Epoch 99/100
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 10ms/step - accuracy: 0.9417 - loss: 0.5331
Epoch 100/100
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 10ms/step - accuracy: 0.9417 - loss: 0.5285

 

 

print(model.evaluate(X_test, y_test))
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 640ms/step - accuracy: 0.9333 - loss: 0.5185
[0.5185215473175049, 0.9333333373069763]

 

 

model.save('./iris_model/iris_model.keras') # 모델의 구조와 가중치를 함께 저장
from tensorflow.keras.models import load_model
loaded_model = load_model('./iris_model/iris_model.keras')
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)

print(loaded_model.evaluate(X_test, y_test))
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 447ms/step - accuracy: 0.9333 - loss: 0.5185
[0.5185215473175049, 0.9333333373069763]

 

 

 

  • Callback을 이용한 딥러닝 모델 관리 및 모니터링
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Input

model = Sequential()
model.add(Input(shape=(4,))) # input layer
model.add(Dense(50, activation='sigmoid')) # hidden layer
model.add(Dense(30, activation='sigmoid')) # hidden layer
model.add(Dense(3, activation='softmax')) # output layer
# model.summary()

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)

from tensorflow.keras.callbacks import ModelCheckpoint
path = './callback_model/model-{epoch:02d}-{val_accuracy:.4f}.h5'
checkpoint = ModelCheckpoint(path, save_best_only=True, monitor='val_accuracy', verbose=1)

from tensorflow.keras.callbacks import EarlyStopping
early_stopping = EarlyStopping(monitor='val_accuracy', patience=20)

from tensorflow.keras.callbacks import TensorBoard
tensorboard = TensorBoard(log_dir='logs', histogram_freq=1, embeddings_freq=1)

model.fit(X_train, y_train, validation_split=0.2, epochs=100, batch_size=50, callbacks=[checkpoint])
Epoch 1/100
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 867ms/step - accuracy: 0.2706 - loss: 1.2384
Epoch 1: val_accuracy improved from None to 0.50000, saving model to ./callback_model/model-01-0.5000.h5
WARNING:absl:You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`. 
2/2 ━━━━━━━━━━━━━━━━━━━━ 3s 1s/step - accuracy: 0.2812 - loss: 1.2292 - val_accuracy: 0.5000 - val_loss: 1.1159
Epoch 2/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 20ms/step - accuracy: 0.3200 - loss: 1.1666
Epoch 2: val_accuracy did not improve from 0.50000
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 53ms/step - accuracy: 0.2812 - loss: 1.1914 - val_accuracy: 0.5000 - val_loss: 1.1016
Epoch 3/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 20ms/step - accuracy: 0.2600 - loss: 1.2310
Epoch 3: val_accuracy did not improve from 0.50000
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 58ms/step - accuracy: 0.2812 - loss: 1.1651 - val_accuracy: 0.5000 - val_loss: 1.0929
Epoch 4/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 19ms/step - accuracy: 0.3400 - loss: 1.1650
Epoch 4: val_accuracy improved from 0.50000 to 0.70833, saving model to ./callback_model/model-04-0.7083.h5
WARNING:absl:You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`. 
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 76ms/step - accuracy: 0.2917 - loss: 1.1369 - val_accuracy: 0.7083 - val_loss: 1.0891
Epoch 5/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 23ms/step - accuracy: 0.5800 - loss: 1.1508
Epoch 5: val_accuracy did not improve from 0.70833
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 57ms/step - accuracy: 0.6458 - loss: 1.1169 - val_accuracy: 0.2500 - val_loss: 1.0899
Epoch 6/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 23ms/step - accuracy: 0.5200 - loss: 1.0667
Epoch 6: val_accuracy did not improve from 0.70833
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 53ms/step - accuracy: 0.3958 - loss: 1.0982 - val_accuracy: 0.2083 - val_loss: 1.0957
Epoch 7/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 19ms/step - accuracy: 0.3800 - loss: 1.0782
Epoch 7: val_accuracy did not improve from 0.70833
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 50ms/step - accuracy: 0.3646 - loss: 1.0862 - val_accuracy: 0.2083 - val_loss: 1.1020
Epoch 8/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 20ms/step - accuracy: 0.3800 - loss: 1.0744
Epoch 8: val_accuracy did not improve from 0.70833
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 61ms/step - accuracy: 0.3646 - loss: 1.0776 - val_accuracy: 0.2083 - val_loss: 1.1094
Epoch 9/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 19ms/step - accuracy: 0.3600 - loss: 1.0750
Epoch 9: val_accuracy did not improve from 0.70833
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 53ms/step - accuracy: 0.3646 - loss: 1.0722 - val_accuracy: 0.2083 - val_loss: 1.1169
Epoch 10/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 20ms/step - accuracy: 0.4600 - loss: 1.0477
Epoch 10: val_accuracy did not improve from 0.70833
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 58ms/step - accuracy: 0.3646 - loss: 1.0694 - val_accuracy: 0.2917 - val_loss: 1.1254
...
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 57ms/step - accuracy: 0.8021 - loss: 0.5521 - val_accuracy: 0.7083 - val_loss: 0.6369
Epoch 88/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 18ms/step - accuracy: 0.8000 - loss: 0.5740
Epoch 88: val_accuracy improved from 0.70833 to 0.75000, saving model to ./callback_model/model-88-0.7500.h5
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
WARNING:absl:You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`. 
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 68ms/step - accuracy: 0.8125 - loss: 0.5460 - val_accuracy: 0.7500 - val_loss: 0.6305
Epoch 89/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 20ms/step - accuracy: 0.8600 - loss: 0.5364
Epoch 89: val_accuracy did not improve from 0.75000
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 63ms/step - accuracy: 0.8125 - loss: 0.5405 - val_accuracy: 0.7500 - val_loss: 0.6259
Epoch 90/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 18ms/step - accuracy: 0.8200 - loss: 0.5719
Epoch 90: val_accuracy did not improve from 0.75000
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 54ms/step - accuracy: 0.8125 - loss: 0.5349 - val_accuracy: 0.7500 - val_loss: 0.6205
Epoch 91/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 18ms/step - accuracy: 0.8200 - loss: 0.5727
Epoch 91: val_accuracy did not improve from 0.75000
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 61ms/step - accuracy: 0.8125 - loss: 0.5294 - val_accuracy: 0.7500 - val_loss: 0.6150
Epoch 92/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 18ms/step - accuracy: 0.8400 - loss: 0.5370
Epoch 92: val_accuracy improved from 0.75000 to 0.79167, saving model to ./callback_model/model-92-0.7917.h5
WARNING:absl:You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`. 
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 67ms/step - accuracy: 0.8333 - loss: 0.5242 - val_accuracy: 0.7917 - val_loss: 0.6098
Epoch 93/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 20ms/step - accuracy: 0.8600 - loss: 0.5229
Epoch 93: val_accuracy did not improve from 0.79167
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 60ms/step - accuracy: 0.8438 - loss: 0.5191 - val_accuracy: 0.7917 - val_loss: 0.6051
Epoch 94/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 18ms/step - accuracy: 0.8400 - loss: 0.5212
Epoch 94: val_accuracy did not improve from 0.79167
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 51ms/step - accuracy: 0.8542 - loss: 0.5142 - val_accuracy: 0.7917 - val_loss: 0.6004
Epoch 95/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 18ms/step - accuracy: 0.8000 - loss: 0.4950
Epoch 95: val_accuracy did not improve from 0.79167
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 51ms/step - accuracy: 0.8542 - loss: 0.5094 - val_accuracy: 0.7917 - val_loss: 0.5957
Epoch 96/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 19ms/step - accuracy: 0.8000 - loss: 0.5782
Epoch 96: val_accuracy did not improve from 0.79167
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 61ms/step - accuracy: 0.8542 - loss: 0.5048 - val_accuracy: 0.7917 - val_loss: 0.5915
Epoch 97/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 19ms/step - accuracy: 0.8600 - loss: 0.4794
Epoch 97: val_accuracy did not improve from 0.79167
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 50ms/step - accuracy: 0.8542 - loss: 0.5003 - val_accuracy: 0.7917 - val_loss: 0.5882
Epoch 98/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 19ms/step - accuracy: 0.8000 - loss: 0.5233
Epoch 98: val_accuracy did not improve from 0.79167
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 62ms/step - accuracy: 0.8542 - loss: 0.4958 - val_accuracy: 0.7917 - val_loss: 0.5842
...
Epoch 100/100
1/2 ━━━━━━━━━━━━━━━━━━━━ 0s 19ms/step - accuracy: 0.8400 - loss: 0.4893
Epoch 100: val_accuracy did not improve from 0.79167
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 58ms/step - accuracy: 0.8542 - loss: 0.4873 - val_accuracy: 0.7917 - val_loss: 0.5768

 

 

 

from tensorflow.keras.datasets import mnist
(X_train, y_train),(X_test, y_test) = mnist.load_data()
X_train, X_test = X_train/255.0, X_test/255.0

from tensorflow.keras.callbacks import ModelCheckpoint
path = './callback_model/mnist_model-{epoch:02d}-{val_accuracy:.4f}.keras'
checkpoint = ModelCheckpoint(filepath=path, monitor='val_accuracy', 
                             save_best_only=True, verbose=1)

from tensorflow.keras.callbacks import EarlyStopping
early_stopping=EarlyStopping(monitor='val_accuracy',
                             patience=3)

from tensorflow.keras.callbacks import TensorBoard
tensor_board = TensorBoard(log_dir='tensor_log', 
                           embeddings_freq=1, histogram_freq=1)

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Flatten, Dense

model = Sequential()
model.add(Flatten(input_shape=(28, 28)))
model.add(Dense(360, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))

model.compile(loss='sparse_categorical_crossentropy',
              optimizer='adam', metrics=['accuracy'])

model.fit(X_train, y_train, validation_data=(X_test, y_test),
          batch_size=1000, epochs=1000,
          callbacks=[checkpoint, early_stopping, tensor_board])
/home/user1/yolov3/.venv/lib/python3.12/site-packages/keras/src/layers/reshaping/flatten.py:37: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
  super().__init__(**kwargs)
Epoch 1/1000
59/60 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.7124 - loss: 1.0554
Epoch 1: val_accuracy improved from None to 0.93680, saving model to ./callback_model/mnist_model-01-0.9368.keras
60/60 ━━━━━━━━━━━━━━━━━━━━ 2s 17ms/step - accuracy: 0.8471 - loss: 0.5706 - val_accuracy: 0.9368 - val_loss: 0.2254
Epoch 2/1000
55/60 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.9387 - loss: 0.2151
Epoch 2: val_accuracy improved from 0.93680 to 0.95470, saving model to ./callback_model/mnist_model-02-0.9547.keras
60/60 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.9459 - loss: 0.1903 - val_accuracy: 0.9547 - val_loss: 0.1546
Epoch 3/1000
50/60 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.9567 - loss: 0.1461
Epoch 3: val_accuracy improved from 0.95470 to 0.96390, saving model to ./callback_model/mnist_model-03-0.9639.keras
60/60 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.9614 - loss: 0.1339 - val_accuracy: 0.9639 - val_loss: 0.1243
Epoch 4/1000
60/60 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.9697 - loss: 0.1053
Epoch 4: val_accuracy improved from 0.96390 to 0.96810, saving model to ./callback_model/mnist_model-04-0.9681.keras
60/60 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.9710 - loss: 0.1015 - val_accuracy: 0.9681 - val_loss: 0.1056
Epoch 5/1000
51/60 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.9762 - loss: 0.0821
Epoch 5: val_accuracy improved from 0.96810 to 0.97250, saving model to ./callback_model/mnist_model-05-0.9725.keras
60/60 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.9765 - loss: 0.0814 - val_accuracy: 0.9725 - val_loss: 0.0885
Epoch 6/1000
52/60 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.9820 - loss: 0.0642
Epoch 6: val_accuracy improved from 0.97250 to 0.97500, saving model to ./callback_model/mnist_model-06-0.9750.keras
60/60 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.9815 - loss: 0.0644 - val_accuracy: 0.9750 - val_loss: 0.0830
Epoch 7/1000
...
Epoch 16/1000
52/60 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.9988 - loss: 0.0088
Epoch 16: val_accuracy did not improve from 0.97930
60/60 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.9983 - loss: 0.0096 - val_accuracy: 0.9773 - val_loss: 0.0780
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

 

 

 

tensorboard --logdir=tensor_log
http://localhost:6006/

 

 

 

  • 와인 데이터 분류 모델
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Input, Dense, Dropout
 
model = Sequential([
    Input(shape=(11,)),
    Dense(100, activation='sigmoid'),
    Dropout(0.2),
    Dense(200, activation='relu'),
    Dropout(0.4),
    Dense(50, activation='tanh'),
    Dropout(0.1),
    Dense(10, activation='softmax'),
]) 
model.summary()
Model: "sequential_4"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Layer (type)                    ┃ Output Shape           ┃       Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ dense_12 (Dense)                │ (None, 100)            │         1,200 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dropout (Dropout)               │ (None, 100)            │             0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense_13 (Dense)                │ (None, 200)            │        20,200 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dropout_1 (Dropout)             │ (None, 200)            │             0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense_14 (Dense)                │ (None, 50)             │        10,050 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dropout_2 (Dropout)             │ (None, 50)             │             0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense_15 (Dense)                │ (None, 10)             │           510 │
└─────────────────────────────────┴────────────────────────┴───────────────┘
 Total params: 31,960 (124.84 KB)
 Trainable params: 31,960 (124.84 KB)
 Non-trainable params: 0 (0.00 B)

 

 

 

import pandas as pd
 
redwine = pd.read_csv('./data/winequality-red.csv', sep=';')
X, y = redwine.iloc[:, :-1], redwine.iloc[:, -1]
from sklearn.model_selection import train_test_split
 
X_train, X_test, y_train, y_test = train_test_split(X, y)
model.compile(loss='sparse_categorical_crossentropy',
              optimizer='adam', metrics=['accuracy'])
history = model.fit(X_train, y_train, batch_size=200, epochs=300)
Epoch 1/300
2025-12-16 16:36:15.723128: I external/local_xla/xla/service/gpu/autotuning/dot_search_space.cc:208] All configs were filtered out because none of them sufficiently match the hints. Maybe the hints set does not contain a good representative set of valid configs? Working around this by using the full hints set instead.
2025-12-16 16:36:15.723188: I external/local_xla/xla/service/gpu/autotuning/dot_search_space.cc:208] All configs were filtered out because none of them sufficiently match the hints. Maybe the hints set does not contain a good representative set of valid configs? Working around this by using the full hints set instead.
2025-12-16 16:36:15.723217: I external/local_xla/xla/service/gpu/autotuning/dot_search_space.cc:208] All configs were filtered out because none of them sufficiently match the hints. Maybe the hints set does not contain a good representative set of valid configs? Working around this by using the full hints set instead.
2025-12-16 16:36:15.723264: I external/local_xla/xla/service/gpu/autotuning/dot_search_space.cc:208] All configs were filtered out because none of them sufficiently match the hints. Maybe the hints set does not contain a good representative set of valid configs? Working around this by using the full hints set instead.
2025-12-16 16:36:15.926455: I external/local_xla/xla/stream_executor/cuda/subprocess_compilation.cc:346] ptxas warning : Registers are spilled to local memory in function 'gemm_fusion_dot_814', 12 bytes spill stores, 12 bytes spill loads

2025-12-16 16:36:16.596034: I external/local_xla/xla/stream_executor/cuda/subprocess_compilation.cc:346] ptxas warning : Registers are spilled to local memory in function 'gemm_fusion_dot_1381', 400 bytes spill stores, 400 bytes spill loads

2025-12-16 16:36:17.090177: I external/local_xla/xla/stream_executor/cuda/subprocess_compilation.cc:346] ptxas warning : Registers are spilled to local memory in function 'gemm_fusion_dot_1381', 24 bytes spill stores, 24 bytes spill loads

2025-12-16 16:36:17.181461: I external/local_xla/xla/stream_executor/cuda/subprocess_compilation.cc:346] ptxas warning : Registers are spilled to local memory in function 'gemm_fusion_dot_1452', 36 bytes spill stores, 36 bytes spill loads

1/6 ━━━━━━━━━━━━━━━━━━━━ 23s 5s/step - accuracy: 0.0400 - loss: 2.6043
2025-12-16 16:36:19.730201: I external/local_xla/xla/service/gpu/autotuning/dot_search_space.cc:208] All configs were filtered out because none of them sufficiently match the hints. Maybe the hints set does not contain a good representative set of valid configs? Working around this by using the full hints set instead.
2025-12-16 16:36:19.730259: I external/local_xla/xla/service/gpu/autotuning/dot_search_space.cc:208] All configs were filtered out because none of them sufficiently match the hints. Maybe the hints set does not contain a good representative set of valid configs? Working around this by using the full hints set instead.
2025-12-16 16:36:19.730295: I external/local_xla/xla/service/gpu/autotuning/dot_search_space.cc:208] All configs were filtered out because none of them sufficiently match the hints. Maybe the hints set does not contain a good representative set of valid configs? Working around this by using the full hints set instead.
2025-12-16 16:36:19.730357: I external/local_xla/xla/service/gpu/autotuning/dot_search_space.cc:208] All configs were filtered out because none of them sufficiently match the hints. Maybe the hints set does not contain a good representative set of valid configs? Working around this by using the full hints set instead.
2025-12-16 16:36:19.927049: I external/local_xla/xla/stream_executor/cuda/subprocess_compilation.cc:346] ptxas warning : Registers are spilled to local memory in function 'gemm_fusion_dot_814', 12 bytes spill stores, 12 bytes spill loads

2025-12-16 16:36:20.471828: I external/local_xla/xla/stream_executor/cuda/subprocess_compilation.cc:346] ptxas warning : Registers are spilled to local memory in function 'gemm_fusion_dot_1381', 400 bytes spill stores, 400 bytes spill loads

2025-12-16 16:36:21.115551: I external/local_xla/xla/stream_executor/cuda/subprocess_compilation.cc:346] ptxas warning : Registers are spilled to local memory in function 'gemm_fusion_dot_1381', 24 bytes spill stores, 24 bytes spill loads

2025-12-16 16:36:21.216844: I external/local_xla/xla/stream_executor/cuda/subprocess_compilation.cc:346] ptxas warning : Registers are spilled to local memory in function 'gemm_fusion_dot_1452', 48 bytes spill stores, 48 bytes spill loads

6/6 ━━━━━━━━━━━━━━━━━━━━ 9s 787ms/step - accuracy: 0.2636 - loss: 2.0332
Epoch 2/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.4237 - loss: 1.4081 
Epoch 3/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.4420 - loss: 1.2781 
Epoch 4/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.4479 - loss: 1.2312 
Epoch 5/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.4479 - loss: 1.2173 
Epoch 6/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.4646 - loss: 1.1911 
Epoch 7/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.4812 - loss: 1.1920 
Epoch 8/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.4754 - loss: 1.1848 
Epoch 9/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.4796 - loss: 1.1828 
Epoch 10/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.4946 - loss: 1.1635 
Epoch 11/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.4662 - loss: 1.1825 
Epoch 12/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.4779 - loss: 1.1847 
Epoch 13/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.5096 - loss: 1.1597 
...
Epoch 299/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.6297 - loss: 0.8881 
Epoch 300/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.6138 - loss: 0.8885

 

 

 

model.evaluate(X_test, y_test)
2025-12-16 16:37:54.253839: I external/local_xla/xla/service/gpu/autotuning/dot_search_space.cc:208] All configs were filtered out because none of them sufficiently match the hints. Maybe the hints set does not contain a good representative set of valid configs? Working around this by using the full hints set instead.
2025-12-16 16:37:54.253904: I external/local_xla/xla/service/gpu/autotuning/dot_search_space.cc:208] All configs were filtered out because none of them sufficiently match the hints. Maybe the hints set does not contain a good representative set of valid configs? Working around this by using the full hints set instead.
2025-12-16 16:37:55.010357: I external/local_xla/xla/stream_executor/cuda/subprocess_compilation.cc:346] ptxas warning : Registers are spilled to local memory in function 'gemm_fusion_dot_40', 16 bytes spill stores, 16 bytes spill loads

13/13 ━━━━━━━━━━━━━━━━━━━━ 3s 72ms/step - accuracy: 0.5725 - loss: 1.0107

 

 

 

import matplotlib.pyplot as plt

fig, ax1 = plt.subplots(figsize=(6, 4))
ax1.plot(history.history['loss'], 'b-', label='loss')
ax1.set_ylabel('loss')
ax1.legend(loc='upper left')

ax2 = ax1.twinx()
ax2.plot(history.history['accuracy'], 'r--', label='accuracy')
ax2.set_ylabel('accuracy')
ax2.legend(loc='upper right')
plt.show()