Exemplo n.º 1
0
                history['reward'].append(reward)
                history['exploration'].append(ai_player.epsilon)
            else:
                reward = reward+0.05 if headed_towards_fruit else reward-0.05

            next_state = np.array([convert_to_numbers(s) for s in next_state])\
                .reshape(input_dims)
            ai_player.memorize(state, action, reward, next_state, done)
            state = next_state

        BOARD.wipe_game()
        # Retrain the snake using older states
        ai_player.replay(400)

        # Save every 1000 episodes
        if e%1000 == 0:
            ai_player.save_model('./model')
            print('saving the model...')
            print('saving the history to csv file...')
            with open('./hist.csv', 'w') as f:
                w = csv.DictWriter(f, history.keys())
                w.writeheader()
                w.writerow(history)


    return history

if __name__=='__main__':
    hist = train_snake(show_pygame=False, episodes=2000, epsilon_phase_size=0.66, num_last_frames=4)
    plot_history(hist)
    load_show_agent('./model', BOARD, 5)
Exemplo n.º 2
0
(train_data, test_data), info = tfds.load('imdb_reviews/subwords8k',
                                          split=(tfds.Split.TRAIN,
                                                 tfds.Split.TEST),
                                          with_info=True,
                                          as_supervised=True)

encoder = info.features['text'].encoder
train_batches = pad_sequences(train_data)
test_batches = pad_sequences(test_data)

embedding_dim = 16

model = Sequential([
    Embedding(encoder.vocab_size, embedding_dim),
    GlobalAveragePooling1D(),
    Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

model.fit(train_batches,
          epochs=10,
          validation_data=test_batches,
          validation_steps=20)

save(model)
saveEmbedding(model.layers[0], info.features['text'].encoder)
plot_history(model.history.history)
Exemplo n.º 3
0
        optimizer=optimizers.Adam(lr=0.05, amsgrad=True),
        loss=tf.losses.categorical_crossentropy,
        metrics=['acc', auc]
    )

    history = model.fit(
        train_gen,
        epochs=MAX_EPOCHS,
        validation_data=val_gen,
        verbose=2,
        shuffle=False,
        callbacks=[es_callback]
    )

    model.summary()
    plot_history(history, es_callback, f'Node Level GCN', cfg.STORAGE_BASE_THESIS_IMG + rf'\gcn_node.pdf')


# TEST MODEL ===========================================================================================================
    all_predictions = model.predict(all_gen)
    test_predictions = model.predict(test_gen)

    all_node_predictions = target_encoding.inverse_transform(all_predictions.squeeze())
    test_node_predictions = target_encoding.inverse_transform(test_predictions.squeeze())

    all_predictions_df = pd.DataFrame({"Predicted": all_node_predictions,
                                       "True": graph_labels,
                                       "Fraud IDs": fraud_ids}, index=graph_labels.index)
    all_predictions_df['is_correct'] = all_predictions_df['Predicted'] == all_predictions_df['True']
    all_predictions_df.to_csv(cfg.STORAGE_ROOT_PATH + rf'\results_all_gcn_node.csv', sep=';')