コード例 #1
0
ファイル: test_snake.py プロジェクト: Allen3Young/qlearning4k
from keras.models import Sequential
from keras.layers import *
from qlearning4k.games import Snake
from keras.optimizers import *
from qlearning4k import Agent

grid_size = 10
nb_frames = 4
nb_actions = 5

model = Sequential()
model.add(BatchNormalization(axis=1, input_shape=(nb_frames, grid_size, grid_size)))
model.add(Convolution2D(16, nb_row=3, nb_col=3, activation='relu'))
model.add(Convolution2D(32, nb_row=3, nb_col=3, activation='relu'))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(nb_actions))
model.compile(RMSprop(), 'MSE')

snake = Snake(grid_size)

agent = Agent(model=model, memory_size=-1, nb_frames=nb_frames)
agent.train(snake, batch_size=64, nb_epoch=10000, gamma=0.8)
agent.play(snake)
コード例 #2
0
from keras.layers import *
from keras.optimizers import *
from qlearning4k import Agent

#------------------------------------
from server import Game
#------------------------------------

from keras import backend as K
K.set_image_dim_ordering('th')

grid_size = 21
nb_frames = 1
nb_actions = 5

model = Sequential()
model.add(Conv2D(16, (3, 3), activation='relu', input_shape=(nb_frames, grid_size, grid_size, 3)))
model.add(Conv2D(32, (4, 4), activation='relu'))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(nb_actions))
model.compile(RMSprop(), 'MSE')

#------------------------------------
game = Game()
#------------------------------------

agent = Agent(model=model, memory_size=-1, nb_frames=nb_frames)
agent.train(game, batch_size=1, nb_epoch=10000, gamma=0.8)
agent.play(game)
コード例 #3
0
ファイル: test_catch.py プロジェクト: Allen3Young/qlearning4k
from keras.models import Sequential
from keras.layers import Flatten, Dense
from qlearning4k.games import Catch
from keras.optimizers import *
from qlearning4k import Agent

grid_size = 10
hidden_size = 100
nb_frames = 1

model = Sequential()
model.add(Flatten(input_shape=(nb_frames, grid_size, grid_size)))
model.add(Dense(hidden_size, activation='relu'))
model.add(Dense(hidden_size, activation='relu'))
model.add(Dense(3))
model.compile(sgd(lr=.2), "mse")

catch = Catch(grid_size)
agent = Agent(model=model)
agent.train(catch, batch_size=10, nb_epoch=1000, epsilon=.1)
agent.play(catch)
コード例 #4
0
ファイル: test_catch.py プロジェクト: ibkrish/qlearning4k
from keras.models import Sequential
from keras.layers import Flatten, Dense
from qlearning4k.games import Catch
from keras.optimizers import *
from qlearning4k import Agent

grid_size = 10
hidden_size = 100
nb_frames = 1

model = Sequential()
model.add(Flatten(input_shape=(nb_frames, grid_size, grid_size)))
model.add(Dense(hidden_size, activation='relu'))
model.add(Dense(hidden_size, activation='relu'))
model.add(Dense(3))
model.compile(sgd(lr=.2), "mse")

catch = Catch(grid_size)
agent = Agent(model=model)
agent.train(catch, batch_size=10, nb_epoch=500, epsilon=.1)
agent.play(catch)

# serialize model to JSON
model_json = model.to_json()
with open('C:/temp/py/fariz/catch' + '.json', 'w') as json_file:
    json_file.write(model_json)

# serialize weights to HDF5
model.save_weights('C:/temp/py/fariz/catch' + '.h5')
コード例 #5
0
time.sleep(1)

nb_frames = 10
nb_actions = len(car_game.get_possible_actions())
linput = len(car_game.get_state())

print("get_state()", car_game.get_state())
print("get_possible_actions()", car_game.get_possible_actions())

activation_method = 'sigmoid'

print("nb_actions", nb_actions)
model = Sequential()
model.add(Flatten(input_shape=(nb_frames, linput)))
model.add(Dense(512, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.7))
model.add(Dense(512, activation='relu'))
model.add(Dense(512, activation=activation_method))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.7))
model.add(Dense(512, activation='relu'))
model.add(Dense(512, activation=activation_method))
model.add(Dense(nb_actions))
model.compile(RMSprop(), 'MSE')

agent = Agent(model=model, memory_size=-1, nb_frames=nb_frames)
agent.train(car_game, batch_size=128, nb_epoch=5000, gamma=0.4)
agent.play(car_game)
コード例 #6
0
ファイル: test_snake.py プロジェクト: zeyuan1987/qlearning4k
from keras.models import Sequential
from keras.layers import *
from qlearning4k.games import Snake
from keras.optimizers import *
from qlearning4k import Agent

grid_size = 10
nb_frames = 4
nb_actions = 5

model = Sequential()
model.add(
    Convolution2D(16,
                  nb_row=3,
                  nb_col=3,
                  activation='relu',
                  input_shape=(nb_frames, grid_size, grid_size)))
model.add(Convolution2D(32, nb_row=3, nb_col=3, activation='relu'))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(nb_actions))
model.compile(RMSprop(), 'MSE')

snake = Snake(grid_size)

agent = Agent(model=model, memory_size=-1, nb_frames=nb_frames)
agent.train(snake, batch_size=64, nb_epoch=10000, gamma=0.8)
agent.play(snake)
コード例 #7
0
ファイル: play_2048.py プロジェクト: choupi/NDHUDLWorkshop
from keras.models import Sequential,model_from_json
from keras.layers import *
from keras.optimizers import *
from qlearning4k import Agent
from my2048 import mGame

grid_size = 4
nb_frames = 1
nb_actions = 4

model = model_from_json(open('2048_model.json', 'rb').read())
print model.input_shape

m2048 = mGame(vis=True)
#m2048 = mGame()
print m2048.get_frame().shape

agent = Agent(model=model, memory_size=65536, nb_frames=nb_frames)
model.load_weights('2048_weights.h5')
model.summary()
print model.get_weights()

agent.play(m2048, epsilon=0.1, visualize=False)
コード例 #8
0
model.compile(optimizer=Adam(1e-2), loss='mse')

enemy_model = Sequential.from_config(model.get_config())
enemy_model.compile(optimizer=Adam(1e-2), loss='mse')

tron = Tron(enemy_model)
agent = Agent(model=model)

print('Initial phase')

agent.train(game=tron,
            epsilon=(1.0, 0.1),
            epsilon_rate=0.5,
            batch_size=32,
            nb_epoch=10000,
            gamma=0.9,
            checkpoint=500)
agent.play(tron, nb_epoch=1)
tron.update_ai_model(agent.model)

for i in range(10):
    print('Phase #', i + 1)
    agent.train(game=tron,
                epsilon=0.1,
                batch_size=32,
                nb_epoch=10000,
                gamma=0.9,
                checkpoint=100)
    agent.play(tron, nb_epoch=1)
    game.update_ai_model()
コード例 #9
0
from model import model
from qlearning4k import Agent
from flappy_bird import FlappyBird

game = FlappyBird(frame_rate=30, sounds=True)
model.load_weights('weights.dat')
agent = Agent(model)
agent.play(game, nb_epoch=100, epsilon=0.01, visualize=False)
コード例 #10
0
                    3,
                    subsample=(2, 2),
                    activation='relu',
                    dim_ordering='th')(val)
val = Convolution2D(128,
                    3,
                    3,
                    subsample=(2, 2),
                    activation='relu',
                    dim_ordering='th')(val)
val = Convolution2D(256,
                    3,
                    3,
                    subsample=(2, 2),
                    activation='relu',
                    dim_ordering='th')(val)
val = Flatten()(val)
val = Dense(1)(val)
val = RepeatVector(nb_actions)(val)
val = Flatten()(val)

y_out = merge([adv, val], mode='sum')

model = Model(input=x_in, output=y_out)
model.compile(optimizer='rmsprop', loss='mse')

agent = Agent(model=model, memory_size=-1, nb_frames=nb_frames)
agent.train(ql4kgame, batch_size=64, nb_epoch=300, gamma=0.7)
model.save("dqn_model.h5")
agent.play(ql4kgame)
コード例 #11
0
ファイル: test_frogger.py プロジェクト: Saritus/qlearning4k
from keras.models import Sequential
from keras.layers import *
from qlearning4k.games import Frogger
from keras.optimizers import *
from qlearning4k import Agent

rows = 10
cols = 10
hidden_size = 100
nb_frames = 1

model = Sequential()
model.add(Flatten(input_shape=(nb_frames, rows, cols)))
model.add(Dense(hidden_size, activation='relu'))
model.add(Dense(hidden_size, activation='relu'))
model.add(Dense(5, activation='softmax'))
model.compile(sgd(lr=.2), "mse")

game = Frogger(rows, cols)
agent = Agent(model=model)
agent.train(game, batch_size=50, nb_epoch=10000, epsilon_rate=0.2)
agent.play(game, nb_epoch=10)