Skip to content

Instantly share code, notes, and snippets.

View pedrohbtp's full-sized avatar
😁

Pedro Borges pedrohbtp

😁
View GitHub Profile
@pedrohbtp
pedrohbtp / basic_flask.py
Last active November 14, 2018 13:28
Basic Flask Example
from flask import Flask
import flask
import json
from flask import Response
app = Flask(__name__)
@app.route('/test',methods=['GET'])
def test():
'''
GET: Receives the request in /test route and returns a response containing {"response": "test"}
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# Defining 3 linear layers but NOT the way they should be connected
@pedrohbtp
pedrohbtp / pytorch_weight_update.py
Last active December 13, 2018 16:13
Pytroch training example
import torch.optim as optim
import torch.nn as nn
# instantiate your network that should be defined by you
net = Net()
# create your optimizer
optimizer = optim.SGD(net.parameters(), lr=0.01)
# define your criterion for optimization
criterion = nn.MSELoss()
# dat_set comes from somewhere
for data in data_set:
import torch
import pandas as pd
from torch.utils.data import Dataset, DataLoader
class ExampleDataset(Dataset):
"""Example Dataset"""
def __init__(self, csv_file):
"""
csv_file (string): Path to the csv file containing data.
@pedrohbtp
pedrohbtp / custom_open_ai_gym_env.py
Created September 12, 2019 19:27
basic structure of an openai gym env
import gym
from gym import spaces
class MyEnv(gym.Env):
"""Custom Environment"""
metadata = {'render.modes': ['human']}
def __init__(self):
super(Snake, self).__init__()
# necessary internal env id
@pedrohbtp
pedrohbtp / knapsack_breadth_first_search
Created May 1, 2021 00:11
implementation of knapsack problem exploring the state space in a breadth first search manner and using memoization to reduce the exploration space
import heapq
def dijkstra(cost_matrix, destination_node):
open_list = []
closed_list = set()
number_of_nodes = len(cost_matrix)
# (cum_cost, (previous_node, current_node))
heapq.heappush(open_list, (0, (0, 0)))
while open_list:
current_node = heapq.heappop(open_list)
# if the current node not already expanded