def fit_lstm(train, n, n_seq, n_batch, nb_epoch, n_neurons): # reshape training into [samples, timesteps, features] X, y = train[:, 0:n_lag], train[:, n_lag:] X = X.reshape(X.shape[0], timesteps, X.shape[1]) # design network model = Sequential() model.add( LSTM( n_neurons, return_sequences=True, batch_input_shape=(n_batch, X.shape[1], X.shape[2]), stateful=True, # unroll = True, # recurrent_activation = 'elu', # activation = 'tanh', kernel_initializer=k.glorot_normal(), recurrent_initializer=k.glorot_normal())) # model.add(Dropout(0.001)) # model.add(LSTM(n_neurons_5, return_sequences = True, ## unroll = True, # recurrent_activation = 'elu', # activation = 'tanh', # kernel_initializer = k.he_normal(seed=142), # recurrent_initializer = k.he_normal(seed=142) # )) # model.add(Dropout(0.01)) # model.add(LSTM(n_neurons_2, return_sequences = True, ## unroll = True, ## recurrent_activation = 'elu', ## activation = 'tanh', # kernel_initializer = k.he_normal(seed=142), # recurrent_initializer = k.he_normal(seed=142) # )) # model.add(LSTM(n_neurons_3, return_sequences = True, ## unroll = True, # recurrent_activation = 'elu', # activation = 'tanh', # kernel_initializer = k.he_normal(seed=142), # recurrent_initializer = k.he_normal(seed=142) # )) model.add(LSTM(n_neurons_4)) model.add(Dense(y.shape[1])) model.compile(loss='mean_squared_error', optimizer=optim) # fit network for i in range(nb_epoch): model.fit(X, y, epochs=1, batch_size=n_batch, verbose=1, shuffle=False) model.reset_states() return model
step_counter = 0 load_model = False # Load the model train = True num_games = 0 relu_neg_slope = 0.01 team_size = 1 enemy_size = 0 action_dim = 8 state_size = 58 + (team_size - 1) * 8 + enemy_size * 8 memory = SequentialMemory(limit=10000, window_length=1) critic_input_action = Input(shape=[action_dim], name='critic_ain') critic_input_state = Input(shape=[state_size], name='critic_sin') critic_input_final = layers.concatenate([critic_input_state, critic_input_action], axis=1, name='critic_in') dense1 = Dense(1024, kernel_initializer=initializers.glorot_normal(), bias_initializer=initializers.glorot_normal(), name='critic_d1')(critic_input_final) relu1 = LeakyReLU(alpha=relu_neg_slope, name='critic_re1')(dense1) dense2 = Dense(512, kernel_initializer=initializers.glorot_normal(), bias_initializer=initializers.glorot_normal(), name='critic_d2')(relu1) relu2 = LeakyReLU(alpha=relu_neg_slope, name='critic_re2')(dense2) dense3 = Dense(256, kernel_initializer=initializers.glorot_normal(), bias_initializer=initializers.glorot_normal(), name='critic_d3')(relu2) relu3 = LeakyReLU(alpha=relu_neg_slope, name='critic_re3')(dense3) dense4 = Dense(128, kernel_initializer=initializers.glorot_normal(), bias_initializer=initializers.glorot_normal(), name='critic_d4')(relu3) relu4 = LeakyReLU(alpha=relu_neg_slope, name='critic_re4')(dense4) critic_out = Dense(1, kernel_initializer=initializers.glorot_normal(), bias_initializer=initializers.glorot_normal())(relu4) critic = Model(inputs=[critic_input_state, critic_input_action], outputs=critic_out)