2019-05-05 17:34:26 +02:00
|
|
|
import numpy as np
|
|
|
|
import tensorflow as tf
|
|
|
|
|
|
|
|
from sklearn.datasets import load_iris
|
|
|
|
from sklearn.model_selection import train_test_split
|
|
|
|
|
|
|
|
from tensorflow.keras import layers
|
|
|
|
from tensorflow.keras.utils import to_categorical
|
|
|
|
|
|
|
|
# Getting data
|
|
|
|
data_set = load_iris()
|
|
|
|
x = data_set['data']
|
|
|
|
y = to_categorical(data_set['target'])
|
|
|
|
|
|
|
|
train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=0.2)
|
|
|
|
|
|
|
|
# Building the model
|
|
|
|
model = tf.keras.Sequential()
|
|
|
|
|
2019-05-06 14:45:47 +02:00
|
|
|
model.add(layers.Dense(20, activation='relu', input_dim=4))
|
2019-05-05 17:34:26 +02:00
|
|
|
model.add(layers.Dense(3, activation='sigmoid'))
|
|
|
|
|
|
|
|
model.compile(optimizer='adam', loss='categorical_crossentropy',
|
|
|
|
metrics=['accuracy'])
|
|
|
|
# Training the model
|
2019-05-06 14:45:47 +02:00
|
|
|
model.fit(train_x, train_y, validation_data=(test_x, test_y), epochs=1000)
|
2019-05-05 17:34:26 +02:00
|
|
|
|
|
|
|
model.save('iris_model.h5')
|