Created
January 6, 2019 13:56
-
-
Save linnil1/c38f61ccd6dd39a4da1aa2c600d1b2fe to your computer and use it in GitHub Desktop.
Fizz Buzz for Mechine Learning(pytorch and keras)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import keras | |
from keras.models import Sequential | |
from keras.layers import Dense | |
import tensorflow as tf | |
n = 1000 | |
model_file = 'fizzbuzz_keras.h5' | |
valid_n = 100 | |
# model | |
model = Sequential() | |
model.add(Dense(1000, activation='relu', input_shape=(10,))) | |
model.add(Dense(4, activation='softmax')) | |
# Create classes | |
x = np.arange(n) | |
y = np.zeros(n).astype(np.int) | |
y += (x % 3 == 0) * 1 | |
y += (x % 5 == 0) * 2 | |
# Number to binary | |
x = np.zeros([n, 10]).astype(np.int) | |
for i in range(n): | |
x[i] = list(np.binary_repr(i, width=10)) | |
one_hot = np.zeros([n, 4]) | |
one_hot[np.arange(n), y] = 1 | |
y = one_hot | |
# Shuffle and cut to train and vaildation | |
# sh = np.random.permutation(n) | |
sh = np.arange(n) # not shuffle | |
X = x[sh] | |
Y = y[sh] | |
trainX = X[valid_n:] | |
trainY = Y[valid_n:] | |
validX = X[:valid_n] | |
validY = Y[:valid_n] | |
# init | |
config = tf.ConfigProto() | |
config.gpu_options.allow_growth = True | |
session = tf.Session(config=config) | |
# train | |
model.compile(loss=keras.losses.categorical_crossentropy, | |
optimizer=keras.optimizers.Adam(), | |
metrics=['accuracy']) | |
model.fit(trainX, trainY, | |
batch_size=20, | |
epochs=100, | |
verbose=1, | |
validation_data=(validX, validY)) | |
# save | |
model.save(model_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import torch | |
from torch import nn | |
import torch.nn.functional as F | |
from torch.utils.data import Dataset, DataLoader | |
n = 1000 | |
model_file = 'fizzbuzz_pytorch.pt' | |
valid_n = 100 | |
# model | |
class Net(nn.Module): | |
def __init__(self): | |
super(Net, self).__init__() | |
self.nn = nn.Sequential( | |
nn.Linear(10, 1000), | |
nn.ReLU(), | |
nn.Linear(1000, 4), | |
# nn.Softmax() | |
) | |
def forward(self, x): | |
x = self.nn(x) | |
return x | |
# Create classes | |
x = np.arange(n) | |
y = np.zeros(n).astype(np.int) | |
y += (x % 3 == 0) * 1 | |
y += (x % 5 == 0) * 2 | |
# Number to binary | |
x = np.zeros([n, 10]).astype(np.int) | |
for i in range(n): | |
x[i] = list(np.binary_repr(i, width=10)) | |
# Shuffle and cut to train and vaildation | |
# sh = np.random.permutation(n) | |
sh = np.arange(n) # not shuffle | |
X = x[sh] | |
Y = y[sh] | |
trainX = X[valid_n:] | |
trainY = Y[valid_n:] | |
validX = X[:valid_n] | |
validY = Y[:valid_n] | |
class Dataset(Dataset): | |
def __init__(self, x, y, transform=None): | |
self.x = x | |
self.y = y | |
def __len__(self): | |
return len(self.x) | |
def __getitem__(self, idx): | |
return [torch.FloatTensor(self.x[idx]), self.y[idx]] | |
def resultGet(output, target): | |
with torch.no_grad(): | |
_, predict = torch.max(output, 1) | |
return (predict == target).sum().float() | |
# pytorch load | |
trainloader = DataLoader(Dataset(trainX, trainY), batch_size=20, | |
shuffle=True, num_workers=0) | |
validloader = DataLoader(Dataset(validX, validY), batch_size=20, | |
shuffle=True, num_workers=0) | |
model = Net().cuda() | |
optimizer = torch.optim.Adam(model.parameters()) | |
# train | |
for epoch in range(150): | |
total = 0 | |
correct = 0 | |
all_loss = [] | |
model.train() | |
for batch_idx, (data, target) in enumerate(trainloader): | |
optimizer.zero_grad() | |
target = target.cuda() | |
output = model(data.cuda()) | |
loss = F.cross_entropy(output, target) | |
loss.backward() | |
optimizer.step() | |
with torch.no_grad(): | |
correct += resultGet(output, target).cpu() | |
total += len(data) | |
all_loss.append(loss.cpu().numpy()) | |
print('Train Epoch: {:5d}'.format(epoch)) | |
print('\tTrain: Loss: {:.6f}\tAcc: {:.6f}'.format( | |
np.mean(all_loss), correct / total), end=' ') | |
total = 0 | |
correct = 0 | |
all_loss = [] | |
model.eval() | |
with torch.no_grad(): | |
for batch_idx, (data, target) in enumerate(validloader): | |
output = model(data.cuda()) | |
correct += resultGet(output, target.cuda()) | |
total += len(target) | |
all_loss.append(loss.cpu().numpy()) | |
print('\tTest: \tLoss: {:.6f}\tAcc: {:.6f}'.format( | |
np.mean(all_loss), correct / total)) | |
# save | |
torch.save(model.state_dict(), model_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment