首页 > 解决方案 > 'nim' 游戏的 Keras 神经网络模型

问题描述

nim 游戏是一个有 21 场比赛和 2 场比赛的比赛,

我们一个接一个地进行1到3场比赛。目标是让对方拿下最后一场比赛。

这个问题似乎不难用 Q Table 方法解决。

我想尝试使用神经网络(我想在此之后进行深度 Q 学习)。

目前,我编写了一个程序,模拟 10.000 场比赛,并将每一个动作存储在一个数组中,如果我们输掉比赛,我们会从数组中删除这些动作,如果我们赢了,我们会保留我们在游戏中所做的动作。

所以,我有 2 个数组:

这是我的所有代码:

def game():

remaining = 21
history=[]
playerPlaying = random.randint(0,1)
while remaining > 0:
    move = random.randint(1,3)
    remaining-= move
    if playerPlaying:
        history= history+[(remaining+move,move)]
    playerPlaying = not playerPlaying
#return loosing player
return (not playerPlaying, history)
        
xs=[]
ys=[]
for i in range(10000):
    res = game()
    if not res[0] == True:
        for e in res[1]:
            xs+=[e[0]]
            ys+=[e[1]/3]
            
xs=np.array([xs])
ys=np.array([ys])
            
        
model = Sequential()
model.add(Dense(8, input_dim=1, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='mean_squared_error',
              optimizer='adam',
              metrics=['accuracy'])


model.fit(xs,ys, epochs=1000, verbose=2)

打印(model.predict([2,3,4,5,6,7]))

我的问题是这个模型似乎没有改进。它远不是一个好的结果,并给出这种训练输出的形状:

Epoch 996/1000
807/807 - 1s - loss: 0.0708 - accuracy: 0.3222
Epoch 997/1000
807/807 - 1s - loss: 0.0708 - accuracy: 0.3222
Epoch 998/1000
807/807 - 1s - loss: 0.0708 - accuracy: 0.3222
Epoch 999/1000
807/807 - 1s - loss: 0.0708 - accuracy: 0.3222
Epoch 1000/1000
807/807 - 1s - loss: 0.0708 - accuracy: 0.3222

我犯了一个大错误吗?我不明白为什么我的 Xs 和 Yx 不能让模型学习。

我只有深度学习的基础知识。我还不太熟悉优化器和在 NN 中设计隐藏层的方法。

标签: pythontensorflowkerasdeep-learningneural-network

解决方案


推荐阅读