Algoteka
Log in to submit your own samples!

Train a Neural Network on Data

Problem by oml1111
# Tech tags Title Creator Created date
1 0
TensorFlow
Keras
2022-10-01 23:28
View all samples for this language (1 verified and 0 unverified)

Short Code | Python | TensorFlow Keras |

By oml1111 |
0 likes

Trains a model with the RMSprop optimizer, categorical crossentropy loss function and with batching and validation.

Prerequisites

You first need to have constructed and compiled the neural network. Example:
Construct a Feedforward Neural Network: Python, TensorFlow Keras - algoteka.com

Code

# ...
# Construct your model

# The optimizer and loss function are defined in the compile method of the model
some_model.compile(
    optimizer=keras.optimizers.RMSprop(learning_rate=1e-3),
    loss=keras.losses.CategoricalCrossentropy(),
    metrics=["acc"]
)

def train_model(model, x_train, y_train):
    # model - An object of type keras.Model corresponding to our neural network
    # x_train - A numpy array of our training data inputs
    # y_train - A numpy array of our training data labels
    model.fit(x_train, y_train, batch_size=64, epochs=2, validation_split=0.2)

Further reading

Once the neural network has been built, the following sample shows how you can make predictions with it:
Predict with a Neural Network - algoteka.com

You can read up on the theory from the following links:
Neural Networks: Lecture 5: Back-propagation slides - courses.cs.ut.ee
Neural Networks: Lecture 5: Back-propagation video - courses.cs.ut.ee
Neural Networks: Lecture 6: Optimization & regularization slides - courses.cs.ut.ee
Neural Networks: Lecture 6: Optimization & regularization video - courses.cs.ut.ee

References

functions
tensorflow.keras.Model.compile tensorflow.org
tensorflow.keras.Model.fit tensorflow.org

Problem Description

Train some neural network on some given data. Samples should strive for simplicity.

View sample discussion (0 comments)
View problem discussion (0 comments)