Skip to content

Instantly share code, notes, and snippets.

@nbertagnolli
nbertagnolli / mnist_dropout.ipynb
Last active January 7, 2022 23:55
A notebook describing how to implement dropout in pytorch from scratch. For the associated article https://medium.com/p/67f08a87ccff/edit.
View mnist_dropout.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@nbertagnolli
nbertagnolli / true_dropout.py
Last active January 7, 2022 23:56
A real implementation of dropout with proper normalization.
View true_dropout.py
class TrueDropout(torch.nn.Module):
def __init__(self, p: float=0.5):
super(TrueDropout, self).__init__()
self.p = p
if self.p < 0 or self.p > 1:
raise ValueError("p must be a probability")
def forward(self, x):
if self.training:
@nbertagnolli
nbertagnolli / mlp_mnist.py
Created January 1, 2022 21:14
A simple mlp mnist model using custom dropout class
View mlp_mnist.py
class MNISTModel(torch.nn.Module):
def __init__(self):
super(MNISTModel, self).__init__()
self.layer_1 = nn.Linear(28 * 28, 512)
self.layer_2 = nn.Linear(512, 512)
self.layer_3 = nn.Linear(512, 10)
self.dropout = Dropout(.5)
def forward(self, x):
@nbertagnolli
nbertagnolli / dropout_basic.py
Last active January 7, 2022 23:56
The most basic form of dropout
View dropout_basic.py
class Dropout(torch.nn.Module):
def __init__(self, p: float=0.5):
super(Dropout, self).__init__()
self.p = p
if self.p < 0 or self.p > 1:
raise ValueError("p must be a probability")
def forward(self, x):
if self.training:
View Dockerfile.ui
FROM python:3.8
RUN mkdir /ui
COPY requirements.txt /ui
COPY . /ui
WORKDIR /ui
RUN apt-get update && apt-get install -y build-essential libapr1-dev libssl-dev openssh-client
# Install all necessary libraries into a pyenv environment
View Dockerfile.api
FROM python:3.8
# Setup the api directory for your project code
RUN mkdir /api
COPY . /api
COPY requirements.txt /api
WORKDIR /api
# Install all necessary libraries into a pyenv environment
View ds-app-docker-compose.yml
version: '3.8'
services:
movie-app-api:
image: movie-app-api
ports:
- 8000:8000
volumes:
- ./api:/api
networks:
- bridge_network
View streamlit_app.py
import streamlit as st
import requests
import json
url = "http://localhost:8000/review/predict"
# Write out the header for the page
st.write("# Movie Review Analyzer")
# Accept user text input
View fast_api_post_example.py
from fastapi import FastAPI
from pydantic import BaseModel
# Initialize the FastAPI App
app = FastAPI()
# Create a Data Model so FastAPI can read the data from the request.
class Review(BaseModel):
text: str
@nbertagnolli
nbertagnolli / sklearn_lambda_handler.py
Last active June 13, 2021 23:22
A simple lambda handler to serve a pretrained sklearn model
View sklearn_lambda_handler.py
import boto3
import json
import os
import pickle
s3 = boto3.resource("s3")
BUCKET_NAME = "nic-sklearn-models"