Skip to content

Instantly share code, notes, and snippets.

int main(int argc, const char * argv[]) {
for (unsigned i = 0; i != 10000; ++i) {
vector<float> layer_1 = sigmoid(dot(X, W0, 4, 2, 4 ) );
vector<float> layer_2 = sigmoid(dot(layer_1, W1, 4, 4, 1 ) );
vector<float> layer_2_delta = (y - layer_2) * sigmoid_d(layer_2);
vector<float> layer_1_delta = dot(layer_2_delta, transpose( &W1[0], 4, 1 ), 4, 1, 4) * sigmoid_d(layer_1);
W1 = W1 + dot(transpose( &layer_1[0], 4, 4 ), layer_2_delta, 4, 4, 1);
W0 = W0 + dot(transpose( &X[0], 4, 2 ), layer_1_delta, 2, 4, 4);
};
return 0;
// XOR Dataset
vector<float> X {
0.0, 0.0,
0.0, 1.0,
1.0, 0.0,
1.0, 1.0};
// Quasi random numbers
vector<float> W0 {
-0.07555777, -0.04661271, -0.0982434, 0.01800294,
0.0314169
0.97234
0.972556
0.0295431
0.500027
0.500029
0.500029
0.50003
//
// onehiddenlayerperceptron.cu
// onehiddenlayerperceptron
//
// Created by Sergei Bugrov on 8/21/17.
// Copyright © 2017 Sergei Bugrov. All rights reserved.
//
#include <stdio.h>
@sbugrov
sbugrov / learn.cu
Last active October 27, 2018 12:21
__global__ void kFit( const float* X, const int X_w, const int X_h, const float* y, const int y_w, float* l1, const int l1_w, float* l_1_d, float* pred, float* pred_d, float* W0, float* W1, float* buffer) {
for (unsigned i = 0; i < 50; ++i) {
dSigmoid(dDot(X, W0, l1, X_h, X_w, l1_w), l1, X_h, l1_w);
dSigmoid(dDot(l1, W1, pred, X_h, l1_w, y_w), pred, X_h, y_w);
dMartixByMatrixElementwise(dMartixSubstractMatrix(y, pred, pred_d, X_h, y_w), dSigmoid_d(pred, buffer, X_h, y_w), pred_d, X_h, y_w );
dMartixByMatrixElementwise(dDot_m1_m2T(pred_d, W1, l_1_d, X_h, y_w, l1_w), dSigmoid_d(l1, buffer, X_h, l1_w), l_1_d, X_h, l1_w);
dDot_m1T_m2( l1, pred_d, W1, X_h, l1_w, y_w );
dDot_m1T_m2( X, l_1_d, W0, X_h, X_w, l1_w );
}
}
X W0 layer_1 W1 pred y
5.1 3.5 1.4 0.2 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.05 0
4.9 3.0 1.4 0.2 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.06 0
6.2 3.4 5.4 2.3 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.93 1
5.9 3.0 5.1 1.8 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.92 1
0.5
0.5
0.5
0.5
dSigmoid(dDot(X, W0, l1, X_h, X_w, l1_w), l1, X_h, l1_w);
__global__ void kDot(const float *m1, const float *m2, float *output, const int m1_rows , const int m1_columns, const int m2_columns ){
/* Computes the product of two matrices: m1 x m2.
Inputs:
m1: array, left matrix of size m1_rows x m1_columns
m2: array, right matrix of size m1_columns x m2_columns (the number of rows in the right matrix
must be equal to the number of the columns in the left one)
output: array, the results of the computation are to be stored here:
m1 * m2, product of two arrays m1 and m2, a matrix of size m1_rows x m2_columns
m1_rows: int, number of rows in the left matrix m1
m1_columns: int, number of columns in the left matrix m1
__global__ void kSigmoid(float const *input, float *output) {
/* Computes the value of the sigmoid function f(x) = 1/(1 + e^-x).
Inputs:
input: array
output: array, the results of the computation are to be stored here
*/
const int id = blockIdx.x * blockDim.x + threadIdx.x;
output[id] = 1.0 / (1.0 + std::exp(-input[id]));