博亚app下载官网|APP下载最新版本

热门关键词:  as  xxx  as) and 1=2 (

您的位置: 主页 > 资讯动态 > 热门新闻 >
Chrome潜伏的恐龙跳一跳,已经被AI轻松掌握了
作者:博亚app最新下载官网 来源:博亚app最新下载官网 点击: 发布日期: 2023-05-21 19:43
信息摘要:
博亚app下载官网|APP下载最新版本夏乙 郭一璞 发自 凹非寺量子位 出品 | 民众号 QbitAI什么!未毗连到互联网!!明显是联网状态,为什么我想会见的页面无!法!打!开!淡定。作为一个Google Chrome浏览器的用户,当你看到上面谁人页面时,不要沮丧。 换个角度一想,墙内还能有更多的Play时间哦~你有没有注意到画面里谁人小恐龙?当你遇到打不开网页的时候,只需要再点击一下这个页面(手机),或者按下空格(电脑),随着小恐龙轻轻一跳——一个新世界开启了。...
本文摘要:夏乙 郭一璞 发自 凹非寺量子位 出品 | 民众号 QbitAI什么!未毗连到互联网!!明显是联网状态,为什么我想会见的页面无!法!打!开!淡定。作为一个Google Chrome浏览器的用户,当你看到上面谁人页面时,不要沮丧。 换个角度一想,墙内还能有更多的Play时间哦~你有没有注意到画面里谁人小恐龙?当你遇到打不开网页的时候,只需要再点击一下这个页面(手机),或者按下空格(电脑),随着小恐龙轻轻一跳——一个新世界开启了。

博亚app最新下载官网

夏乙 郭一璞 发自 凹非寺量子位 出品 | 民众号 QbitAI什么!未毗连到互联网!!明显是联网状态,为什么我想会见的页面无!法!打!开!淡定。作为一个Google Chrome浏览器的用户,当你看到上面谁人页面时,不要沮丧。

换个角度一想,墙内还能有更多的Play时间哦~你有没有注意到画面里谁人小恐龙?当你遇到打不开网页的时候,只需要再点击一下这个页面(手机),或者按下空格(电脑),随着小恐龙轻轻一跳——一个新世界开启了。这个“恐龙跳一跳”其实是藏在Chrome浏览器里很多多少年的一个彩蛋。

小恐龙是一只霸王龙(T-Rex)。2013年Chrome开始用这个小恐龙的图像取代令人烦恼的404页面。2014年秋天,这只恐龙被正式革新成一个横版小游戏。

以彩蛋的方式隐藏在新版Chrome浏览器里。呐,如果你还不知道这个彩蛋,可以抓紧试一试。例如说——会见一个不翻墙就看不了的网页或者直接输入:chrome://dino或者会见:https://chromedino.com/ (需翻墙)厥后,这个小游戏也成了不少AI练手的工具。

好比最近就有人在YouTube上贴了一段视频,展示了他如何用神经网络+遗传算法,让一个AI系统独秀于浏览器之中。我们把英华的部门截取了一下,就是下面这段视频。

动图版:速度已经快到飞起总而言之,一句话,这个AI能轻松玩到2万多分……你能玩到几分?或许率是玩不到这个结果的吧。究竟在chromedino.com页面上,人类玩家的历史最高分是18842。不外,上传这段视频的作者,并没有详细宣布他用的方法,固然也没有给出一个开源的地址。

不外没关系,也有别人公然分享了更多细节。例如,GitHub上就有一个开源的代码“IAMDinosaur”,同样也是使用神经网络+遗传算法,来搞定恐龙跳一跳。地址在此:https://github.com/ivanseidel/IAMDinosaur美中不足,上面这个项目也没有配上太详尽的解读。然而好消息是,最近有个外洋的小哥Ravi Munde,列了一份很是详尽的教程。

这个教程用的方法是强化学习中的Q-learning,比力适合入门练手,而且对硬件的要求不高。量子位搬运这份教程如下。

Q-learning相识/温习一下对动物来说,强化学习的能力是与生俱来的。拿儿童学步来举例,如果小朋侪努力的迈出第一步,就会获得怙恃的勉励——可能是拍手叫好,也可能是一块糖;但如果小朋侪坚决不愿学习走路,那怙恃就不会给它糖吃了。

强化学习就是依照这类激励行为而设置的。而在这个游戏中,对我们的AI小恐龙来说,强化学习需要让他在无监视的情况下,先认识到做出差别行动的效果,而且以获得高分为最高激励。一个典型的强化学习闭环Ravi Munde用Q-learning模拟了一个特殊函数,这个函数驱动AI在差别状况下做出正确的选择。

Q-learning是强化学习的一种无模型实现,凭据Q值对每个状态举行判断此时如果接纳行动,能获得怎样的奖励。一个样本Q表让我们相识数据的结构。

在恐龙跑酷游戏中,状态是当前的游戏截图,能接纳的行动是跳或不跳[0,1]一个样本Q表Ravi Munde决议用深度神经网络来决议小恐龙何时起跳,而且要在最简朴的强化学习实现基础上,引入差别参数来辅助它。缺乏已标志的数据让强化学习很是不稳定。为了获得适用于这个游戏的数据,Munde小哥决议,先让小恐龙自己瞎跳几千次,把每个行动的反馈记下来,然后从数据中随机挑选一些来训练模型。

但之后,Munde小哥发现,他训练了一个倔强的模型——模型坚定的认为,跳,一定比不跳好。所以,为了让模型在训练时能在跳与不跳之间多实验一下,他引入了一个函数ɛ来决议行动的随机性,然后再逐渐减小它的值来削减随机性,最终让模型去选择最有可能获得奖励的行动。赞誉漫衍(Credit Assignment)问题可能会让模型陷入杂乱——现在获得的奖励究竟来自于已往的哪个行为呢?在恐龙跑酷游戏中,小恐龙跳到半空中后无法再次跳跃,但模型可能会在恐龙处于半空中时发出跳跃指令,这种情况就让恐龙很是容易砸到仙人掌上。

在这种情况下,“砸到仙人掌上”这个负反馈实际上是此前上一次做出跳跃决议的效果,而不是刚刚恐龙在半空中时做出的跳跃效果所导致的。在面临这种问题的情况下,可以引入贴现因子(Discount Factor)γ来决议模型做出行动时看得多远。

γ间接解决了赞誉漫衍问题,在这个游戏中,当γ=0.99时,模型认识到在无障碍时随便跳会导致真的遇到障碍时自己正在半空中,无法继续跳跃。除了这两个参数之外,后面就险些不需要任何参数了。#game parametersGAMMA = 0.99 # decay rate of past observations original 0.99OBSERVATION = 50000. # timesteps to observe before trainingEXPLORE = 100000 # frames over which to anneal epsilonFINAL_EPSILON = 0.0001 # final value of epsilonINITIAL_EPSILON = 0.1 # starting value of epsilonREPLAY_MEMORY = 50000 # number of previous transitions to rememberBATCH = 32 # size of minibatchFRAME_PER_ACTION = 1你需要准备的是Python 3.6SeleniumOpenCVPILChromium driver for SeleniumKeras略微解释一下这几个工具。构建这个AI模型,需要用Python编程。

而游戏是用JavaScript写成的。所以,得借助一些工具才气更好地相同。Selenium是一种盛行的浏览器自动化工具,用于向浏览器发送操作指令,以及获取种种游戏参数。接口的事情搞定了,还得想措施获得游戏截屏。

用Selenium也行,可是速度很慢,截屏和处置惩罚一次约莫得1秒钟。用PIL和OpenCV能够更好地完成截屏和图像预处置惩罚,可以到达5fps的帧率。你可能以为还是慢,但已经足够敷衍这个游戏了。

游戏模块下面这个模块,实现了Python和浏览器(使用Selenium)的相同。'''* Game class: Selenium interfacing between the python and browser* __init__(): Launch the broswer window using the attributes in chrome_options* get_crashed() : return true if the agent as crashed on an obstacles. Gets javascript variable from game decribing the state* get_playing(): true if game in progress, false is crashed or paused* restart() : sends a signal to browser-javascript to restart the game* press_up(): sends a single to press up get to the browser* get_score(): gets current game score from javascript variables.* pause(): pause the game* resume(): resume a paused game if not crashed* end(): close the browser and end the game'''class Game: def __init__(self,custom_config=True): chrome_options = Options() chrome_options.add_argument("disable-infobars") self._driver = webdriver.Chrome(executable_path = chrome_driver_path,chrome_options=chrome_options) self._driver.set_window_position(x=-10,y=0) self._driver.set_window_size(200, 300) self._driver.get(os.path.abspath(game_url)) #modifying game before training if custom_config: self._driver.execute_script("Runner.config.ACCELERATION=0") def get_crashed(self): return self._driver.execute_script("return Runner.instance_.crashed") def get_playing(self): return self._driver.execute_script("return Runner.instance_.playing") def restart(self): self._driver.execute_script("Runner.instance_.restart()") time.sleep(0.25)# no actions are possible # for 0.25 sec after game starts, # skip learning at this time and make the model wait def press_up(self): self._driver.find_element_by_tag_name("body").send_keys(Keys.ARROW_UP) def get_score(self): score_array = self._driver.execute_script("return Runner.instance_.distanceMeter.digits") score = ''.join(score_array) # the javascript object is of type array with score in the formate[1,0,0] which is 100. return int(score) def pause(self): return self._driver.execute_script("return Runner.instance_.stop()") def resume(self): return self._driver.execute_script("return Runner.instance_.play()") def end(self): self._driver.close()恐龙智能体模块这个模块在游戏模块的资助下,用于控制小恐龙的行动。class DinoAgent: def __init__(self,game): #takes game as input for taking actions self._game = game; self.jump(); #to start the game, we need to jump once time.sleep(.5) # no action can be performed for the first time when game starts def is_running(self): return self._game.get_playing() def is_crashed(self): return self._game.get_crashed() def jump(self): self._game.press_up() def duck(self): self._game.press_down()游戏状态模块神经网络直接使用这个模块,来执行操作并获取新的状态。'''get_state(): accepts an array of actions, performs the action on the agentreturns : new state, reward and if the game ended.'''class Game_sate: def __init__(self,agent,game): self._agent = agent self._game = game def get_state(self,actions): score = self._game.get_score() reward = 0.1*score/10 # dynamic reward calculation is_over = False #game over if actions[1] == 1: #else do nothing self._agent.jump() reward = 0.1*score/11 image = grab_screen() if self._agent.is_crashed(): self._game.restart() reward = -11/score is_over = True return image, reward, is_over #return the Experience tuple预处置惩罚游戏修改原始的游戏相对庞大,好比游戏速度会逐渐加速,障碍物会改变,还会泛起云朵、星星、地面纹理等。

一次同时学习这么多工具会消耗大量时间,甚至在训练历程中引入不须要的噪音。为此作者修改了游戏的源代码、简化局势,去除了一些视觉元素(云、历史最佳结果等),另有让恐龙的奔跑速度保持稳定。原图修改后图像处置惩罚原始截图的分辨率为1200×300,包罗三个通道。

作者计划使用4个一连的屏幕截图作为模型的单一输入,也就是1200×300×3×4。问题是,这个小哥只有一个i7的CPU可用,所以他的电脑没措施在处置惩罚这个尺寸输入的同时玩游戏。所以,还得继续用OpenCV的库调正截图巨细、裁剪等。

最终输入图像巨细为40×20像素,单通道,并用Canny突出显示边缘。def grab_screen(_driver = None): #bbox = region of interest on the entire screen screen = np.array(ImageGrab.grab(bbox=(40,180,440,400))) image = process_img(screen)#processing image as required return imagedef process_img(image): #game is already in grey scale canvas, canny to get only edges and reduce unwanted objects(clouds) # resale image dimensions image = cv2.resize(image, (0,0), fx = 0.15, fy = 0.10) #crop out the dino agent from the frame image = image[2:38,10:50] #img[y:y+h, x:x+w] image = cv2.Canny(image, threshold1 = 100, threshold2 = 200) #apply the canny edge detection return image然后,堆叠4张图建立单个输入,也就是:40×20×4。请注意,这里小恐龙也淘汰掉了,因为整个学习历程,只需要知道障碍物和与边缘的距离即可。模型架构现在输入有了,用模型输出来玩游戏的方法也有了,只差模型架构。

小哥选择把3个卷积层压平,毗连到一个512神经元的全毗连层(dense layer)上。池化层直接被砍掉了,这个工具在图像分类问题上很有用,可是玩Dino的时候神经网络只需要知道障碍物的位置,池化层就起不了什么作用了。多层网络架构这个模型的输出,形状和可能的操作数量一样。模型会预测种种操作的Q值,也叫discounted future reward,然后我们选数值最高的谁人。

下面这段代码,就能召唤一个用TensorFlow后端的Keras来搭建的模型:#model hyper parametersLEARNING_RATE = 1e-4img_rows , img_cols = 40,20img_channels = 4 #We stack 4 framesACTIONS = 2def buildmodel(): print("Now we build the model") model = Sequential() model.add(Conv2D(32, (8, 8), strides=(4, 4), padding='same',input_shape=(img_cols,img_rows,img_channels))) #20*40*4 model.add(Activation('relu')) model.add(Conv2D(64, (4, 4), strides=(2, 2), padding='same')) model.add(Activation('relu')) model.add(Conv2D(64, (3, 3), strides=(1, 1), padding='same')) model.add(Activation('relu')) model.add(Flatten()) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dense(ACTIONS)) adam = Adam(lr=LEARNING_RATE) model.compile(loss='mse',optimizer=adam) print("We finish building the model") return model开始训练接下来,就是见证奇迹的时刻~~也就是用一段代码来训练模型,这段代码的任务是:从无操作开始,获得初始状态initial state(s_t)视察玩游戏的历程,代码中的OBSERVATION表现步数预测一个操作的效果在Replay Memory中存储履历训练阶段,从Replay Memory里随机选择一组,用它来训练模型如果game over了,就重开一局更详细的,可以看这段自带注释的代码:'''Parameters:* model => Keras Model to be trained* game_state => Game State module with access to game environment and dino* observe => flag to indicate wherther the model is to be trained(weight updates), else just play'''def trainNetwork(model,game_state): # store the previous observations in replay memory D = deque() #load from file system # get the first state by doing nothing do_nothing = np.zeros(ACTIONS) do_nothing[0] =1 #0 => do nothing, #1=> jump x_t, r_0, terminal = game_state.get_state(do_nothing) # get next step after performing the action s_t = np.stack((x_t, x_t, x_t, x_t), axis=2).reshape(1,20,40,4) # stack 4 images to create placeholder input reshaped 1*20*40*4 OBSERVE = OBSERVATION epsilon = INITIAL_EPSILON t = 0 while (True): #endless running loss = 0 Q_sa = 0 action_index = 0 r_t = 0 #reward at t a_t = np.zeros([ACTIONS]) # action at t #choose an action epsilon greedy if random.random() <= epsilon: #randomly explore an action print("----------Random Action----------") action_index = random.randrange(ACTIONS) a_t[action_index] = 1 else: # predict the output q = model.predict(s_t) #input a stack of 4 images, get the prediction max_Q = np.argmax(q) # chosing index with maximum q value action_index = max_Q a_t[action_index] = 1 # o=> do nothing, 1=> jump #We reduced the epsilon (exploration parameter) gradually if epsilon > FINAL_EPSILON and t > OBSERVE: epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / EXPLORE #run the selected action and observed next state and reward x_t1, r_t, terminal = game_state.get_state(a_t) last_time = time.time() x_t1 = x_t1.reshape(1, x_t1.shape[0], x_t1.shape[1], 1) #1x20x40x1 s_t1 = np.append(x_t1, s_t[:, :, :, :3], axis=3) # append the new image to input stack and remove the first one # store the transition in D D.append((s_t, action_index, r_t, s_t1, terminal)) D.popleft() if len(D) > REPLAY_MEMORY #only train if done observing; sample a minibatch to train on trainBatch(random.sample(D, BATCH)) if t > OBSERVE s_t = s_t1 t = t + 1 print("TIMESTEP", t, "/ EPSILON", epsilon, "/ ACTION", action_index, "/ REWARD", r_t,"/ Q_MAX " , np.max(Q_sa), "/ Loss ", loss)将这个模型用到从Replay Memory里随机选择的一批上:def trainBatch(minibatch):for i in range(0, len(minibatch)): loss = 0 inputs = np.zeros((BATCH, s_t.shape[1], s_t.shape[2], s_t.shape[3])) #32, 20, 40, 4 targets = np.zeros((inputs.shape[0], ACTIONS)) #32, 2 state_t = minibatch[i][0] # 4D stack of images action_t = minibatch[i][1] #This is action index reward_t = minibatch[i][2] #reward at state_t due to action_t state_t1 = minibatch[i][3] #next state terminal = minibatch[i][4] #wheather the agent died or survided due the action inputs[i:i + 1] = state_t targets[i] = model.predict(state_t) # predicted q values Q_sa = model.predict(state_t1) #predict q values for next step if terminal: targets[i, action_t] = reward_t # if terminated, only equals reward else: targets[i, action_t] = reward_t + GAMMA * np.max(Q_sa) loss += model.train_on_batch(inputs, targets)主体方法挪用下面的方法,就能启动上面的训练流程:#argument: observe, only plays if true, else trainsdef playGame(observe=False): game = Game() dino = DinoAgent(game) game_state = Game_sate(dino,game) model = buildmodel() trainNetwork(model,game_state)效果这个模型,小哥用一周的时间训练了200万帧,其中前100万帧用来调整游戏参数修补bug,后100万帧真正用来训练。现在,这个模型的最好结果是265分。从下面的得分和损失变化图里,能看出模型的loss在后100万帧逐渐稳定,比力低,可是会随时间颠簸。游戏得分后100帧的损失(loss)现在的局限虽然这个模型厥后体现还算可以了,但比人类还是差了一大截。

固然,别忘了这个小哥比力穷,他只有一个i7的CPU。他认为,模型学得还不够快,得分还不够高,要怪这样几个因素:一是因为用CPU来学习,它总是掉帧;二是供这个AI来玩耍的图像实在是太小了,只有40×20,在当前的模型架构下就可能导致了特征的损失,还拖慢了学习速度。如果改用GPU,说不定……相关链接用GPU究竟会不会改善,你们可以拿这份代码来试试:https://github.com/ravi72munde/Chrome-Dino-Reinforcement-Learning原文地址:https://medium.com/acing-ai/how-i-build-an-ai-to-play-dino-run-e37f37bdf153One More Thing其实嘛,让AI搞定小恐龙这件事,本质上跟让AI搞定Flappy Bird是一样的。如果你想深入研究一下这件事,这里再推荐两篇。

机械学习玩转Flappy Bird全书:六大“门户”从原理到代码使用神经网络+遗传算法玩转Flappy Bird | 教程就酱~— 完 —诚挚招聘量子位正在招募编辑/记者,事情所在在北京中关村。期待有才气、有热情的同学加入我们!相关细节,请在量子位民众号(QbitAI)对话界面,回复“招聘”两个字。

量子位 QbitAI · 头条号签约作者վ'ᴗ' ի 追踪AI技术和产物新动态。


本文关键词:Chrome,潜伏,的,恐龙,跳,一跳,博亚app最新下载官网,已经,被,轻松

本文来源:博亚app最新下载官网-www.hfrsfyj.com

全国服务热线

0939-31788211