Tensorflow/Keras are very good to start implementing your first neural network. A couple of fucntions calls are enough. For now we need:
- A function/class to read the database.
- The network definition (a.k.a model).
- Call the function compile (which defines the loss) and fit (which defines the batch) to train the model.
- Call the function evaluate to test the learning.
- Save the trained weights.
- Load the trained model and call the function predict to use the network.
Most of these steps can be customized.
1. Reading the database
The database we made here, generates a text file with the pattern:
- p1.x p1.y p2.x p2.y p3.x p3.y area
where "p1, p2, p3" are the three points of a triangle to feed the model, and "area" the triangle area for the loss function (therefore, this is a regression model with supervised learning).
The reading can be done in a single line, when using NumPy:
- all_data = np.loadtxt(src_file, max_rows=None, skiprows=3, usecols=range(0,7), delimiter=" ", dtype=np.float32)
where
all the lines are read, the first three are skipped (because they are
comments), creates a list of size 7 for each line, using the blank space
as splitting character, and the list content is converted to 32 bit
float.
Later, we separate the first 6 columns to be the set X (input), and the last column to be the set Y (expected output).
2. Building the model
The triangle points makes the input layer, so there are 6 nodes. The output layer is a single value, which is the triangle area.
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(1, input_shape=(6,)),
tf.keras.layers.Dense(1)
])
The network architecture is yet to be discovered, and the model above does not have hidden layers nor actvaton functions.
3. Training
The
training, in this simple case, consists in first compiling the internal
graph to configure the model with the loss, optimizer and metrics.
- model.compile(loss=loss_fn, optimizer=optimizer, metrics=relative_error)
The arguments of the function above are not defined yet, wait a little longer, please.
Then, we perform the actual training:
- history = model.fit(X_train, Y_train, epochs=epochs, batch_size=batch, validation_data=(X_eval, Y_eval))
4. Evaluation, save and prediction
Here we get the score and metric value (defined during compilation) in the test set:
- score, metric = model.evaluate(X_eval, Y_eval, batch_size=batch)
If both are satisfying, we can save the model:
- model.save(save_model_filename)
Then load it to predict areas of triangles not seen before:
- model = keras.models.load_model(save_model_filename, compile=False)
- areas = model.predict(X_test)
The full code of this simple (and yet not useful) network is here.
Now we can search for the architecture.
No comments:
Post a Comment