Skip to content

Instantly share code, notes, and snippets.

View maxpumperla's full-sized avatar
:octocat:
living here

Max Pumperla maxpumperla

:octocat:
living here
View GitHub Profile
@maxpumperla
maxpumperla / load_torch.py
Created September 9, 2022 07:28
Storing and loading Torch models from checkpoint
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
# -*- coding: utf-8 -*-
"""
Train an Auxiliary Classifier Generative Adversarial Network (ACGAN) on the
MNIST dataset. See https://arxiv.org/abs/1610.09585 for more details.
You should start to see reasonable images after ~5 epochs, and good images
by ~15 epochs. You should use a GPU, as the convolution-heavy operations are
very slow on the CPU. Prefer the TensorFlow backend if you plan on iterating,
as the compilation time can be a blocker using Theano.
Timings:
Hardware | Backend | Time / Epoch
@maxpumperla
maxpumperla / how_to_samediff.md
Created March 13, 2018 14:01
same same, but SameDiff

How to add new operations to SameDiff

A quick SameDiff overview

To get started with SameDiff, familiarize yourself with the autodiff module of the ND4J API located here on GitHub.

For better or worse, SameDiff code is organized in just a few key places. For basic usage and testing of SameDiff the following modules are key. We'll discuss some of them in more detail in just a bit.

  • functions: This module has the basic building blocks to build SameDiff variables and graphs.
  • execution: has everything related to SameDiff graph execution.
hyperopt/pyll/tests/test_base.py ....................... [ 11%]
hyperopt/pyll/tests/test_stochastic.py ..... [ 14%]
hyperopt/tests/test_anneal.py ................ [ 22%]
hyperopt/tests/test_base.py ............ [ 28%]
hyperopt/tests/test_criteria.py .... [ 30%]
hyperopt/tests/test_domains.py .......F. [ 35%]
hyperopt/tests/test_fmin.py ......... [ 40%]
hyperopt/tests/test_mongoexp.py ......... [ 44%]
hyperopt/tests/test_pchoice.py ....F.... [ 49%]
hyperopt/tests/test_pyll_utils.py .. [ 50%]
============================================= FAILURES ==============================================
___________________________________ TestLogNormal.test_pdf_logpdf ___________________________________
self = <hyperopt.tests.test_rdists.TestLogNormal testMethod=test_pdf_logpdf>
def test_pdf_logpdf(self):
check_pdf_logpdf(lognorm_gen(0, 1), args=(),
> msg='base case')
E TypeError: check_pdf_logpdf() got an unexpected keyword argument 'args'
@maxpumperla
maxpumperla / dl4j_go_example.java
Created January 10, 2018 15:18
Running a DL4J convolutional neural network on Go data
package org.deeplearning4j.dlgo;
import org.deeplearning4j.eval.Evaluation;
import org.deeplearning4j.nn.api.OptimizationAlgorithm;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.Updater;
import org.deeplearning4j.nn.conf.inputs.InputType;
import org.deeplearning4j.nn.conf.layers.ConvolutionLayer;
import org.deeplearning4j.nn.conf.layers.DenseLayer;
# This will start a Python web server on 127.0.0.1:5000. Might take a while to start up.
# Navigate to
# - 127.0.0.1:5000/static/play_mcts_55.html for a 5x5 example (trained with Monte Carlo tree search)
# - http://127.0.0.1:5000/static/play_pg_99.html for a 9x9 example (trained with policy gradients)
# http://127.0.0.1:5000/static/play_pg_99.html for a 19x19 example (trained with a CNN)
docker pull maxpumperla/dlgo
docker run maxpumperla/dlgo
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/bin/java -ea -Ddtype=double -Didea.test.cyclic.buffer.size=1048576 "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=56409:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit-rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit5-rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Jav
o.n.l.f.Nd4jBackend - Loaded [CpuBackend] backend
o.n.n.NativeOpsHolder - Number of threads used for NativeOps: 4
Unable to tune runtime. Please set OMP_NUM_THREADS manually.
o.n.n.Nd4jBlas - Number of threads used for BLAS: 4
o.n.l.a.o.e.DefaultOpExecutioner - Backend used: [CPU]; OS: [Mac OS X]
o.n.l.a.o.e.DefaultOpExecutioner - Cores: [8]; Memory: [3.6GB];
o.n.l.a.o.e.DefaultOpExecutioner - Blas vendor: [UNKNOWN]
o.d.n.m.k.KerasSequentialModel - Model cannot be trained: output layer activation_4 is not an IOutputLayer (no loss function specified)
o.d.n.m.MultiLayerNetwork - Starting MultiLayerNetwork with WorkspaceModes set to [training: NONE; inference: SEPARATE]
File completeModelFile = new ClassPathResource("complete.h5").getFile();
String completeModelFileName = completeModelFile.getAbsolutePath();
MultiLayerNetwork network = KerasModelImport.importKerasSequentialModelAndWeights(completeModelFileName);
NativeImageLoader loader = new NativeImageLoader(height, width);
File imageFile = new ClassPathResource("1.jpg").getFile();
INDArray image = loader.asMatrix(imageFile);
image.divi(255); // <== how about that? :)
INDArray output = network.output(image);