This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(' ') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |