From 3240e076ea12c549c624ae0409022cefba1f7b7f Mon Sep 17 00:00:00 2001 From: pl5g92irw <484114892@qq.com> Date: Tue, 5 Apr 2022 23:29:19 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Python=E6=8F=8F=E8=BF=B0=E7=9A=84=E7=A5=9E?= =?UTF-8?q?=E7=BB=8F=E7=BD=91=E7=BB=9C=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nn.py | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 nn.py diff --git a/nn.py b/nn.py new file mode 100644 index 0000000..0ef5eae --- /dev/null +++ b/nn.py @@ -0,0 +1,124 @@ +import numpy as np + + +# Create a neural net +def create_neural_net(layer_array, input_dims): + weights = [] + biases = [] + activations = [] + + for i in range(len(layer_array)): + node_num = layer_array[i][0] + weights_of_layer = [] + biases_of_layer = [] + if i == 0: + last_layer_node_number = input_dims + else: + last_layer_node_number = layer_array[i - 1][0] + + for n in range(0, node_num): + weights_of_node = [] + for l in range(0, last_layer_node_number): + weights_of_node.append(1) + weights_of_layer.append(weights_of_node) + biases_of_layer.append(0) + + weights.append(weights_of_layer) + biases.append(biases_of_layer) + activations.append(layer_array[i][1]) + return [weights, biases, activations] + + +# Activations +def sigmoid(x): + return 1 / (1 + np.exp(-x)) + + +def sigmoid_deriv(x): + return x * (1 - x) + + +def relu(x): + if x < 0: + return 0 + else: + return x + + +# prediction +def predict_ratio(data, neural_net): + weights = neural_net[0] + biases = neural_net[1] + activations = neural_net[2] + + layer_num = len(weights) + + for l in range(0, layer_num): + data = np.dot(weights[l], data) + for t in range(len(data)): + data[t] += biases[l][t] + if activations[l] == 'sigmoid': + data = sigmoid(data) + elif activations[l] == 'relu': + data = relu(data) + else: + # If not identified, do it with sigmoid + data = sigmoid(data) + print('activation function', activations[l], 'cannot be found. Sigmoid is used') + return data + + +def predict(data, neural_net): + data = predict_ratio(data, neural_net) + + class_num = len(data) + + highest_class = None + highest_class_probability = -1 + + for i in range(0, class_num): + if highest_class == None: + highest_class = i + highest_class_probability = data[i] + elif data[i] > highest_class_probability: + highest_class = i + highest_class_probability = data[i] + + return highest_class, highest_class_probability + + +# Training +def train_network(X, Y, labels, neural_net, epochs=1000): + for epoch in range(0, epochs): + for d in range(0, len(X)): + prediction = predict_ratio(X[d], neural_net) + + # Calculate total error per label + true_prediction = [] + for i in range(0, len(labels)): + true_prediction.append(0) + true_prediction[labels.index(Y[d])] = 1 + + errors = [] + for t in range(len(prediction)): + errors.append(true_prediction[t] - prediction[t]) + adjust_deriv = errors * sigmoid_deriv(prediction) + + for k in range(0, len(adjust_deriv)): + adjustment = np.dot(X[d], adjust_deriv[k]) + neural_net[0][0][k] += adjustment + return neural_net + + +if __name__ == '__main__': + X = [[1, 1, 0, 0, 1, 1, 1, 1], [1, 1, 0, 0, 0, 1, 1, 1], [1, 0, 0, 0, 1, 1, 1, 1], [0, 1, 1, 1, 0, 0, 0, 1], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0]] + Y = [1, 1, 1, 0, 0, 0] + labels = [0, 1] + layer_array = [[len(labels), 'sigmoid']] + input_dims = 8 + neural_net = create_neural_net(layer_array, input_dims) + + print('weights:', neural_net[0], '\nbiases:', neural_net[1], '\nactivations:', neural_net[2]) + neural_net = train_network(X, Y, labels, neural_net, epochs=1000) + for i in range(len(X)): + print(predict(X[i], neural_net)) -- 2.34.1 From 267521f871fed67c6485e5cdc58c498a0a281762 Mon Sep 17 00:00:00 2001 From: pl5g92irw <484114892@qq.com> Date: Tue, 5 Apr 2022 23:30:48 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BB=A5Verilog=E8=AF=AD=E8=A8=80=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0FPGA=E7=A5=9E=E7=BB=8F=E7=BD=91=E7=BB=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- translated.v | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 translated.v diff --git a/translated.v b/translated.v new file mode 100644 index 0000000..b90dc9d --- /dev/null +++ b/translated.v @@ -0,0 +1,41 @@ +module main; + `include "tasks.v" + // global vars + reg [7:0] a [0:3]; + reg [7:0] b [0:3] [0:3]; + reg signed [7:0] i = 2; + reg signed [7:0] j; + reg [8*14:1] string_test = "ad"; + + // Include all tasks + + + // main() + initial begin + for (i=0; i<=3; i++) begin + sigmoid_deriv_task(i,j); // Demonstrating task in for loop + $display(j); + end + + for (i=0; i<=3; i++) begin + j = sigmoid_deriv_func(i); // Demonstrating function in for loop + $display(j); + end + + for (i=0; i<=3; i++) begin + for (j=0; j<=3; j++) begin // Creating 4D array + b[i][j] = i*j; + end + end + + j = 2 + i = sigmoid(j) + + for (i=0; i<=3; i++) begin + for (j=0; j<=3; j++) begin + $write(b[i][j]); + end + $display(); + end + end +endmodule \ No newline at end of file -- 2.34.1 From 48bebc411a9ff0ee6f8543957e20499b2645656e Mon Sep 17 00:00:00 2001 From: pl5g92irw <484114892@qq.com> Date: Tue, 5 Apr 2022 23:32:50 +0800 Subject: [PATCH 3/3] =?UTF-8?q?FPGA=E7=A5=9E=E7=BB=8F=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E6=89=80=E9=87=8D=E5=A4=8D=E8=B0=83=E5=8A=A8=E7=9A=84=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E6=A8=A1=E5=9D=97=EF=BC=8CVerilog=E8=AF=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tasks.v | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tasks.v diff --git a/tasks.v b/tasks.v new file mode 100644 index 0000000..336a232 --- /dev/null +++ b/tasks.v @@ -0,0 +1,25 @@ +//Task version + +task automatic sigmoid_deriv_task; + input [7:0] x; + output [7:0] y; + begin + y = x * (1-x); + end +endtask + +//Function version + +function automatic [7:0] sigmoid_deriv_func; + input signed [7:0] x; + begin + sigmoid_deriv_func = x * (1-x); + end + endfunction + +function automatic [7:0] sigmoid; + input signed [7:0] x; + begin + sigmoid = 1 / (1+ 2.71828**(-x)); + end +endfunction \ No newline at end of file -- 2.34.1