text stringlengths 0 2.87M |
|---|
Note that orig is the origin of the ray, dir is the direction of the ray, vert0, vert1, and vert2 are the vertices of the triangle, t is the distance from the ray origin to the intersection point, u and v are barycentric coordinates that define the intersection point within the triangle, EPSILON is a small constant use... |
1- "Bohemian Rhapsody" de Queen |
2- "Stairway to Heaven" de Led Zeppelin |
3- "Sweet Child o' Mine" de Guns N' Roses |
4- "Smells Like Teen Spirit" de Nirvana |
5- "Imagine" de John Lennon |
6- "Comfortably Numb" de Pink Floyd |
7- "Billie Jean" de Michael Jackson |
8- "Purple Haze" de Jimi Hendrix |
9- "The Wind Cries Mary" de Jimi Hendrix |
10- "Back in Black" de AC/DC. |
Esta lista es solo un ejemplo, sin embargo, la idea del "mejor rock" es subjetiva, por lo que otras personas pueden tener listas diferentes.<kinrel><user>Escribe el código de un perceptrón multicapa, sin utilizar librerías, en Python.<user><kinrel>El código siguiente implementa un perceptrón multicapa sin utilizar lib... |
Primero, declaramos la clase NeuralNetworkPrimero, declaramos la clase NeuralNetwork: |
import numpy as np |
def sigmoid(x): |
return 1.0/(1.0 + np.exp(-x)) |
def sigmoid_derivada(x): |
return sigmoid(x)*(1.0-sigmoid(x)) |
def tanh(x): |
return np.tanh(x) |
def tanh_derivada(x): |
return 1.0 - x**2 |
class NeuralNetwork: |
def __init__(self, layers, activation='tanh'): |
if activation == 'sigmoid': |
self.activation = sigmoid |
self.activation_prime = sigmoid_derivada |
elif activation == 'tanh': |
self.activation = tanh |
self.activation_prime = tanh_derivada |
# inicializo los pesos |
self.weights = [] |
self.deltas = [] |
# capas = [2,3,2] |
# rando de pesos varia entre (-1,1) |
# asigno valores aleatorios a capa de entrada y capa oculta |
for i in range(1, len(layers) - 1): |
r = 2*np.random.random((layers[i-1] + 1, layers[i] + 1)) -1 |
self.weights.append(r) |
# asigno aleatorios a capa de salida |
r = 2*np.random.random( (layers[i] + 1, layers[i+1])) - 1 |
self.weights.append(r) |
def fit(self, X, y, learning_rate=0.2, epochs=100000): |
# Agrego columna de unos a las entradas X |
# Con esto agregamos la unidad de Bias a la capa de entrada |
ones = np.atleast_2d(np.ones(X.shape[0])) |
X = np.concatenate((ones.T, X), axis=1) |
for k in range(epochs): |
i = np.random.randint(X.shape[0]) |
a = [X[i]] |
for l in range(len(self.weights)): |
dot_value = np.dot(a[l], self.weights[l]) |
activation = self.activation(dot_value) |
a.append(activation) |
# Calculo la diferencia en la capa de salida y el valor obtenido |
error = y[i] - a[-1] |
deltas = [error * self.activation_prime(a[-1])] |
# Empezamos en el segundo layer hasta el ultimo |
# (Una capa anterior a la de salida) |
for l in range(len(a) - 2, 0, -1): |
deltas.append(deltas[-1].dot(self.weights[l].T)*self.activation_prime(a[l])) |
self.deltas.append(deltas) |
# invertir |
# [level3(output)->level2(hidden)] => [level2(hidden)->level3(output)] |
deltas.reverse() |
# backpropagation |
# 1. Multiplcar los delta de salida con las activaciones de entrada |
# para obtener el gradiente del peso. |
# 2. actualizo el peso restandole un porcentaje del gradiente |
for i in range(len(self.weights)): |
layer = np.atleast_2d(a[i]) |
delta = np.atleast_2d(deltas[i]) |
self.weights[i] += learning_rate * layer.T.dot(delta) |
if k % 10000 == 0: print('epochs:', k) |
def predict(self, x): |
ones = np.atleast_2d(np.ones(x.shape[0])) |
a = np.concatenate((np.ones(1).T, np.array(x)), axis=0) |
for l in range(0, len(self.weights)): |
a = self.activation(np.dot(a, self.weights[l])) |
return a |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.