Turning the number of inputs when I call makeModel from 3 to 1 allows the program to run without errors but no training actually happens and the accuracy doesn’t change.
JavaScript
x
39
39
1
import pandas as pd
2
from numpy import loadtxt
3
from sklearn.impute import SimpleImputer
4
from sklearn.model_selection import train_test_split
5
from tensorflow.keras.models import Sequential
6
from tensorflow.keras.layers import Dense
7
from sklearn.preprocessing import LabelEncoder
8
from sklearn.utils import shuffle
9
from sklearn.tree import DecisionTreeRegressor as dtr
10
from sklearn.metrics import mean_absolute_error as mae
11
import numpy as np
12
13
def makeModel(num_inputs, num_classes, train_X, train_y):
14
model = Sequential()
15
model.add(Dense(8, input_dim=num_inputs, activation='relu'))
16
model.add(Dense(10, activation='relu'))
17
model.add(Dense(10, activation='relu'))
18
model.add(Dense(10, activation='relu'))
19
model.add(Dense(3, activation='softmax'))
20
21
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
22
model.fit(train_X, train_y, epochs=10, batch_size=10)
23
24
return model
25
26
label_encoder = LabelEncoder()
27
iris_data = pd.read_csv("iris.csv")
28
iris_data = shuffle(iris_data)
29
30
iris_data['species'] = label_encoder.fit_transform(iris_data['species'])
31
32
feature_columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
33
34
X = iris_data[feature_columns]
35
y = iris_data['species']
36
37
train_x, val_x, train_y, val_y = train_test_split(X, y, test_size=0.2)
38
iris_model = makeModel(4, 3, train_x, train_y)
39
Advertisement
Answer
LabelEncoder
transforms the input to an array of encoded values. i.e if your input is ["paris", "paris", "tokyo", "amsterdam"]
then they can be encoded as [0, 0, 1, 2]
. It is not one-hot encoding scheme which is expected by categorical_crossentropy
loss. If you have a integer encoding you will have to use sparse_categorical_crossentropy
Fix
change your code loss to sparse_categorical_crossentropy
:
JavaScript
1
2
1
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
2
Sample
JavaScript
1
21
21
1
def makeModel(num_inputs, num_classes, train_X, train_y):
2
model = Sequential()
3
model.add(Dense(8, input_dim=num_inputs, activation='relu'))
4
model.add(Dense(10, activation='relu'))
5
model.add(Dense(10, activation='relu'))
6
model.add(Dense(10, activation='relu'))
7
model.add(Dense(3, activation='softmax'))
8
9
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
10
model.fit(train_X, train_y, epochs=10, batch_size=10)
11
12
return model
13
14
label_encoder = LabelEncoder()
15
iris = datasets.load_iris()
16
y = iris.target
17
y = label_encoder.fit_transform(y)
18
19
train_x, val_x, train_y, val_y = train_test_split(iris.data, y, test_size=0.2)
20
iris_model = makeModel(4, 3, train_x, train_y)
21