Skip to content

PKG: Add package lnn #1719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ RUN(NAME str_to_list_cast LABELS cpython llvm c)

RUN(NAME test_package_01 LABELS cpython llvm)
RUN(NAME test_pkg_lpdraw LABELS cpython llvm wasm)
RUN(NAME test_pkg_lnn LABELS cpython llvm)

RUN(NAME generics_01 LABELS cpython llvm c)
RUN(NAME generics_02 LABELS cpython llvm c)
Expand Down
Empty file.
1 change: 1 addition & 0 deletions integration_tests/lnn/perceptron/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .perceptron_main import init_perceptron, train_dataset, test_perceptron, normalize_input_vectors, print_perceptron, Perceptron
127 changes: 127 additions & 0 deletions integration_tests/lnn/perceptron/perceptron_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
from lpython import dataclass, i32, f64
from sys import exit

@dataclass
class Perceptron:
no_of_inputs: i32
weights: list[f64]
learn_rate: f64
iterations_limit: i32
des_accuracy: f64
cur_accuracy: f64
epochs_cnt: i32

def normalize(value: f64, leftMin: f64, leftMax: f64, rightMin: f64, rightMax: f64) -> f64:
# Figure out how 'wide' each range is
leftSpan: f64 = leftMax - leftMin
rightSpan: f64 = rightMax - rightMin

# Convert the left range into a 0-1 range (float)
valueScaled: f64 = (value - leftMin) / leftSpan

# Convert the 0-1 range into a value in the right range.
return rightMin + (valueScaled * rightSpan)

def normalize_input_vectors(input_vectors: list[list[f64]]):
rows: i32 = len(input_vectors)
cols: i32 = len(input_vectors[0])

j: i32
for j in range(cols):
colMinVal: f64 = input_vectors[0][j]
colMaxVal: f64 = input_vectors[0][j]
i: i32
for i in range(rows):
if input_vectors[i][j] > colMaxVal:
colMaxVal = input_vectors[i][j]
if input_vectors[i][j] < colMinVal:
colMinVal = input_vectors[i][j]

for i in range(rows):
input_vectors[i][j] = normalize(input_vectors[i][j], colMinVal, colMaxVal, -1.0, 1.0)



def get_inp_vec_with_bias(a: list[f64]) -> list[f64]:
b: list[f64] = []
i: i32
for i in range(len(a)):
b.append(a[i])
b.append(1.0)
return b

def init_weights(size: i32) -> list[f64]:
weights: list[f64] = []
i: i32
for i in range(size):
weights.append(0.0)
weights.append(0.0) # append bias
return weights

def init_perceptron(p: Perceptron, n: i32, rate: f64, iterations_limit: i32, des_accuracy: f64):
if (n < 1 or n > 1000):
print("no_of_inputs must be between [1, 1000]")
exit(1)
p.no_of_inputs = n
p.weights = init_weights(n)
p.learn_rate = rate
p.iterations_limit = iterations_limit
p.des_accuracy = des_accuracy
p.cur_accuracy = 0.0
p.epochs_cnt = 0

def train_perceptron(p: Perceptron, input_vector: list[f64], actual_output: i32):
predicted_output: i32 = predict_perceptron(p, input_vector)
error: i32 = actual_output - predicted_output
i: i32
for i in range(len(input_vector)):
p.weights[i] += p.learn_rate * f64(error) * f64(input_vector[i])

def predict_perceptron(p: Perceptron, input_vector: list[f64]) -> i32:
weighted_sum: f64 = 0.0
i: i32 = 0
for i in range(len(input_vector)):
weighted_sum = weighted_sum + p.weights[i] * f64(input_vector[i])
return activation_function(weighted_sum)

def activation_function(value: f64) -> i32:
if value >= 0.0:
return 1
return -1

def train_epoch(p: Perceptron, input_vectors: list[list[f64]], outputs: list[i32]):
i: i32
for i in range(len(input_vectors)):
input_vector: list[f64] = get_inp_vec_with_bias(input_vectors[i])
if predict_perceptron(p, input_vector) != outputs[i]:
train_perceptron(p, input_vector, outputs[i])

def train_dataset(p: Perceptron, input_vectors: list[list[f64]], outputs: list[i32]):
p.cur_accuracy = 0.0
p.epochs_cnt = 0
while p.cur_accuracy < p.des_accuracy and p.epochs_cnt < p.iterations_limit:
p.epochs_cnt += 1
train_epoch(p, input_vectors, outputs)
p.cur_accuracy = test_perceptron(p, input_vectors, outputs)

def test_perceptron(p: Perceptron, input_vectors: list[list[f64]], outputs: list[i32]) -> f64:
correctly_classified_cnt: i32 = 0
i: i32
for i in range(len(input_vectors)):
input_vector: list[f64] = get_inp_vec_with_bias(input_vectors[i])
if predict_perceptron(p, input_vector) == outputs[i]:
correctly_classified_cnt += 1
return (correctly_classified_cnt / len(input_vectors)) * 100.0

def print_perceptron(p: Perceptron):
print("weights = [", end = "")
i: i32
for i in range(p.no_of_inputs):
print(p.weights[i], end = ", ")
print(p.weights[p.no_of_inputs], end = "(bias)]\n")
print("learn_rate = ", end = "")
print(p.learn_rate)
print("accuracy = ", end = "")
print(p.cur_accuracy)
print("epochs_cnt = ", end = "")
print(p.epochs_cnt)
3 changes: 2 additions & 1 deletion integration_tests/lpdraw/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
W = TypeVar("W")

def Pixel(H: i32, W: i32, Screen: i32[H, W], x: i32, y: i32) -> None:
Screen[y, x] = 255
if x >= 0 and y >= 0 and x < W and y < H:
Screen[i32(int(H - 1 - y)), i32(int(x))] = 255

def Clear(H: i32, W: i32, Screen: i32[H, W]):
i: i32
Expand Down
89 changes: 89 additions & 0 deletions integration_tests/test_pkg_lnn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from lnn.perceptron import init_perceptron, print_perceptron, normalize_input_vectors, Perceptron, train_dataset
from lpdraw import Line, Circle, Display, Clear
from lpython import i32, f64, Const
from numpy import empty, int32


def compute_decision_boundary(p: Perceptron, x: f64) -> f64:
bias: f64 = p.weights[-1]
slope: f64 = (-p.weights[0] / p.weights[1])
intercept: f64 = (-bias / p.weights[1])
return slope * x + intercept

def plot_graph(p: Perceptron, input_vectors: list[list[f64]], outputs: list[i32]):
Width: Const[i32] = 500 # x-axis limits [0, 499]
Height: Const[i32] = 500 # y-axis limits [0, 499]
Screen: i32[Height, Width] = empty((Height, Width), dtype=int32)
Clear(Height, Width, Screen)

x1: f64 = 2.0
y1: f64 = compute_decision_boundary(p, x1)
x2: f64 = -2.0
y2: f64 = compute_decision_boundary(p, x2)

# center the graph using the following offset
scale_offset: f64 = Width / 4
shift_offset: f64 = Width / 2
x1 *= scale_offset
y1 *= scale_offset
x2 *= scale_offset
y2 *= scale_offset

# print (x1, y1, x2, y2)
Line(Height, Width, Screen, i32(x1 + shift_offset), i32(y1 + shift_offset), i32(x2 + shift_offset), i32(y2 + shift_offset))

i: i32
point_size: i32 = 5
for i in range(len(input_vectors)):
input_vectors[i][0] *= scale_offset
input_vectors[i][1] *= scale_offset
input_vectors[i][0] += shift_offset
input_vectors[i][1] += shift_offset
if outputs[i] == 1:
x: i32 = i32(input_vectors[i][0])
y: i32 = i32(input_vectors[i][1])
Line(Height, Width, Screen, x - point_size, y, x + point_size, y)
Line(Height, Width, Screen, x, y - point_size, x, y + point_size)
else:
Circle(Height, Width, Screen, i32(input_vectors[i][0]), i32(input_vectors[i][1]), f64(point_size))

Display(Height, Width, Screen)

def main0():
p: Perceptron = Perceptron(0, [0.0], 0.0, 0, 0.0, 0.0, 0)
init_perceptron(p, 2, 0.05, 10000, 90.0)
print_perceptron(p)
print("=================================")

input_vectors: list[list[f64]] = [[-1.0, -1.0], [-1.0, 1.0], [1.0, -1.0], [1.0, 1.0]]
outputs: list[i32] = [1, 1, 1, -1]

normalize_input_vectors(input_vectors)
train_dataset(p, input_vectors, outputs)
print_perceptron(p)

assert p.cur_accuracy > 50.0
assert p.epochs_cnt > 1

plot_graph(p, input_vectors, outputs)

def main1():
p: Perceptron = Perceptron(0, [0.0], 0.0, 0, 0.0, 0.0, 0)
init_perceptron(p, 2, 0.05, 10000, 90.0)
print_perceptron(p)
print("=================================")

input_vectors: list[list[f64]] = [[-1.0, -1.0], [-1.0, 1.0], [1.0, -1.0], [1.0, 1.0], [1.5, 1.0]]
outputs: list[i32] = [1, 1, -1, 1, -1]

normalize_input_vectors(input_vectors)
train_dataset(p, input_vectors, outputs)
print_perceptron(p)

assert p.cur_accuracy > 50.0
assert p.epochs_cnt > 1

plot_graph(p, input_vectors, outputs)

main0()
main1()
95 changes: 51 additions & 44 deletions src/libasr/pass/pass_array_by_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,68 +359,75 @@ class EditProcedureCallsVisitor : public ASR::ASRPassBaseWalkVisitor<EditProcedu
al(al_), v(v_) {}

template <typename T>
void visit_Call(const T& x) {
ASR::symbol_t* subrout_sym = x.m_name;
bool is_external = ASR::is_a<ASR::ExternalSymbol_t>(*subrout_sym);
subrout_sym = ASRUtils::symbol_get_past_external(subrout_sym);
if( v.proc2newproc.find(subrout_sym) == v.proc2newproc.end() ) {
bool args_updated = false;
Vec<ASR::call_arg_t> new_args;
new_args.reserve(al, x.n_args);
for ( size_t i = 0; i < x.n_args; i++ ) {
ASR::call_arg_t arg = x.m_args[i];
ASR::expr_t* expr = arg.m_value;
bool use_original_arg = true;
if (expr) {
if (ASR::is_a<ASR::Var_t>(*expr)) {
ASR::Var_t* var = ASR::down_cast<ASR::Var_t>(expr);
ASR::symbol_t* sym = var->m_v;
if ( v.proc2newproc.find(sym) != v.proc2newproc.end() ) {
ASR::symbol_t* new_var_sym = v.proc2newproc[sym].first;
ASR::expr_t* new_var = ASRUtils::EXPR(ASR::make_Var_t(al, var->base.base.loc, new_var_sym));
ASR::call_arg_t new_arg;
new_arg.m_value = new_var;
new_arg.loc = arg.loc;
new_args.push_back(al, new_arg);
args_updated = true;
use_original_arg = false;
void check_and_update_args_for_pass_arr_by_data_passed_as_callback(const T& x) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this for #1721?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a refactoring of code into a function. I named the function as check_and_update_args_for_pass_arr_by_data_passed_as_callback based on the operation that I thought the code was performing. I think it improves the code readibility. I think it is not towards any specific issue.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just renamed it to update_args_for_pass_arr_by_data_funcs_passed_as_callback().

bool args_updated = false;
Vec<ASR::call_arg_t> new_args;
new_args.reserve(al, x.n_args);
for ( size_t i = 0; i < x.n_args; i++ ) {
ASR::call_arg_t arg = x.m_args[i];
ASR::expr_t* expr = arg.m_value;
if (expr) {
if (ASR::is_a<ASR::Var_t>(*expr)) {
ASR::Var_t* var = ASR::down_cast<ASR::Var_t>(expr);
ASR::symbol_t* sym = var->m_v;
if ( v.proc2newproc.find(sym) != v.proc2newproc.end() ) {
ASR::symbol_t* new_var_sym = v.proc2newproc[sym].first;
ASR::expr_t* new_var = ASRUtils::EXPR(ASR::make_Var_t(al, var->base.base.loc, new_var_sym));
{
// update exisiting arg
arg.m_value = new_var;
arg.loc = arg.loc;
}
args_updated = true;
}
}
if( use_original_arg ) {
new_args.push_back(al, arg);
}
}
if (args_updated) {
T&xx = const_cast<T&>(x);
xx.m_args = new_args.p;
xx.n_args = new_args.size();
}
return ;
new_args.push_back(al, arg);
}
if (args_updated) {
T&xx = const_cast<T&>(x);
xx.m_args = new_args.p;
xx.n_args = new_args.size();
}
}

ASR::symbol_t* new_func_sym = v.proc2newproc[subrout_sym].first;
std::vector<size_t>& indices = v.proc2newproc[subrout_sym].second;

Vec<ASR::call_arg_t> construct_new_args(size_t n_args, ASR::call_arg_t* orig_args, std::vector<size_t>& indices) {
Vec<ASR::call_arg_t> new_args;
new_args.reserve(al, x.n_args);
for( size_t i = 0; i < x.n_args; i++ ) {
new_args.push_back(al, x.m_args[i]);
if( std::find(indices.begin(), indices.end(), i) == indices.end() ||
x.m_args[i].m_value == nullptr ) {
continue ;
new_args.reserve(al, n_args);
for( size_t i = 0; i < n_args; i++ ) {
new_args.push_back(al, orig_args[i]);
if (orig_args[i].m_value == nullptr ||
std::find(indices.begin(), indices.end(), i) == indices.end()) {
continue;
}

Vec<ASR::expr_t*> dim_vars;
dim_vars.reserve(al, 2);
ASRUtils::get_dimensions(x.m_args[i].m_value, dim_vars, al);
ASRUtils::get_dimensions(orig_args[i].m_value, dim_vars, al);
for( size_t j = 0; j < dim_vars.size(); j++ ) {
ASR::call_arg_t dim_var;
dim_var.loc = dim_vars[j]->base.loc;
dim_var.m_value = dim_vars[j];
new_args.push_back(al, dim_var);
}
}
return new_args;
}

template <typename T>
void visit_Call(const T& x) {
ASR::symbol_t* subrout_sym = x.m_name;
bool is_external = ASR::is_a<ASR::ExternalSymbol_t>(*subrout_sym);
subrout_sym = ASRUtils::symbol_get_past_external(subrout_sym);
if( v.proc2newproc.find(subrout_sym) == v.proc2newproc.end() ) {
check_and_update_args_for_pass_arr_by_data_passed_as_callback(x);
return;
}

ASR::symbol_t* new_func_sym = v.proc2newproc[subrout_sym].first;
std::vector<size_t>& indices = v.proc2newproc[subrout_sym].second;

Vec<ASR::call_arg_t> new_args = construct_new_args(x.n_args, x.m_args, indices);

{
ASR::Function_t* new_func_ = ASR::down_cast<ASR::Function_t>(new_func_sym);
Expand Down
Loading