Keras neural network takes only few samples to train

不问归期 提交于 2020-06-23 16:20:17

问题


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


回答1:


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.




回答2:


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)


来源:https://stackoverflow.com/questions/61618163/keras-neural-network-takes-only-few-samples-to-train

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!