使用 Gym 训练神经网络 [英] Training Neural Networks with Gym
问题描述
我对下面提供的代码有一些问题.我正在研究 python 3.6.我已经重新安装了 Python 和运行代码所需的所有模块.总的来说,我根据这个教程做了所有事情.
I have some issue with code provided below. I am working on python 3.6. I already reinstalled Python and all modules required to run the code. In general I did everything based on this tutorial.
当我运行此代码时,我收到以下警告,但根本没有输出.我不明白这些警告的含义以及如何修复它.如有任何帮助,我将不胜感激.
When I run this code I am getting the following warnings and no output at all. I do not understand what these warnings mean and how I can fix it. I will be grateful for any help.
警告(来自警告模块):文件"D:\Users\Rafal\AppData\Local\Programs\Python\Python36\lib\site包\h5py__init__.py",第 36 行from ._conv import register_converters as _register_converters FutureWarning: 转换 issubdtype 的第二个参数 from不推荐使用浮动到 np.floating .将来,它将被视为np.float64 == np.dtype(float).type
Warning (from warnings module): File "D:\Users\Rafal\AppData\Local\Programs\Python\Python36\lib\site packages\h5py__init__.py", line 36 from ._conv import register_converters as _register_converters FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type
还有:
[33mWARN:gym.spaces.Box 自动检测到 dtype 为 .请提供明确的数据类型.[0m
[33mWARN: gym.spaces.Box autodetected dtype as . Please provide explicit dtype.[0m
我运行的代码:
import gym
import random
import numpy as np
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
from statistics import median, mean
from collections import Counter
LR = 1e-3
env = gym.make("CartPole-v0")
env.reset()
goal_steps = 500
score_requirement = 50
initial_games = 10000
def initial_population():
# [OBS, MOVES]
training_data = []
# all scores:
scores = []
# just the scores that met our threshold:
accepted_scores = []
# iterate through however many games we want:
for _ in range(initial_games):
score = 0
# moves specifically from this environment:
game_memory = []
# previous observation that we saw
prev_observation = []
# for each frame in 200
for _ in range(goal_steps):
# choose random action (0 or 1)
action = random.randrange(0,2)
# do it!
observation, reward, done, info = env.step(action)
# notice that the observation is returned FROM the action
# so we'll store the previous observation here, pairing
# the prev observation to the action we'll take.
if len(prev_observation) > 0 :
game_memory.append([prev_observation, action])
prev_observation = observation
score+=reward
if done: break
# IF our score is higher than our threshold, we'd like to save
# every move we made
# NOTE the reinforcement methodology here.
# all we're doing is reinforcing the score, we're not trying
# to influence the machine in any way as to HOW that score is
# reached.
if score >= score_requirement:
accepted_scores.append(score)
for data in game_memory:
# convert to one-hot (this is the output layer for our neural network)
if data[1] == 1:
output = [0,1]
elif data[1] == 0:
output = [1,0]
# saving our training data
training_data.append([data[0], output])
# reset env to play again
env.reset()
# save overall scores
scores.append(score)
# just in case you wanted to reference later
training_data_save = np.array(training_data)
np.save('saved.npy',training_data_save)
# some stats here, to further illustrate the neural network magic!
print('Average accepted score:',mean(accepted_scores))
print('Median score for accepted scores:',median(accepted_scores))
print(Counter(accepted_scores))
return training_data
推荐答案
针对此错误回答第二个问题:
To answer the second concern with this error:
gym.spaces.Box autodetected dtype as <class 'numpy.float32'>
转到下载的gym"文件所在的目录.进入健身房/空间/并打开box.py"文件.
在第 12 行附近的某个地方,您应该会看到:
Go to the directory of the downloaded "gym" file. Go into gym/spaces/ and open up the "box.py" file.
Somewhere around the 12th line, you should see:
def __init__(self,low.shape=None,high.shape=None,shape=None,dtype=None):
将 dtype=None
更改为 dtype=np.float32
这为我修复了错误.
这篇关于使用 Gym 训练神经网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!