<jupyter_start><jupyter_text>Implementing FizzBuzz with numpy/kerasThe model of FizzBuzz is generated using numpy/keras on Python.```pythonimport numpy as npfrom tensorflow.contrib.keras.api.keras.models import Sequential, model_from_jsonfrom tensorflow.contrib.keras.api.keras.layers import Dense, Dropout, Activationfrom tensorflow.contrib.keras.api.keras.optimizers import SGD, Adamimport tensorflow.contrib.lite as litedef fizzbuzz(i):    if   i % 15 == 0: return np.array([0, 0, 0, 1])    elif i % 5  == 0: return np.array([0, 0, 1, 0])    elif i % 3  == 0: return np.array([0, 1, 0, 0])    else:             return np.array([1, 0, 0, 0])def bin(i, num_digits):    return np.array([i >> d & 1 for d in range(num_digits)])NUM_DIGITS = 7trX = np.array([bin(i, NUM_DIGITS) for i in range(1, 101)])trY = np.array([fizzbuzz(i) for i in range(1, 101)])model = Sequential()model.add(Dense(64, input_dim = 7))model.add(Activation('tanh'))model.add(Dense(4, input_dim = 64))model.add(Activation('softmax'))model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])model.fit(trX, trY, epochs = 3600, batch_size = 64)model.save('fizzbuzz_model.h5')converter = lite.TFLiteConverter.from_keras_model_file('fizzbuzz_model.h5')tflite_model = converter.convert()open('fizzbuzz_model.tflite', 'wb').write(tflite_model)``` At the first, import packages.<jupyter_code>import (
    "fmt"
    "log"
)<jupyter_output><empty_output><jupyter_text>Add utility functions to encode/decode numbers since the number must be 1 hot.<jupyter_code>func bin(n int, num_digits int) []float32 {
    f := make([]float32, num_digits)
    for i := 0; i < num_digits; i++ {
        f[i] = float32((n >> uint(i)) & 1)
    }
    return f[:]
}

func dec(b []float32) int {
    for i := 0; i < len(b); i++ {
        if b[i] > 0.4 {
            return i
        }
    }
    panic("Sorry, I'm wrong")
}<jupyter_output><empty_output><jupyter_text>Next, import go-tflite. If you use [gophernotes](https://github.com/gopherdata/gophernotes) kernel on Jupyter Notebook, you must generate go-tflite as third_party. See the https://github.com/cosmos72/gomacroimporting-packages<jupyter_code>import "github.com/mattn/go-tflite"<jupyter_output><empty_output><jupyter_text>Load tflite model file that is generated by Python.<jupyter_code>model := tflite.NewModelFromFile("fizzbuzz_model.tflite")
if model == nil {
    log.Fatal("cannot load model")
}<jupyter_output><empty_output><jupyter_text>Now you can create interpreter of TensorFlow Lite. The second argument is InterpreterOptions. If you want to specify thread number of the interpreter, modify options that created by NewInterpreterOptions. ```gooptions := tflite.NewInterpreterOptions()options.SetNumThread(4)defer options.Delete()interpreter := tflite.NewInterpreter(model, options)```<jupyter_code>interpreter := tflite.NewInterpreter(model, nil)<jupyter_output><empty_output><jupyter_text>Allocate tensors according to the model.<jupyter_code>if status := interpreter.AllocateTensors(); status != tflite.OK {
    log.Fatal("cannot allocate tensors")
}<jupyter_output><empty_output><jupyter_text>Now interpreter have input/output tensors. You can update input tensor.<jupyter_code>input := interpreter.GetInputTensor(0)<jupyter_output><empty_output><jupyter_text>So the `input` is a slice of values that can make sure the type with `Type()`.<jupyter_code>input.Type()<jupyter_output><empty_output><jupyter_text>You can update this slice directly. For example, update with 5 (Buzz) using `bin` defined above.<jupyter_code>bin(5, 7)
copy(input.Float32s(), bin(5, 7))<jupyter_output><empty_output><jupyter_text>Then, call `Invoke()`<jupyter_code>if status := interpreter.Invoke(); status != tflite.OK {
    log.Fatal("cannot invoke")
}<jupyter_output><empty_output><jupyter_text>Now you can access output tensor.<jupyter_code>output := interpreter.GetOutputTensor(0)
output.Float32s()
fmt.Println(
    int(output.Float32s()[0]+0.5),
    int(output.Float32s()[1]+0.5),
    int(output.Float32s()[2]+0.5),
    int(output.Float32s()[3]+0.5),
)<jupyter_output>0 0 1 0<jupyter_text>The numbers can be decoded with `dec` above.<jupyter_code>dec(output.Float32s())<jupyter_output><empty_output><jupyter_text>This `2` mean `Buzz` that is encoded for the number that can be divided by 5.```pythondef fizzbuzz(i):    if   i % 15 == 0: return np.array([0, 0, 0, 1])    elif i % 5  == 0: return np.array([0, 0, 1, 0])    elif i % 3  == 0: return np.array([0, 1, 0, 0])    else:             return np.array([1, 0, 0, 0])```This is an offset to the of four values that makes pulse 1. The binary encodeding makes reversed values:* `0001` is `1` that indicate `Fizz`* `0010` is `2` that indicate `Buzz`* `0100` is `3` that indicate `FizzBuzz`* `1000` is `4` that indicate original number. i.e. `5`. Let's add function to display Fizz, Buzz, FizzBuzz or input number.<jupyter_code>func display(v []float32, i int) {
    switch dec(v) {
    case 0:
        fmt.Println(i)
    case 1:
        fmt.Println("Fizz")
    case 2:
        fmt.Println("Buzz")
    case 3:
        fmt.Println("FizzBuzz")
    }
}
display(output.Float32s(), 5)<jupyter_output>Buzz<jupyter_text>Finally, let's display FizzBuzz with one hundred numbers.<jupyter_code>for i := 1; i <= 16; i++ {
    copy(interpreter.GetInputTensor(0).Float32s(), bin(i, 7))
    interpreter.Invoke()
    display(interpreter.GetOutputTensor(0).Float32s(), i)
}<jupyter_output>1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
{'fraction_non_alphanumeric': 0.14029038112522685, 'fraction_numerical': 0.032667876588021776, 'mean_word_length': 5.0627062706270625, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 59, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '122901', 'license': 'MIT', 'n_tokens_mistral': 2118, 'n_tokens_neox': 1974, 'n_words': 486, 'path': '_example/fizzbuzz/Fun with TensorFlow Lite using go-tflite.ipynb', 'repo_name': 'cwarden/go-tflite', 'size': '5510'}