Форум сайта python.su
Я написал модуль обучения простого персептрона.
def train(X, y, syn0, lens0, lens01, lr): errorreturn = 0 for i in range(0, lens0): a = 0 for j in range(0, lens01): a = a + syn0[i][j] * X[j] b = 1 / (1 + 2.718281828459045235360287471352662497757 ** -a) error = y[i] - b errorreturn = errorreturn + abs(error) delta = (error) * 1 / (1 + 2.718281828459045235360287471352662497757 ** -b) * lr for z in range(0, lens01): syn0[i][z] = syn0[i][z] + X[z] * delta return syn0, errorreturn
def train(X, y, syn0, lens0, lens01, lr): errorreturn = 0 for i in range(0, lens0): a = 0 for j in range(0, lens01): a = a + syn0[i][j] * X[j] b = 1 / (1 + 2.718281828459045235360287471352662497757 ** -a) error = y[i] - b errorreturn = errorreturn + abs(error) delta = (error) * 1 / (1 + 2.718281828459045235360287471352662497757 ** -b) * lr for z in range(0, lens01): syn0[i][z] = syn0[i][z] + X[z] * delta return syn0, errorreturn def predict(syn0, X, lens0, lens01): ret = [] for i in range(0, lens0): a = 0 for j in range(0, lens01): a = a + syn0[i][j] * X[j] b = 1 / (1 + 2.718281828459045235360287471352662497757 ** -a) ret.append(b) return ret import random def create(x, y): syn0 = [] for z in range(0, x): h = [] for i in range(0, y): h.append(random.uniform(-0.1, 0.1)) syn0.append(h) return syn0 syn = create(2, 4) X = [[0.1, 0.2, 0.3, 0.4],[0.2, 0.3, 0.4, 0.5],[0.4, 0.5, 0.6, 0.7],[0.5,0.6,0.7,0.8]] y = [[0.5, 0.6],[0.6, 0.7],[0.8, 0.9],[0.9, 1]] def trainmass2(X, y, syn, lens0, lens01, lr, iter): for i in range(iter): for j in range(lens01): syn, er = train(X[j], y[j], syn, lens0, lens01, lr) #print(er) return syn, er trainmass2(X, y, syn, 2, 4, 1, 50000) h = predict(syn, X[0], 2, 4) h2 = predict(syn, [0.5, 0.6, 0.7, 0.8], 2, 4) print(h) print(h2)
#include <math.h> float train(int lens0, int lens01, float* y, float x[lens01], float syn0[lens0][lens01], float lr, float a, int i, int j, int z) { float b; float error; float errorreturn; float delta; for ( int i = 0; i < lens0; i++ ) a = 0; for ( int j = 0; i < lens01; j++ ) a = a + syn0[i][j] * x[j]; b = 1/(1+(exp((double) - a))); error = y[i] - b; errorreturn = errorreturn + error; delta = error * 1/(1+(exp((double) - a))) * lr; for ( int z = 0; z < lens01; z++ ) syn0[i][z] = syn0[i][z] + x[z] * delta; return syn0[lens0][lens01]; }
import ctypes import random X = [[0, 0, 1],[0, 1, 1],[1, 0, 1],[1, 1, 1]] y = [0, 0.1, 1, 0] syn0= [] for i in range(0,4): syn0.append(random.uniform(-0.1,0.1)) syn0 = [syn0] print(syn0) fib = ctypes.CDLL('./myfib.so').train g = fib(1, 4, [y[i]], X[0], [syn0], 0.1, 0, 1, 4, 4)
Офлайн
Lainelirсоздать его при помощи ctypes ((ctypes.c_int*3)*5) заполнить в питоне и передать в сишную функцию.
Как отправить 2ух мерный массив в си функцию?
Офлайн
doza_andА как его заполнять?
Офлайн