Skip to content

Instantly share code, notes, and snippets.

View llSourcell's full-sized avatar

Siraj Raval llSourcell

View GitHub Profile
# Code to read csv file into colaboratory:
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [16, 10]
import os
import pickle
import re
from flask import Flask, request, jsonify
# Unpickle the trained classifier and write preprocessor method used
def tokenizer(text):
return text.split(' ')
#visualization
import matplotlib.pyplot as plt
#matrix math
import numpy as np
#machine learning
from sklearn import datasets, linear_model
#peformance measurement
from sklearn.metrics import mean_squared_error, r2_score
# Load the diabetes dataset
//after npm or yarn install
import * as tf from '@tensorflow/tfjs';
//loading pretrained model
import * as loader from './loader';
//sets up basic dom elements
import * as ui from './ui';
//Load the pretrained models , versioning information, timestamps,
const HOSTED_URLS = {
model:
ARITHMETIC_FUNCTIONS = {
'add': lambda x, y: x + y,
'sub': lambda x, y: x - y,
'mul': lambda x, y: x * y,
'div': lambda x, y: x / y,
'squared': lambda x, y: torch.pow(x, 2),
'root': lambda x, y: torch.sqrt(x),
}
for fn_str, fn in ARITHMETIC_FUNCTIONS.items():
class NeuralAccumulatorCell(nn.Module):
#PyTorch Code for the NAC!
def __init__(self, in_dim, out_dim):
super().__init__()
self.in_dim = in_dim
self.out_dim = out_dim
self.W_hat = Parameter(torch.Tensor(out_dim, in_dim))
self.M_hat = Parameter(torch.Tensor(out_dim, in_dim))
self.W = Parameter(F.tanh(self.W_hat) * F.sigmoid(self.M_hat))
import math
import torch
class NeuralArithmeticLogicUnitCell(nn.Module):
#NALU code in PyTorch!
def __init__(self, in_dim, out_dim):
super().__init__()
self.in_dim = in_dim
self.out_dim = out_dim
self.eps = 1e-10
def training_loop(self):
#remember our gradients!
gradients = np.vstack(self.gradients)
rewards = np.vstack(self.rewards)
rewards = self.discount_rewards(rewards)
rewards = rewards / np.std(rewards - np.mean(rewards))
gradients *= rewards
X = np.squeeze(np.vstack([self.states]))
Y = self.probs + self.learning_rate * np.squeeze(np.vstack([gradients]))
#update our model after a full-episode, did we win or lose?
from games.tictactoe import TicTacToeGame
#Returns -1 for loss, +1 for win, 0 for draw
def value(game):
if game.over():
return -game.score()
state_values = []
for move in game.valid_moves():
game.make_move(move)
import random
import numpy as np
from games.games import AbstractGame
def playout_value(game):
if game.over():
return -game.score()
move = random.choice(game.valid_moves())
game.make_move(move)