Added lr shader and removed old ml shader
This commit is contained in:
parent
61471339ce
commit
16c56f29e1
2 changed files with 54 additions and 84 deletions
54
shaders/glsl/logisticregression.comp
Normal file
54
shaders/glsl/logisticregression.comp
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#version 450
|
||||
|
||||
layout (constant_id = 0) const uint M = 0;
|
||||
|
||||
layout (local_size_x = 1) in;
|
||||
|
||||
layout(set = 0, binding = 0) buffer bxi { float xi[]; };
|
||||
layout(set = 0, binding = 1) buffer bxj { float xj[]; };
|
||||
layout(set = 0, binding = 2) buffer by { float y[]; };
|
||||
layout(set = 0, binding = 3) buffer bwin { float win[]; };
|
||||
layout(set = 0, binding = 4) buffer bwouti { float wouti[]; };
|
||||
layout(set = 0, binding = 5) buffer bwoutj { float woutj[]; };
|
||||
layout(set = 0, binding = 6) buffer bbin { float bin[]; };
|
||||
layout(set = 0, binding = 7) buffer bbout { float bout[]; };
|
||||
layout(set = 0, binding = 8) buffer blout { float lout[]; };
|
||||
|
||||
float m = float(M);
|
||||
|
||||
float sigmoid(float z) {
|
||||
return 1.0 / (1.0 + exp(-z));
|
||||
}
|
||||
|
||||
float inference(vec2 x, vec2 w, float b) {
|
||||
// Compute the linear mapping function
|
||||
float z = dot(w, x) + b;
|
||||
// Calculate the y-hat with sigmoid
|
||||
float yHat = sigmoid(z);
|
||||
return yHat;
|
||||
}
|
||||
|
||||
float calculateLoss(float yHat, float y) {
|
||||
return -(y * log(yHat) + (1.0 - y) * log(1.0 - yHat));
|
||||
}
|
||||
|
||||
void main() {
|
||||
uint idx = gl_GlobalInvocationID.x;
|
||||
|
||||
vec2 wCurr = vec2(win[0], win[1]);
|
||||
float bCurr = bin[0];
|
||||
|
||||
vec2 xCurr = vec2(xi[idx], xj[idx]);
|
||||
float yCurr = y[idx];
|
||||
|
||||
float yHat = inference(xCurr, wCurr, bCurr);
|
||||
|
||||
float dZ = yHat - yCurr;
|
||||
vec2 dW = (1. / m) * xCurr * dZ;
|
||||
float dB = (1. / m) * dZ;
|
||||
wouti[idx] = dW.x;
|
||||
woutj[idx] = dW.y;
|
||||
bout[idx] = dB;
|
||||
|
||||
lout[idx] = calculateLoss(yHat, yCurr);
|
||||
}
|
||||
|
|
@ -1,84 +0,0 @@
|
|||
#version 450
|
||||
|
||||
// This variable is set by the pipeline
|
||||
layout (constant_id = 0) const uint BUFFER_ELEMENTS = 32;
|
||||
|
||||
layout(binding = 0) buffer Pos {
|
||||
uint values[ ];
|
||||
};
|
||||
|
||||
layout (local_size_x = 4, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
float learningRate = 0.01;
|
||||
|
||||
uint fibonacci(uint n) {
|
||||
if(n <= 1){
|
||||
return n;
|
||||
}
|
||||
uint curr = 1;
|
||||
uint prev = 1;
|
||||
for(uint i = 2; i < n; ++i) {
|
||||
uint temp = curr;
|
||||
curr += prev;
|
||||
prev = temp;
|
||||
}
|
||||
return curr;
|
||||
}
|
||||
|
||||
float sigmoid(float z) {
|
||||
return 1.0 / (1.0 + exp(-z));
|
||||
}
|
||||
|
||||
float forwardProp(float x, float w, float b) {
|
||||
float z = dot(w, x) + b;
|
||||
float yHat = sigmoid(z);
|
||||
return yHat;
|
||||
}
|
||||
|
||||
void backwardProp(float x, float yHat, float y, float m, out float w, out float b) {
|
||||
float dZ = yHat - y;
|
||||
float dW = (1/m) * x * dZ;
|
||||
float dB = (1/m) * dZ;
|
||||
w -= (learningRate * dW);
|
||||
b -= (learningRate * dB);
|
||||
}
|
||||
|
||||
float calculateLoss(float yHat, float y) {
|
||||
return -(y * log(yHat) + (1.0 - y) * log(1.0 - yHat));
|
||||
}
|
||||
|
||||
shared uint sharedTotal[1];
|
||||
|
||||
void main()
|
||||
{
|
||||
uint index = gl_GlobalInvocationID.x;
|
||||
if (index >= BUFFER_ELEMENTS)
|
||||
return;
|
||||
|
||||
// float m = int(BUFFER_ELEMENTS);
|
||||
// float w = 0.001;
|
||||
// float b = 0.0;
|
||||
//
|
||||
// float x = values[index];
|
||||
//
|
||||
// float yHat = forwardProp(x, w, b);
|
||||
// float loss = calculateLoss(yHat, x);
|
||||
//
|
||||
// backwardProp(x, yHat, x, m, w, b);
|
||||
//
|
||||
// values[index] = fibonacci(values[index]);
|
||||
|
||||
sharedTotal[0] = 0;
|
||||
|
||||
barrier();
|
||||
memoryBarrierShared();
|
||||
|
||||
atomicAdd(sharedTotal[0], values[index]);
|
||||
|
||||
barrier();
|
||||
memoryBarrierShared();
|
||||
|
||||
values[index] = int(sharedTotal[0]);
|
||||
}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue