Skip to content

Instantly share code, notes, and snippets.

View dmesquita's full-sized avatar

Déborah Mesquita dmesquita

View GitHub Profile
# Test the Model
correct = 0
total = 0
total_test_data = len(newsgroups_test.target)
batch_x_test,batch_y_test = get_batch(newsgroups_test,0,total_test_data)
articles = Variable(torch.FloatTensor(batch_x_test))
labels = Variable(torch.LongTensor(batch_y_test))
outputs = net(articles)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
# Train the Model
for epoch in range(num_epochs):
total_batch = int(len(newsgroups_train.data)/batch_size)
for i in range(total_batch):
batch_x,batch_y = get_batch(newsgroups_train,i,batch_size)
articles = Variable(torch.FloatTensor(batch_x))
labels = Variable(torch.FloatTensor(batch_y))
# Forward + Backward + Optimize
optimizer.zero_grad() # zero the gradient buffer
import torch.nn as nn
loss = nn.CrossEntropyLoss()
input = Variable(torch.randn(2, 5), requires_grad=True)
print(">>> batch of size 2 and 5 classes")
print(input)
target = Variable(torch.LongTensor(2).random_(5))
print(">>> array of size ‘batch_size’ with the index of the maxium label for each item")
net = OurNet(input_size, hidden_size, num_classes)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
class OurNet(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(Net, self).__init__()
self.layer_1 = nn.Linear(n_inputs,hidden_size, bias=True)
self.relu = nn.ReLU()
import torch
x = torch.IntTensor([1,3,6])
y = torch.IntTensor([1,1,1])
result = x + y
print(result)
>>> 2
>>> 4
>>> 7
@dmesquita
dmesquita / tensorflow.py
Last active October 24, 2017 18:51
Big Picture Deep Learning with Pytorch
def multilayer_perceptron(input_tensor, weights, biases):
layer_1_multiplication = tf.matmul(input_tensor, weights['h1'])
layer_1_addition = tf.add(layer_1_multiplication, biases['b1'])
layer_1_activation = tf.nn.relu(layer_1_addition)
layer_2_multiplication = tf.matmul(layer_1_activation, weights['h2'])
layer_2_addition = tf.add(layer_2_multiplication, biases['b2'])
layer_2_activation = tf.nn.relu(layer_2_addition)
out_layer_multiplication = tf.matmul(layer_2_activation, weights['out'])
@dmesquita
dmesquita / server.py
Created October 9, 2017 23:59
Connect to WiFi and send POST requests with nodeMCU
from flask import Flask, url_for, request
import time
import datetime
app = Flask(__name__)
@app.route('/')
def api_root():
return 'Server running'
@dmesquita
dmesquita / categories_UKWA.json
Last active October 18, 2017 12:47
Comparing fCC R3 labels
{"name":"UKWA", "children": [{"size": 2992, "name": "Business, Economy & Industry"}, {"size": 2426, "name": "Science & Technology"}, {"size": 270, "name": "Social Problems and Welfare"}, {"size": 718, "name": "Sports and Recreation"}, {"size": 3019, "name": "Society & Culture"}, {"size": 2128, "name": "Education & Research"}, {"size": 23, "name": "Popular Science"}, {"size": 743, "name": "Digital Society"}, {"size": 38, "name": "Environment"}, {"size": 26, "name": "Publishing, Printing and Bookselling"}, {"size": 101, "name": "Crime, Criminology, Police and Prisons"}, {"size": 87, "name": "Literature"}, {"size": 81, "name": "Law and Legal System"}, {"size": 52, "name": "Libraries, Archives and Museums"}, {"size": 2170, "name": "Medicine & Health"}, {"size": 123, "name": "Politics, Political Theory and Political Systems"}, {"size": 843, "name": "Company Web Sites"}, {"size": 54, "name": "Computer Science, Information Technology and Web Technology"}, {"size": 378, "name": "Travel & Tourism"}, {"size": 5319, "na
@dmesquita
dmesquita / gantt_alike_chart.js
Created September 15, 2017 20:30
Gantt alike d3 reusable chart
function ganttAlikeChart(){
width = 800;
height = 600;
margin = {top: 20, right: 100, bottom: 20, left:100};
xScale = d3.scaleTime();
yScale = d3.scaleLinear();
colorScale = d3.scaleLinear();
xValue = d => d.date;
colorValue = d => d.status;
barHeight = 30;