# AI use cases for Tensai (mattn/tensai)

Repo: [mattn/tensai](https://github.com/mattn/tensai) | Use cases: 4 | Page: https://dedupedai.com/use-cases/mattn-tensai/

tensai is a small machine-learning framework written in pure Go that covers training, inference, and several local model runtimes.

This file holds every use case of this repo. Each one starts with a "## " heading and ends with its URL.

- Train a small neural network in pure Go: https://dedupedai.com/use-cases/mattn-tensai/train-small-network-in-go/
- Serve local text embeddings for search or RAG: https://dedupedai.com/use-cases/mattn-tensai/serve-local-embeddings-for-search/
- Generate images locally with Tensai image: https://dedupedai.com/use-cases/mattn-tensai/generate-images-offline-in-go/
- Export a trained Sequential model to ONNX: https://dedupedai.com/use-cases/mattn-tensai/export-trained-model-to-onnx/

## How to train a small neural network in pure Go with Tensai

Repo: [mattn/tensai](https://github.com/mattn/tensai) | Difficulty: Intermediate | Time: 30 minutes | Basis: From the README

Run the bundled XOR example to see forward passes and backprop work with no dependencies.

**Who it is for:** A Go developer who wants to learn neural networks and has no Python setup or GPU available.

**The problem:** Most tutorials assume Python and heavy frameworks, which makes it hard to see how training works in plain code.

**The outcome:** A working XOR classifier trained by Tensai, plus a readable Go program to modify for other small problems.

### Steps

1. Install Go 1.26 or newer. (inferred, check the README)
2. Run `git clone https://github.com/mattn/tensai` (inferred, check the README)
3. Run `cd tensai/_example/xor` (inferred, check the README)
4. Run `go run .` (inferred, check the README)
5. Check the printed results, if any, against the XOR targets of 0, 1, 1, 0.

### Prompt to paste into your agent

```text
Clone the mattn/tensai repository and run the program in _example/xor with go run. Report the trained predictions for the four XOR inputs. Verify that the outputs are close to 0, 1, 1, 0.
```

### Caveat

The README lists the XOR example but the exact run command and printed output are not shown, so the steps above are inferred from the repository layout.

Disclaimer: Third-party repos we don't own or vouch for. AI-generated use cases can be wrong: review everything and run at your own risk. Full text: https://dedupedai.com/disclaimer/

URL: https://dedupedai.com/use-cases/mattn-tensai/train-small-network-in-go/

## How to serve local text embeddings for search or RAG with Tensai

Repo: [mattn/tensai](https://github.com/mattn/tensai) | Difficulty: Intermediate | Time: 60 minutes | Basis: From the README

Turn texts into vectors with Tensai embed and answer OpenAI-style /v1/embeddings requests.

**Who it is for:** A developer building semantic search or retrieval for a private document set who does not want to send text to a hosted API.

**The problem:** Retrieval pipelines need embedding vectors, and calling a cloud service exposes the documents.

**The outcome:** A local server that accepts OpenAI-compatible embedding requests, backed by a ModernBERT encoder.

### Steps

1. Clone the repository and build or install the Tensai command from its root. (inferred, check the README)
2. Run `tensai embed` on a few sample texts to confirm vectors are produced.
3. Run `tensai serve` to start the OpenAI-compatible endpoint.
4. Send a POST request to the server's /v1/embeddings path with a sample input and check the returned vector length. (inferred, check the README)

### Prompt to paste into your agent

```text
Use the mattn/tensai repository to run tensai embed on three sample sentences, then start tensai serve. Report the vector dimension returned by the /v1/embeddings endpoint. Verify that all three vectors have the same length.
```

### Caveat

The README does not say where the default model is downloaded from or how large it is, so plan for the first-run setup time.

Disclaimer: Third-party repos we don't own or vouch for. AI-generated use cases can be wrong: review everything and run at your own risk. Full text: https://dedupedai.com/disclaimer/

URL: https://dedupedai.com/use-cases/mattn-tensai/serve-local-embeddings-for-search/

## How to generate images locally with Tensai image with Tensai

Repo: [mattn/tensai](https://github.com/mattn/tensai) | Difficulty: Advanced | Time: 120 minutes | Basis: From the README

Render a 256x256 picture from a text prompt using Qwen-Image-2.1 in pure Go.

**Who it is for:** A creator or developer who wants text-to-image output on a laptop without a cloud image service.

**The problem:** Image generation models usually need a Python stack and a dedicated GPU.

**The outcome:** A generated image file cached beside the checkpoint, produced by a single command.

### Steps

1. Clone the repository and build or install the Tensai command from its root. (inferred, check the README)
2. Make sure enough disk space is free for the checkpoint, which the README sizes at 7GB at eight bits or 3.5GB at four bits.
3. Run `tensai image "a calico cat asleep on a stack of books"`
4. Open the cached result and confirm it shows the requested subject.

### Prompt to paste into your agent

```text
In the mattn/tensai repository, run tensai image with the prompt "a calico cat asleep on a stack of books". Report the output file path and the elapsed time. Verify that the image file exists and that the run took under four minutes on a laptop-class machine, as the README claims.
```

### Caveat

The README timing of under four minutes for a 256x256 picture is a claim about a laptop, so actual speed will depend on the machine and the checkpoint download.

Disclaimer: Third-party repos we don't own or vouch for. AI-generated use cases can be wrong: review everything and run at your own risk. Full text: https://dedupedai.com/disclaimer/

URL: https://dedupedai.com/use-cases/mattn-tensai/generate-images-offline-in-go/

## How to export a trained Sequential model to ONNX with Tensai

Repo: [mattn/tensai](https://github.com/mattn/tensai) | Difficulty: Advanced | Time: 90 minutes | Basis: From the README

Train a Sequential model in Tensai, then marshal it to ONNX with the encoding/onnx package.

**Who it is for:** A developer who trains a small model in Go and needs to run it in onnxruntime or another ONNX consumer.

**The problem:** Trained Go models are stuck in Go unless they can be exported to a standard format.

**The outcome:** An ONNX file (opset 13, FP32) whose outputs match Tensai Predict to about 1e-7 relative error.

### Steps

1. Build a small Sequential model from the layer package and train it with Compile and Fit. (inferred, check the README)
2. Call the marshal function in the encoding/onnx package on the trained model and write the bytes to a .onnx file. (inferred, check the README)
3. Load the file in onnxruntime and run the same input through it. (inferred, check the README)
4. Compare the onnxruntime output with Predict on the same input and confirm the relative error is near 1e-7.

### Prompt to paste into your agent

```text
In the mattn/tensai repository, train a small Sequential model, export it with encoding/onnx, and run the file in onnxruntime. Report the maximum relative error between Predict and onnxruntime outputs. Verify that the error is about 1e-7 or below.
```

### Caveat

The README says onnxruntime reproduces Predict to about 1e-7 relative error but does not name the exact marshal function, so the function call above is inferred.

Disclaimer: Third-party repos we don't own or vouch for. AI-generated use cases can be wrong: review everything and run at your own risk. Full text: https://dedupedai.com/disclaimer/

URL: https://dedupedai.com/use-cases/mattn-tensai/export-trained-model-to-onnx/
