Skip to content

Instantly share code, notes, and snippets.

@mattn

mattn/make.py Secret

Created May 10, 2019 07:01
Show Gist options
  • Save mattn/25fc981a4bed107cb4b3e4f852f50da4 to your computer and use it in GitHub Desktop.
Save mattn/25fc981a4bed107cb4b3e4f852f50da4 to your computer and use it in GitHub Desktop.
import numpy as np
from keras.models import Sequential, model_from_json
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD, Adam
def 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 = 7
trX = 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)
import onnxmltools
onnx_model = onnxmltools.convert_keras(model, target_opset=0)
onnxmltools.utils.save_model(onnx_model, 'fizzbuzz.onnx')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment