python - 'nim' 游戏的 Keras 神经网络模型
问题描述
nim 游戏是一个有 21 场比赛和 2 场比赛的比赛,
我们一个接一个地进行1到3场比赛。目标是让对方拿下最后一场比赛。
这个问题似乎不难用 Q Table 方法解决。
我想尝试使用神经网络(我想在此之后进行深度 Q 学习)。
目前,我编写了一个程序,模拟 10.000 场比赛,并将每一个动作存储在一个数组中,如果我们输掉比赛,我们会从数组中删除这些动作,如果我们赢了,我们会保留我们在游戏中所做的动作。
所以,我有 2 个数组:
- 第一个是我们移动之前的匹配数数组(在 21 和 2 之间)
- 另一个是一个数组,其中包含我们参加的比赛数并导致我们赢得比赛(它应该在 1 到 3 之间,但我将它标准化为 0 到 1 之间,除以 3)。
这是我的所有代码:
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 中设计隐藏层的方法。
解决方案
推荐阅读
- react-native - 应用程序在开发中运行良好,但在生成发布 APK 时失败
- blender - Blender:如何将动画导出为 GLTF 文件
- javascript - jQuery在单击单选按钮时使用id而不是值显示div
- c# - 在过滤掉某些属性的同时序列化 JSON.NET JObject
- css - 使用 apoc.load.html 时,是否可以返回完整的 HTML 而不仅仅是文本?
- xquery - 不使用 Group by 的替代 xquery
- matplotlib - 最大递归深度超过 ax.set_xlim() 和 ax.callbacks.connect('xlim_changed') 的组合
- regex - 从日志文件中提取一个字母(日志级别)
- python - 逐帧下载 MJPEG 流
- cordova - 贝宝交易 ID