2

I'm beginner in Python and ML. I was practising this Iris Data set to create a ML model using tensor flow 2.0.

I parsed the csv and trained the model using the dataset. I'm able to get 90 % training accuracy and 91 % validation accuracy during my model creation.

import tensorflow as tf
import numpy as np
from sklearn import preprocessing

csv_data = np.loadtxt('iris_training.csv',delimiter=',')
target_all = csv_data[:,-1]

csv_data = csv_data[:,0:-1]

# Shuffling the input
shuffled_indices = np.arange(csv_data.shape[0])
np.random.shuffle(shuffled_indices)

shuffled_inputs = csv_data[shuffled_indices]
shuffled_targets = target_all[shuffled_indices]

# Standardize the Inputs
shuffled_inputs = preprocessing.scale(shuffled_inputs)

# Split date into train , validation and test
total_count = shuffled_inputs.shape[0]
train_data_count = int(0.8*total_count)
validation_data_count = int(0.1*total_count)
test_data_count = total_count - train_data_count - validation_data_count

train_inputs = shuffled_inputs[:train_data_count]
train_targets = shuffled_targets[:train_data_count]

validation_inputs = shuffled_inputs[train_data_count:train_data_count+validation_data_count]
validation_targets = shuffled_targets[train_data_count:train_data_count+validation_data_count]

test_inputs = shuffled_inputs[train_data_count+validation_data_count:]
test_targets = shuffled_targets[train_data_count+validation_data_count:]


print(len(train_inputs))
print(len(validation_inputs))
print(len(test_inputs))

# Model Creation
input_size = 4
hidden_layer_size = 100
output_size = 3

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(hidden_layer_size, input_dim=input_size,     activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(hidden_layer_size, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(output_size, activation=tf.nn.softmax))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',metrics=['accuracy'])

model.fit(train_inputs,train_targets, epochs=10, validation_data=(validation_inputs, validation_targets), verbose=2)

prediction = model.predict(test_inputs)

Point me if there is something in my code that i could do to improve the accuracy of my model for this simple Iris Dataset.

File Used for training my Model : Iris Csv

Jaffer Sheriff
  • 1,444
  • 13
  • 33
  • that isn't an issue of pycharm. its simply an index out of range for `target` and/or `outs` – luigigi Jan 09 '20 at 12:33
  • your usage of `evaluate` is wrong. you are probably looking for [predict](https://keras.io/models/model/#predict) – luigigi Jan 09 '20 at 12:36
  • @luigigi If I `test_loss, test_accuracy = model.predict(test_inputs)` Im getting `line 78, in test_loss, test_accuracy = model.predict(test_inputs) ValueError: too many values to unpack Issue` Could you help me in this – Jaffer Sheriff Jan 09 '20 at 12:39
  • yes, because `predict` only have one return value. use sth like `predictions = model.predict(test_inputs) ` – luigigi Jan 09 '20 at 12:40
  • @luigigi yes i was looking for `predict` and it has only one return value. Thanks for ur help man. Now i'm getting distributed probabilities from my model. Is there any way to improve accuracy of my model ? – Jaffer Sheriff Jan 09 '20 at 12:46
  • No, sorry. just here for the coding error – luigigi Jan 09 '20 at 12:47
  • @luigigi Thank you man. I really appreciate your help. – Jaffer Sheriff Jan 09 '20 at 12:55

1 Answers1

1

As for your model, you can try to do hyperparameters tuning,

  • Setting the learning rate to a lower value
  • Increase the epoch
  • Add more training dataset since you have a small set of the dataset.

The neural network shines when there is a good amount of data for the training.

You can also add more layers to the model, add dropouts to avoid overfitting as well as using different activation functions.

These are the common factors that affect model performance.

TF_Support
  • 1,794
  • 1
  • 7
  • 16