Theano - Alto Desempenho em Machine Learning

64
Theano Alto desempenho em Machine Learning Felipe Martins 11 de Abril de 2015

Transcript of Theano - Alto Desempenho em Machine Learning

Page 1: Theano - Alto Desempenho em Machine Learning

TheanoAlto desempenho em Machine Learning

Felipe Martins11 de Abril de 2015

Page 2: Theano - Alto Desempenho em Machine Learning

Quem?Universidade Federal do Ceará- Bacharelado em Computação;

LSBD- Líder técnico em projetos usando C e C++;

Page 3: Theano - Alto Desempenho em Machine Learning

Quem?Universidade Federal do Ceará- Bacharelado em Computação;

LSBD- Líder técnico em projetos usando C e C++;

Por aí...@ffmmjj_martinshttps://github.com/ffmmjj

Page 4: Theano - Alto Desempenho em Machine Learning

Roteiro● Contexto;● O que é a Theano;● Exemplo;

Page 5: Theano - Alto Desempenho em Machine Learning

ContextoPython e Computação Científica

Page 6: Theano - Alto Desempenho em Machine Learning

ContextoPython e Computação Científica

Page 7: Theano - Alto Desempenho em Machine Learning

Contexto

Mas Python não é uma linguagem “lenta”!?!

Page 8: Theano - Alto Desempenho em Machine Learning

ContextoDepende :-)

Page 9: Theano - Alto Desempenho em Machine Learning

ContextoDepende :-)

Page 10: Theano - Alto Desempenho em Machine Learning

ContextoDepende :-)

PyCUDA

Page 11: Theano - Alto Desempenho em Machine Learning

ContextoMachine Learning

Page 12: Theano - Alto Desempenho em Machine Learning

ContextoMachine Learning

Page 13: Theano - Alto Desempenho em Machine Learning

ContextoMachine Learning

Não suporta execução em GPUs :(

Page 14: Theano - Alto Desempenho em Machine Learning

O que é TheanoBiblioteca de computação simbólica para Machine Learning;

Otimiza expressões matemáticas e compila-as para código nativo;

Permite execução em GPUs!

Page 15: Theano - Alto Desempenho em Machine Learning

O que é TheanoExemplo: multiplicar duas matrizes 1000 x 1000;

Page 16: Theano - Alto Desempenho em Machine Learning

O que é TheanoExemplo: multiplicar duas matrizes 1000 x 1000;

import theanoimport theano.tensor as T

Page 17: Theano - Alto Desempenho em Machine Learning

O que é TheanoExemplo: multiplicar duas matrizes 1000 x 1000;

import theanoimport theano.tensor as TX = T.dmatrix('X')Y = T.dmatrix('Y')

Page 18: Theano - Alto Desempenho em Machine Learning

O que é TheanoExemplo: multiplicar duas matrizes 1000 x 1000;

import theanoimport theano.tensor as TX = T.dmatrix('X')Y = T.dmatrix('Y')Z = X*Y

Page 19: Theano - Alto Desempenho em Machine Learning

O que é TheanoExemplo: multiplicar duas matrizes 1000 x 1000;

import theanoimport theano.tensor as TX = T.dmatrix('X')Y = T.dmatrix('Y')Z = X*Yf = theano.function([X, Y], Z)

Page 20: Theano - Alto Desempenho em Machine Learning

O que é TheanoExemplo: multiplicar duas matrizes 1000 x 1000;

import theano x = [[1, 2], [3, 4]]import theano.tensor as T y = [[5, 6], [7, 8]]X = T.dmatrix('X') z = f(x, y)Y = T.dmatrix('Y')Z = X*Yf = theano.function([X, Y], Z)

Page 21: Theano - Alto Desempenho em Machine Learning

O que é TheanoExistem 3 maneiras de ajustar as configurações da Theano(na seguinte ordem de precedência):

Módulo theano.configArquivo .theanorcVariável de ambiente THEANO_FLAGS

Page 22: Theano - Alto Desempenho em Machine Learning

O que é Theanotheano.config

import theanotheano.config.floatx = ‘float32’theano.config.device = ‘gpu0’theano.config.nvcc.fastmath = ‘True’

Page 23: Theano - Alto Desempenho em Machine Learning

O que é Theano~/.theanorc

Page 24: Theano - Alto Desempenho em Machine Learning

O que é TheanoTHEANO_FLAGS

THEANO_FLAGS='floatX=float32,device=gpu0,nvcc.fastmath=True'export THEANO_FLAGS python <myscript>.py

Page 25: Theano - Alto Desempenho em Machine Learning

O que é TheanoGradientes...

Page 26: Theano - Alto Desempenho em Machine Learning

O que é TheanoGradientes…

x = T.dscalar(‘x’)

y = x ** 2

fy = theano.function([x], y)

Page 27: Theano - Alto Desempenho em Machine Learning

O que é TheanoGradientes…

x = T.dscalar(‘x’)

y = x ** 2

fy = theano.function([x], y)

Dy = T.grad(y, x)

Page 28: Theano - Alto Desempenho em Machine Learning

O que é TheanoGradientes…

x = T.dscalar(‘x’)

y = x ** 2

fy = theano.function([x], y)

Dy = T.grad(y, x)

Df = theano.function([x], dy)

Page 29: Theano - Alto Desempenho em Machine Learning

Exemplo

Page 30: Theano - Alto Desempenho em Machine Learning

ExemploMachine Learning em 5 minutos :)

Page 31: Theano - Alto Desempenho em Machine Learning

ExemploMachine Learning em 5 minutos :)

Modelo: Representação dos dados reais;

Page 32: Theano - Alto Desempenho em Machine Learning

ExemploMachine Learning em 5 minutos :)

Modelo: Representação dos dados reais;

Custo: O quanto o modelo se aproxima dos dados reais;

Page 33: Theano - Alto Desempenho em Machine Learning

ExemploMachine Learning em 5 minutos :)

Modelo: Representação dos dados reais;

Custo: O quanto o modelo se aproxima dos dados reais;

Treinamento: Como minimizar o custo;

Page 34: Theano - Alto Desempenho em Machine Learning

ExemploModelo

R$

Page 35: Theano - Alto Desempenho em Machine Learning

Exemplo

R$

Modelo

Page 36: Theano - Alto Desempenho em Machine Learning

ExemploModelo

R$

Page 37: Theano - Alto Desempenho em Machine Learning

ExemploCusto

R$

Page 38: Theano - Alto Desempenho em Machine Learning

ExemploCusto

R$

Page 39: Theano - Alto Desempenho em Machine Learning

ExemploCusto

R$

Média aritmética dos erros

Page 40: Theano - Alto Desempenho em Machine Learning

ExemploCusto

R$

Média aritmética dos erros

Page 41: Theano - Alto Desempenho em Machine Learning

ExemploTreinamento

R$

Page 42: Theano - Alto Desempenho em Machine Learning

ExemploTreinamento

R$

Page 43: Theano - Alto Desempenho em Machine Learning

ExemploTreinamento

R$

Page 44: Theano - Alto Desempenho em Machine Learning

ExemploTreinamento

R$

Page 45: Theano - Alto Desempenho em Machine Learning

Exemplo

R$

Treinamento

Page 46: Theano - Alto Desempenho em Machine Learning

Exemplo

R$

Treinamento

Como?!?

Page 47: Theano - Alto Desempenho em Machine Learning

Exemplo

R$

Treinamento

Como?!?

Page 48: Theano - Alto Desempenho em Machine Learning

Exemplo

R$

Treinamento

Como?!?

Modificando parâmetros...

Page 49: Theano - Alto Desempenho em Machine Learning

ExemploTreinamento

Page 50: Theano - Alto Desempenho em Machine Learning

ExemploTreinamento

Page 51: Theano - Alto Desempenho em Machine Learning

ExemploTreinamento

Custo

Page 52: Theano - Alto Desempenho em Machine Learning

ExemploTreinamento

Custo

Page 53: Theano - Alto Desempenho em Machine Learning

ExemploTreinamento

Custo

Page 54: Theano - Alto Desempenho em Machine Learning

ExemploTreinamento

Custo

Page 55: Theano - Alto Desempenho em Machine Learning

ExemploTreinamento

Custo

Page 56: Theano - Alto Desempenho em Machine Learning

ExemploTreinamento

Custo

Page 57: Theano - Alto Desempenho em Machine Learning

ExemploTreinamento

Custo

:(

Page 58: Theano - Alto Desempenho em Machine Learning

ExemploDe volta ao Python!

Page 59: Theano - Alto Desempenho em Machine Learning

ExemploDe volta ao Python!

alpha = T.dvector('a') a = 0.5

x = T.dvector('x') for i in

range(50):

t = T.dvector('t')

a = a - 0.0001 * Dj(a, X, T)

y = x * alpha

custo = T.sum((y - t)**2)

custo_derivada = theano.grad(custo, alpha)

J = theano.function([alpha, x, t], custo) / T.size(t)[0]

Dj = theano.function([alpha, x, t], custo_derivada)

Page 60: Theano - Alto Desempenho em Machine Learning

Exemplo “real”Rede neural para classificar dígitos manuscritos

Page 61: Theano - Alto Desempenho em Machine Learning

Exemplo “real”Rede neural para classificar dígitos manuscritos

Page 62: Theano - Alto Desempenho em Machine Learning

Exemplo “real”Rede neural para classificar dígitos manuscritos

Page 63: Theano - Alto Desempenho em Machine Learning

ExemploRede neural para classificar dígitos manuscritos

Page 64: Theano - Alto Desempenho em Machine Learning

FIM!