Skip to content

Instantly share code, notes, and snippets.

@vinimonteiro
vinimonteiro / export_csv_line.py
Created January 12, 2022 20:01
export_csv_line
df.to_csv('basketball_training.csv')
@vinimonteiro
vinimonteiro / export_csv.py
Created January 12, 2022 19:59
export_csv
import pandas as pd
data = [
["01/03/2021", 30, 15, 45, 30],
["03/03/2021", 41, 10, 51, 40],
["05/03/2021", 50, 23, 73, 63],
["07/03/2021", 70, 30, 100, 80]
]
df = pd.DataFrame(data, columns = ['TRAINING_DAY', 'SHOTS_MADE', 'SHOTS_MISSED', 'TOTAL', 'TRAINING_DURATION (MINs)'])
@vinimonteiro
vinimonteiro / client.py
Created December 23, 2021 21:02
Client calls MNIST service
import torch
import torch.nn.functional as F
import torchvision
from torchvision import transforms, datasets
import matplotlib.pyplot as plt
import torch.nn as nn
import sys
import json
import requests
@vinimonteiro
vinimonteiro / service.py
Created December 23, 2021 20:54
MNIST model as a a service
import torch
import numpy as np
from neural_network import NeuralNetwork
from flask import Flask, request
import json
model = None
app = Flask(__name__)
def load():
@vinimonteiro
vinimonteiro / summarizer_pipelineapi_1.py
Created December 19, 2021 07:54
summarizer_pipelineapi_1
from transformers import pipeline
summarization = pipeline("summarization")
original_text = """
The researcher Alvaro Pascual-Leone experimented with measuring exactly that. He wanted to know the speed at which the brain applies such changes.
He blindfolded sighted participants for five days and kept training them at braille. Day after day, the participants would show improvement.
At the end of the five days, their brains were measured in the scanner, and it showed that their occipital cortex (vision) was activated when they were touching objects!
After the blindfold was removed, the brain would come back to its previous state within a day. It was remarkable to see how quickly neural reorganization can happen.
But wait! That’s not all.
In another experiment, participants would have their brains scanned at the same time as being blindfolded. They would perform various touching tasks while having the brain mapped out. It was noticed that brain reorganization would happen as rapidly as in 40 to 60 minutes!
@vinimonteiro
vinimonteiro / summarizer_pipelineapi.py
Created December 18, 2021 20:59
summarizer_pipelineapi
from transformers import pipeline
summarization = pipeline("summarization")
original_text = """
In another experiment, participants would have their brains scanned at the same time as being blindfolded. They would perform various touching tasks while having the brain mapped out. It was noticed that brain reorganization would happen as rapidly as in 40 to 60 minutes!
Considering how quickly the brain reorganizes, it’s suggested that dreams are a defence mechanism. When we go to sleep, our vision cortex finds itself at a disadvantage. If you think about it, it’s the only sense lost — we still hear, taste, feel and smell. It’s unfair.
The brain, to protect the visual cortex from being taken over, creates images and videos internally (the dreams), and we see with our eyes shut. This way, vision is safe.
"""
summary = summarization(original_text)[0]['summary_text']
print("Summary:", summary)
@vinimonteiro
vinimonteiro / nn_pytorch_playing.py
Created December 17, 2021 15:02
nn_pytorch_playing
plt.imshow(X[1].view(28,28))
plt.show()
print(torch.argmax(model(X[1].view(-1, 784))[0]))
@vinimonteiro
vinimonteiro / nn_pytorch_validation.py
Created December 17, 2021 15:00
nn_pytorch_validation
orrect = 0
total = 0
with torch.no_grad():
for data in testset:
data_input, target = data
output = model(data_input.view(-1, 784))
for idx, i in enumerate(output):
if torch.argmax(i) == target[idx]:
correct += 1
total += 1
@vinimonteiro
vinimonteiro / nn_pytorch_training.py
Created December 17, 2021 14:58
nn_pytorch_training
optimizer = optim.Adam(model.parameters(), lr=0.001)
EPOCHS = 3
for epoch in range(EPOCHS):
for data in trainset:
X, y = data
model.zero_grad()
output = model(X.view(-1, 28 * 28))
loss = F.nll_loss(output, y)
loss.backward()
optimizer.step()
@vinimonteiro
vinimonteiro / nn_pytorch_neuralnet.py
Created December 17, 2021 14:57
nn_pytorch_neuralnet
class NeuralNetwork(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(784, 86)
self.fc2 = nn.Linear(86, 86)
self.fc3 = nn.Linear(86, 86)
self.fc4 = nn.Linear(86, 10)
def forward(self, x):
x = F.relu(self.fc1(x))