首页 > 其他 > 详细

FizzBuzz小游戏

时间:2021-03-28 17:15:22      阅读:18      评论:0      收藏:0      [点我收藏+]

FizzBuzz小游戏

FizzBuzz是一个简单的小游戏。游戏规则如下:从1开始往上数数,当遇到3的倍数的时候,说fizz,当遇到5的倍数,说buzz,当遇到15的倍数,就说fizzbuzz,其他情况下则正常数数。

我们可以写一个简单的小程序来决定要返回正常数值还是fizz, buzz 或者 fizzbuzz。

代码:

# One-hot encode the desired outputs: [number, "fizz", "buzz", "fizzbuzz"]
def fizz_buzz_encode(i):
    if   i % 15 == 0: return 3
    elif i % 5  == 0: return 2
    elif i % 3  == 0: return 1
    else:             return 0
    
def fizz_buzz_decode(i, prediction):
    return [str(i), "fizz", "buzz", "fizzbuzz"][prediction]

print(fizz_buzz_decode(1, fizz_buzz_encode(1)))
print(fizz_buzz_decode(2, fizz_buzz_encode(2)))
print(fizz_buzz_decode(5, fizz_buzz_encode(5)))
print(fizz_buzz_decode(12, fizz_buzz_encode(12)))
print(fizz_buzz_decode(15, fizz_buzz_encode(15)))

结果:

1
2
buzz
fizz
fizzbuzz

我们首先定义模型的输入与输出(训练数据)

代码:

import numpy as np
import torch

NUM_DIGITS = 10

# Represent each input by an array of its binary digits.
def binary_encode(i, num_digits):
    return np.array([i >> d & 1 for d in range(num_digits)])

trX = torch.Tensor([binary_encode(i, NUM_DIGITS) for i in range(101, 2 ** NUM_DIGITS)])
trY = torch.LongTensor([fizz_buzz_encode(i) for i in range(101, 2 ** NUM_DIGITS)])

然后我们用PyTorch定义模型

代码:

# Define the model
NUM_HIDDEN = 100
model = torch.nn.Sequential(
    torch.nn.Linear(NUM_DIGITS, NUM_HIDDEN),
    torch.nn.ReLU(),
    torch.nn.Linear(NUM_HIDDEN, 4)
)
  • 为了让我们的模型学会FizzBuzz这个游戏,我们需要定义一个损失函数,和一个优化算法。
  • 这个优化算法会不断优化(降低)损失函数,使得模型的在该任务上取得尽可能低的损失值。
  • 损失值低往往表示我们的模型表现好,损失值高表示我们的模型表现差。
  • 由于FizzBuzz游戏本质上是一个分类问题,我们选用Cross Entropyy Loss函数。
  • 优化函数我们选用Stochastic Gradient Descent。

代码:

loss_fn = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr = 0.05)

以下是模型的训练代码

代码:

# Start training it
BATCH_SIZE = 128
for epoch in range(10000):
    for start in range(0, len(trX), BATCH_SIZE):
        end = start + BATCH_SIZE
        batchX = trX[start:end]
        batchY = trY[start:end]

        y_pred = model(batchX)
        loss = loss_fn(y_pred, batchY)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    # Find loss on training data
    loss = loss_fn(model(trX), trY).item()
    print(‘Epoch:‘, epoch, ‘Loss:‘, loss)

最后我们用训练好的模型尝试在1到100这些数字上玩FizzBuzz游戏

代码:

# Output now
testX = torch.Tensor([binary_encode(i, NUM_DIGITS) for i in range(1, 101)])
with torch.no_grad():
    testY = model(testX)
predictions = zip(range(1, 101), list(testY.max(1)[1].data.tolist()))

print([fizz_buzz_decode(i, x) for (i, x) in predictions])

结果:

[‘1‘, ‘2‘, ‘fizz‘, ‘fizz‘, ‘buzz‘, ‘fizz‘, ‘7‘, ‘8‘, ‘fizz‘, ‘buzz‘, ‘11‘, ‘12‘, ‘13‘, ‘14‘, ‘fizzbuzz‘, ‘16‘, ‘17‘, ‘fizz‘, ‘19‘, ‘buzz‘, ‘fizz‘, ‘fizz‘, ‘fizz‘, ‘fizz‘, ‘buzz‘, ‘26‘, ‘fizz‘, ‘28‘, ‘29‘, ‘30‘, ‘31‘, ‘32‘, ‘fizz‘, ‘34‘, ‘buzz‘, ‘fizz‘, ‘37‘, ‘38‘, ‘fizz‘, ‘buzz‘, ‘41‘, ‘fizz‘, ‘43‘, ‘44‘, ‘fizzbuzz‘, ‘46‘, ‘47‘, ‘fizz‘, ‘fizz‘, ‘buzz‘, ‘fizz‘, ‘52‘, ‘53‘, ‘fizz‘, ‘buzz‘, ‘fizz‘, ‘fizz‘, ‘58‘, ‘59‘, ‘fizzbuzz‘, ‘61‘, ‘62‘, ‘fizz‘, ‘64‘, ‘buzz‘, ‘fizz‘, ‘67‘, ‘fizz‘, ‘fizz‘, ‘buzz‘, ‘71‘, ‘72‘, ‘73‘, ‘74‘, ‘fizzbuzz‘, ‘76‘, ‘fizz‘, ‘fizz‘, ‘fizz‘, ‘buzz‘, ‘fizz‘, ‘82‘, ‘83‘, ‘fizz‘, ‘buzz‘, ‘86‘, ‘fizz‘, ‘88‘, ‘89‘, ‘90‘, ‘91‘, ‘92‘, ‘fizz‘, ‘94‘, ‘buzz‘, ‘fizz‘, ‘97‘, ‘98‘, ‘fizz‘, ‘buzz‘]

代码:

print(np.sum(testY.max(1)[1].numpy() == np.array([fizz_buzz_encode(i) for i in range(1,101)])))
testY.max(1)[1].numpy() == np.array([fizz_buzz_encode(i) for i in range(1,101)])

结果:

88
array([ True,  True,  True, False,  True,  True,  True,  True,  True,
        True,  True, False,  True,  True,  True,  True,  True,  True,
        True,  True,  True, False, False,  True,  True,  True,  True,
        True,  True, False,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True, False,  True,  True,  True,  True,  True,
        True, False,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True, False,  True,  True,  True, False,
        True,  True,  True,  True, False,  True, False,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True, False,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True])

FizzBuzz小游戏

原文:https://www.cnblogs.com/luedong/p/14588280.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!