Gyakorlat 9.

B.Sc course, University of Debrecen, Department of Data Science and Visualization, 2024

Adathalmaz

from sklearn.datasets import load_iris
iris = load_iris()
print(iris.keys())
data = iris.data
labels = iris.target
data.shape, labels.shape
labels
from sklearn.utils import shuffle

data, labels = shuffle(data, labels, random_state=42)
labels

Normalizáció

normalization

def normalize(x):
    return (x - np.min(x)) / (np.max(x) - np.min(x))
n_data = normalize(data.values)
n_data.shape

One-hot

one hot

def one_hot_encode(x: np.ndarray, num_labels: int) -> np.ndarray:
    return np.eye(num_labels)[x]

Tanuló és teszt adatok

train_test_split_no = int(n_data.shape[0] * 0.8)
train_test_split_no
X_train = n_data[:train_test_split_no]
y_train = labels[:train_test_split_no].astype(int)
y_train = one_hot_encode(y_train, 3)

X_train.shape, y_train.shape
X_test = n_data[train_test_split_no:]
y_test = labels[train_test_split_no:].astype(int)
y_test = one_hot_encode(y_test, 3)

X_test.shape, y_test.shape

Hogyan tudjuk elképzelni?

Neuronok

neural network 3

Agyunk mint hálózat

neural network 4

Egy rétegű neurális hálók

neural network 1

Több rétegű neurális hálózat

neural network 2

Neurális hálózat koncepció

neural network 5

Aktivácuós függvények

soft max

relu

Egyszerű neurális hálózat pythonban

set(labels)
import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.Input((X_train.shape[1])),
    tf.keras.layers.Dense(128, activation="relu"),
    tf.keras.layers.Dense(3, activation="softmax")
    ]
)
model.summary()
for layer in model.get_weights():
    print(layer.shape)
tf.keras.utils.plot_model(
    model,
    show_shapes=True,
    show_dtype=True,
    show_layer_names=True,
    expand_nested=True,
    dpi=96,
    layer_range=None,
    show_layer_activations=True)
model.compile(
    optimizer="adam", 
    loss="categorical_crossentropy", 
    metrics=["accuracy"]
)
x = tf.ones((3, X_train.shape[1]))
model(x)

Tanulás

model.fit(X_train, y_train, epochs=50, batch_size=32)

Kiértékelés

model.evaluate(X_test, y_test)
choice = np.random.choice(np.arange(X_test.shape[0]+1))
p = model.predict(np.array([X_test[choice]]))
choice, np.argmax(p), np.argmax(y_test[choice])