0

I am new to NN and Keras. I have tried this tutorial from DeepLizard and followed it until the lady showed the first training results ~30min in. I followed the steps exactly except the lines with 'shuffle' since that was not how it is supposed to be used (to my knowledge). Here is my code:

import numpy as np
import random as random
from random import randint
from sklearn.preprocessing import MinMaxScaler

from random import shuffle

train_labels = []
train_samples = []

for i in range(50):
  random_younger = randint(13,64)
  train_samples.append(random_younger)
  train_labels.append(1)

  random_older = randint(65,100)
  train_samples.append(random_older)
  train_labels.append(0)

for i in range (1000):
  random_younger = randint(13,64)
  train_samples.append(random_younger)
  train_labels.append(0)

  random_older = randint(65,100)
  train_samples.append(random_older)
  train_labels.append(1)

train_samples = np.array(train_samples)
train_labels = np.array(train_labels)
shuffle(train_samples)
shuffle(train_labels)

scaler = MinMaxScaler(feature_range = (0,1))
scaled_train_samples = scaler.fit_transform(train_samples. reshape(-1,1))

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy

model = Sequential([Dense(units=16, input_shape=(1,), activation='relu'),
                Dense(units=32, activation='relu'),
                Dense(units=2, activation='softmax')])

model.summary()

    model.compile(optimizer=Adam(learning_rate=0.0001),loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(x=scaled_train_samples, y=train_labels, batch_size=5, epochs=30, shuffle=True, verbose=2)

My NN doesn't learn staying at 50% accuracy all the time. I tried to vary the #of layers and epochs, I have checked the data types and structure. Can someone help with the first in my life minimal example? Thanks!

MsTais
  • 189
  • 2
  • 9
  • 1
    Try representing the ages in binary, arrays of zeros and ones, instead than using integers – alec_djinn Mar 31 '21 at 18:04
  • I actually think shuffle is not needed there at all. I think since "model.fit" shuffles by default, it was a bug in her code. The code, as she wrote it, doesn't work, but if I delete the lines with "shuffle" everything works as expected. – MsTais Mar 31 '21 at 18:09
  • 3
    You're shuffling the `x` and `y` independently – Nicolas Gervais Mar 31 '21 at 18:28
  • @NicolasGervais is right. This part needs to be modified: shuffle(train_samples) shuffle(train_labels) This thread could help for your case:https://stackoverflow.com/questions/23289547/shuffle-two-list-at-once-with-same-order – Dr. H. Lecter Mar 31 '21 at 18:36

1 Answers1

1

As mentioned before, I was shuffling x and y independently. In the tutorial I have reference "shuffle" is used the way it is not supposed to work, see the documentation. My solution:

c = list(zip(train_samples, train_labels))

shuffle(c)

train_samples, train_labels = zip(*c)
train_samples = np.array(train_samples)
train_labels=np.array(train_labels)
MsTais
  • 189
  • 2
  • 9