Compared to my another post Logistic Regression for non linearly separable data which uses one-layer net, i.e. Logistic Regression to classify the iris data set, this post is to discuss the tensorflow method.
iris = load_iris()
x_train, x_test, y_train, y_test = train_test_split(
iris.data, iris.target, test_size=0.33, random_state=2021)
y_train=np_utils.to_categorical(y_train, num_classes=3)
y_test=np_utils.to_categorical(y_test, num_classes=3)
model = tf.keras.Sequential([
tf.keras.layers.Dense(500, input_dim=4, activation='relu'),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(3, activation='softmax')
])
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(x_train,y_train, validation_data=(x_test,y_test), batch_size=20,epochs=30,verbose=0)
prediction=model.predict(x_train)
length=len(prediction)
y_label=np.argmax(y_train,axis=1)
predict_label=np.argmax(prediction,axis=1)
accuracy=np.sum(y_label==predict_label)/length * 100
print("Accuracy of the dataset",accuracy )
prediction=model.predict(x_test)
length=len(prediction)
y_label=np.argmax(y_test,axis=1)
predict_label=np.argmax(prediction,axis=1)
accuracy=np.sum(y_label==predict_label)/length * 100
print("Accuracy of the dataset",accuracy )
The accuracy is
Accuracy of the dataset 99.0
Accuracy of the dataset 94.0
I've tried several times, this is the best performance.
I also tried more and less neurons and layers, the one above is the best.