Tutorial

How to Use JSON for Fine-Tuning Machine Learning Models

Published on March 25, 2025

Technical Writer

How to Use JSON for Fine-Tuning Machine Learning Models

Introduction

Fine-tuning a model is crucial when we want to train a model using a small and task-specific dataset. Fine-tuning is a process where we take up a pre-trained model and then train the model to fit the task-specific data. This process helps the model to optimize its performance for the custom dataset. Unlike model training from scratch, finetuning allows one to utilize the knowledge a model has already learned from a large, general dataset and refine it to work better on a targeted custom dataset.

One effective way to manage fine-tuning parameters is to use JSON (JavaScript Object Notation) files. JSON provides a structured, lightweight, and easily readable format for storing model configurations and hyperparameters, helping maintain structured and reusable configurations across various experiments.

Prerequisites

Before diving into fine-tuning ML models using JSON, ensure you have:

  • Basic Understanding of Machine Learning – Familiarity with training, fine-tuning, and evaluating models.
  • Knowledge of JSON – Understanding JSON structure, including key-value pairs, nesting, and formatting.
  • Experience with Python and ML Frameworks – Proficiency in Python, along with libraries like TensorFlow, PyTorch, or Scikit-learn.
  • Data Preprocessing Skills – Ability to clean, format, and convert JSON datasets for ML training.
  • GPU/Cloud Setup (Optional) – For large-scale fine-tuning, access to cloud GPUs (e.g., DigitalOcean GPU Droplets) can speed up the process.

Why Use JSON for Machine Learning Model Fine-Tuning?

Let us first start by understanding a few key benefits of JSON

Storing Hyperparameters for Fine-tuning – Hyperparameters control the training behavior of a model. Instead of hardcoding them in scripts, JSON files allow easy modification and reproducibility.

Example: JSON for Hyperparameter Tuning

{
  "learning_rate": 0.001,
  "batch_size": 32,
  "num_epochs": 10,
  "optimizer": "adam",
  "dropout_rate": 0.2,
  "early_stopping": true
}

Loading JSON into Python

import json

# Load JSON file
with open("hyperparameters.json", "r") as file:
    config = json.load(file)

# Use hyperparameters
learning_rate = config["learning_rate"]
batch_size = config["batch_size"]

During experimentation, different ML configurations are tested, and in this case, storing hyperparameters in JSON can be useful without altering the code. Libraries like json in Python make reading and writing JSON seamless.

Automating Hyperparameter Tuning with JSON—JSON is often used for Grid search and Bayesian optimization to define different hyperparameter sets.

Example: JSON for Hyperparameter Search

{
  "experiments": [
    {"learning_rate": 0.001, "batch_size": 32, "optimizer": "adam"},
    {"learning_rate": 0.0005, "batch_size": 64, "optimizer": "sgd"},
    {"learning_rate": 0.0001, "batch_size": 128, "optimizer": "rmsprop"}
  ]
}

Using JSON for Grid Search in Python

import json

with open("hyperparameter_search.json", "r") as file:
    experiments = json.load(file)["experiments"]

for exp in experiments:
    print(f"Running experiment with learning_rate={exp['learning_rate']} and batch_size={exp['batch_size']}")

This allows us to efficiently automate finding the best parameters by running multiple experiments. Furthermore, JSON also allows the storage of complex hierarchical hyperparameter settings.

Storing Training Data for Fine-tuning - Fine-tuning a machine learning model often requires labeled data. Storing these labeled data in JSON files allows for efficient reading, writing, and sharing. Furthermore, JSON files can be easily structured into pandas DataFrames.

Example: JSONL for GPT Fine-Tuning

{"messages": [{"role": "system", "content": "You are a helpful assistant."},
              {"role": "user", "content": "How do you store ML parameters in JSON?"},
              {"role": "assistant", "content": "You can store hyperparameters like learning rate and batch size in a JSON file."}]}

Each training example follows the format required by the API.

Build and configure ML Pipelines using JSON - JSON can also be used to set up end-to-end ML Pipelines, specifying the dataset, the dataset’s location, logging configurations, and much more.

Example: JSON for Pipeline Configuration

{
  "dataset": {
    "train": "data/train.csv",
    "test": "data/test.csv"
  },
  "model": {
    "architecture": "transformer",
    "pretrained_weights": "bert-base-uncased"
  },
  "logging": {
    "log_dir": "logs/",
    "save_checkpoints": true
  }
}

Using JSON for a PyTorch Training Pipeline

import json

# Load pipeline configuration
with open("config.json", "r") as file:
    config = json.load(file)

# Access parameters
train_data_path = config["dataset"]["train"]
model_arch = config["model"]["architecture"]

These code blocks can be converted into Python functions to reduce redundancy and maintain clean code that can be reused multiple times.

JSONFormer Library for Structured JSON Outputs

JSONFormer is a Python library designed to constrain and structure text generation models (like LLMs) to output valid JSON. It ensures that the model adheres to a specified schema, making it ideal for structured data generation tasks such as:

Why Use JSONFormer?

Ensuring structured output can be challenging when fine-tuning models or working with generative AI. JSONFormer helps in:

  • Validating model output: Ensures compliance with a predefined schema.
  • Reducing post-processing effort: Eliminates the need for extra validation steps.
  • Increasing reliability in applications: Useful for AI agents that interact with APIs, databases, or structured workflows.

JSONFormer modifies LLM behavior by embedding constraints during token generation. It does not change the model’s parameters but instead structures the output dynamically.

pip install jsonformer
from jsonformer.format import highlight_values

from jsonformer.main import Jsonformer


from transformers import AutoModelForCausalLM, AutoTokenizer

# Define a schema
schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer"},
        "city": {"type": "string"}
    }
}

# Load model and tokenizer
model_name = "mistralai/Mistral-7B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Generate structured JSON
jsonformer = Jsonformer(model, tokenizer, schema)
output = jsonformer()

print(output)

JSONFormer is an approach used to simplify structured fine-tuning outputs, making JSON-based ML configurations more efficient and thus reducing post-processing needs.

jsonify in Flask

The jsonify function in Flask is a convenient way to return JSON responses from an API. It automatically serializes Python dictionaries into JSON format and sets the correct response headers.
Below is the syntax of jsonify() function: jsonify(*args, **kwargs)

from flask import Flask, jsonify
 
app = Flask(__name__)
 
# Sample JSON response
@app.route('/get-config', methods=['GET'])
def get_config():
    config = {
        "model": "transformer",
        "hyperparameters": {
            "learning_rate": 0.001,
            "batch_size": 32,
            "epochs": 20
        }
    }
    return jsonify(config)  # Converts dict to JSON and returns a response

if __name__ == "__main__":
    app.run(debug=True)

The JSONify library converts Python data structures such as dictionaries, lists, or tuples into JSON, ensuring proper text-based data encoding.

jsonify(config) ensures the output is correctly formatted as JSON.

Structuring JSON for Model Fine-Tuning

A well-structured JSON file helps in organizing hyperparameters for fine-tuning.

Below is an example of a JSON file for training a deep learning model:

{
  "model": "transformer",
  "hyperparameters": {
    "learning_rate": 0.001,
    "batch_size": 32,
    "epochs": 20,
    "optimizer": "adam",
    "dropout": 0.3
  },
  "dataset": {
    "train_path": "data/train.jsonl",
    "validation_path": "data/val.jsonl"
  },
  "fine_tune": {
    "base_model": "bert-base-uncased",
    "dataset_size": 100000,
    "num_labels": 3
  }
}

This JSON file stores various hyperparameters such as learning rate, batch size, optimizer choice, and dataset paths, which are essential for fine-tuning a transformer-based model.

How to Load JSON-Based ML Parameters in Python

Using Python, you can load and parse JSON files to configure model training dynamically:

import json
# Load JSON configuration file
with open("config.json", "r") as file:
    config = json.load(file)

# Access hyperparameters
learning_rate = config["hyperparameters"]["learning_rate"]
batch_size = config["hyperparameters"]["batch_size"]

print(f"Learning Rate: {learning_rate}, Batch Size: {batch_size}")

This approach allows for the modification of parameters without the need to change the code.

Fine-Tuning Machine Learning Models with JSON

Using JSON in TensorFlow

import tensorflow as tf
from tensorflow.keras.optimizers import Adam

# Load JSON config
with open("config.json", "r") as file:
    config = json.load(file)

# Define model
model = tf.keras.Sequential([
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(config["hyperparameters"]["dropout"]),
    tf.keras.layers.Dense(10, activation='softmax')
])

# Compile model
model.compile(optimizer=Adam(learning_rate=config["hyperparameters"]["learning_rate"]),
              loss='categorical_crossentropy',
              metrics=['accuracy'])

Using JSON in PyTorch

import torch
import torch.nn as nn
import torch.optim as optim

# Load JSON config
with open("config.json", "r") as file:
    config = json.load(file)

# Define model
class SimpleModel(nn.Module):
    def __init__(self):
        super(SimpleModel, self).__init__()
        self.fc1 = nn.Linear(128, 64)
        self.dropout = nn.Dropout(config["hyperparameters"]["dropout"])
        self.fc2 = nn.Linear(64, 10)

    def forward(self, x):
        x = self.fc1(x)
        x = self.dropout(x)
        x = self.fc2(x)
        return x

# Initialize model
model = SimpleModel()
optimizer = optim.Adam(model.parameters(), lr=config["hyperparameters"]["learning_rate"])

JSON vs. YAML vs. Python Dicts for ML Configurations

While configuring the settings for finetuning a model or even training the model from scratch, it is crucial to choose the appropriate format. Three popular options are JSON, YAML, and Python dictionaries. JSON is a widely used standard, but YAML offers better readability for large configurations.

Feature JSON YAML Python Dicts
Readability Human-readable but lacks comments Mode readable Readable but requires Python execution
Parsing Complexity Requires a parser (json module in Python) Requires a parser(yaml module) No parsing is needed can be directly used in Python scripts
Supports Nesting Yes Yes Yes
Data Types Strings, numbers, lists, objects Strings, numbers, lists, dictionaries Supports all Python data types
Best Use Case Storing ML configurations across different frameworks Readable configs for manual tuning Directly used in Python scripts
Data Type Support Limited (string, number, boolean, array, object) Supports various types like lists and dictionaries Full support for all Python data types

JSON example

{
  "model": "transformer",
  "learning_rate": 0.001,
  "batch_size": 32
}

YAML example

model: transformer
learning_rate: 0.001
batch_size: 32

Python Dictionaries example

config = {
    "model": "transformer",
    "learning_rate": 0.001,
    "batch_size": 32
}

Each format has its advantages: JSON is widely used in ML pipelines, YAML is easier to read, and Python dictionaries integrate seamlessly into scripts.

Frequently Asked Questions (FAQs)

What is the difference between fine-tuning and parameter-efficient fine-tuning?

Fine-tuning adjusts all model parameters, whereas parameter-efficient fine-tuning (PEFT) updates only a subset of parameters to reduce computation costs.

What are the benefits of using JSON for ML fine-tuning?

JSON enables easy storage, readability, and integration with multiple frameworks, making ML training processes more manageable.

Can JSON store hyperparameters for deep learning models?

Yes, JSON can store all hyperparameters, making it ideal for configuring deep learning models.

How do I load JSON-based ML parameters in Python?

You can use Python’s json module to read and parse JSON files dynamically.

Can I use JSON with TensorFlow and PyTorch?

Yes, both frameworks support JSON-based configurations for fine-tuning models.

What is the difference between fine-tuning and hyperparameter tuning?

Fine-tuning updates model weights, while hyperparameter tuning optimizes external training settings.

Conclusion

Fine-tuning a machine learning model is crucial when the dataset is small and the computational cost must be avoided. Using JSON to manage hyperparameters or model output offers a well-structured approach.

As machine learning continues to evolve, efficient configuration management will play a crucial role in model optimization. With cloud solutions like DigitalOcean’s GPU-powered infrastructure, you can effortlessly scale fine-tuning workloads, reducing training times and enhancing model accuracy. Integrating JSON into your workflow allows you to build more adaptive, high-performing machine-learning models with minimal complexity.

References

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Shaoni Mukherjee
Shaoni MukherjeeTechnical Writer
See author profile
Category:
Tutorial
Tags:

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment
Leave a comment...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.