Łukasz Cieśluk
Łukasz Cieśluk

Reputation: 3

Keras neural network takes only few samples to train

data = np.random.random((10000, 150)) 
labels = np.random.randint(10, size=(10000, 1))
labels = to_categorical(labels, num_classes=10) 
model = Sequential()
model.add(Dense(units=32, activation='relu', input_shape=(150,)))
model.add(Dense(units=10, activation='softmax'))
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

model.fit(data, labels, epochs=30, validation_split=0.2)

I created 10000 random samples to train my net, but it use only few of them(250/10000) Exaple of the 1st epoch:

Epoch 1/30

250/250 [==============================] - 0s 2ms/step - loss: 2.1110 - accuracy: 0.2389 - val_loss: 2.2142 - val_accuracy: 0.1800

Upvotes: 0

Views: 728

Answers (2)

Lentian Latifi
Lentian Latifi

Reputation: 132

Try code like following example

model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# Generate dummy data
import numpy as np
data = np.random.random((1000, 100))
labels = np.random.randint(10, size=(1000, 1))

# Convert labels to categorical one-hot encoding
one_hot_labels = keras.utils.to_categorical(labels, num_classes=10)

# Train the model, iterating on the data in batches of 32 samples
model.fit(data, one_hot_labels, epochs=10, batch_size=32)

Upvotes: 0

AlexM4
AlexM4

Reputation: 523

Your data is split into training and validation subsets (validation_split=0.2). Training subset has size 8000 and validation 2000. Training goes in batches, each batch has size 32 samples by default. So one epoch should take 8000/32=250 batches, as it shows in the progress.

Upvotes: 2

Related Questions