Commit 3febc175 by ishikawatatsuki

Initializing labs

parents
File added
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include "includes/AnyLib.h"
double MathBasics::multi_a_b(double a, double b)
{
return a * b;
}
\ No newline at end of file
# Generated Cmake Pico project file
cmake_minimum_required(VERSION 3.13)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
# initalize pico_sdk from installed location
# (note this can come from environment, CMake cache etc)
#set(PICO_SDK_PATH "D:/Raspberry/Raspberry_pico/code/pico-sdk")
# Pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake)
project(MLES C CXX ASM)
# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()
# Add executable. Default name is the project name, version 0.1
add_executable(main main.cpp includes/AnyLib.h AnyLib.cpp)
# Include directories
include_directories(../includes)
pico_set_program_name(main "GPIO")
pico_set_program_version(main "0.1")
pico_enable_stdio_uart(main 1)
pico_enable_stdio_usb(main 1)
# Add the standard library to the build
target_link_libraries(main pico_stdlib)
pico_add_extra_outputs(main)
#ifndef ANYLIB_H
#define ANYLIB_H
#include <vector>
#include <string>
class MathBasics
{
public:
// Function to multiply
double multi_a_b(double a, double b);
};
#endif // ANYLIB_H
#include <stdio.h>
#include <iostream>
#include <vector>
#include "pico/stdlib.h"
#include "includes/AnyLib.h"
const uint LED_PIN = 25;
void task1()
{
MathBasics mb;
double a = 23, b = -2;
double result = mb.multi_a_b(a, b);
printf("%f\n", result);
}
int main()
{
stdio_init_all();
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN,GPIO_OUT);
bool value =0;
while (true)
{
gpio_put(LED_PIN,value);
sleep_ms(500);
value =!value;
printf("LED toggle\n");
task1();
}
return 0;
}
\ No newline at end of file
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()
if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()
set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_SDK_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
)
if (NOT pico_sdk)
message("Downloading Raspberry Pi Pico SDK")
FetchContent_Populate(pico_sdk)
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
message(FATAL_ERROR
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
)
endif ()
endif ()
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_SDK_PATH})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
endif ()
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
endif ()
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
include(${PICO_SDK_INIT_CMAKE_FILE})
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include "AnyLib.h"
double MathBasics::multi_a_b(double a, double b)
{
return a * b;
}
\ No newline at end of file
#ifndef ANYLIB_H
#define ANYLIB_H
#include <vector>
#include <string>
class MathBasics
{
public:
// Function to multiply
double multi_a_b(double a, double b);
};
#endif // ANYLIB_H
BIN = lab0
CC = g++
SRC = lab.cpp AnyLib.cpp
all: $(BIN)
$(BIN): $(SRC)
$(CC) -o $(BIN) $(SRC)
run: $(BIN)
./$(BIN)
\ No newline at end of file
#include <stdio.h>
#include <iostream>
#include <vector>
#include "AnyLib.h"
void task1()
{
MathBasics mb;
double a = 23, b = -2;
double result = mb.multi_a_b(a, b);
printf("%f\n", result);
}
int main()
{
task1();
}
\ No newline at end of file
BIN = lab1
CC = g++
SRC = lab1.cpp ../lib/src/NeuralNetwork.cpp
all: $(BIN)
$(BIN): $(SRC)
$(CC) -o $(BIN) $(SRC)
run: $(BIN)
./$(BIN)
\ No newline at end of file
#include <iostream>
#include <vector>
#include "../lib/includes/NeuralNetwork.h"
void task1() {
NeuralNetwork nn;
double input = 23, weight = -2, bias = 92;
double result = nn.singleNeuron(input, weight) + bias;
std::cout << "Single Neuron Result: " << result << "\n";
}
void task2() {
NeuralNetwork nn;
std::vector<double> temperature = {12, 23, 50, -10, 16};
std::vector<double> humidity = {60, 67, 45, 65, 63};
std::vector<double> airQuality = {60, 47, 157, 187, 94};
std::vector<double> weights = {-2, 2, 1};
double result = 0;
double bias = 0;
for (size_t i = 0; i < temperature.size(); i++) {
std::vector<double> inputs = {temperature[i], humidity[i], airQuality[i]};
result = nn.multipleInputSingleOutput(inputs, weights, bias);
std::cout << "Multiple Input Single Output Result: " << result << "\n";
}
}
void task3() {
NeuralNetwork nn;
double input = 0.9;
std::vector<double> weights = {-20.2, 95, 201.0};
std::vector<double> outputs(3);
double bias = 0;
nn.singleInputMultipleOutput(input, weights, bias, outputs);
std::cout << "Single Input Multiple Outputs Result: " << outputs[0] << ", " << outputs[1] << ", " << outputs[2] << "\n";
}
void task4() {
NeuralNetwork nn;
int inputSize = 3, outputSize = 3;
std::vector<double> inputs = {30.0, 87.0, 110.0};
std::vector<double> biases = {0, 0, 0};
std::vector<double> multiOutputs(3);
std::vector<double> multiWeights = {
-2.0, 9.5, 2.0, // Weights for output 1
-0.8, 7.2, 6.3, // Weights for output 2
-0.5, 0.4, 0.9 // Weights for output 3
};
nn.multipleInputMultipleOutput(inputs, multiWeights, biases, multiOutputs, inputSize, outputSize);
std::cout << "Multiple Input Multiple Outputs Result: " << multiOutputs[0] << ", " << multiOutputs[1] << ", " << multiOutputs[2] << "\n";
}
int main() {
task1();
// task2();
// task3();
// task4();
return 0;
}
BIN = lab2
CC = g++
SRC = lab2.cpp ../lib/src/NeuralNetwork.cpp
all: $(BIN)
$(BIN): $(SRC)
$(CC) -o $(BIN) $(SRC)
run: $(BIN)
./$(BIN)
\ No newline at end of file
#include <iostream>
#include <vector>
#include "../lib/includes/NeuralNetwork.h"
// Define constants for the layer sizes
const int OUT_LEN = 3;
const int IN_LEN = 3;
const int HID_LEN = 3;
std::vector<double> inputVector = {30.0, 87.0, 110.0}; // Input values: temp, hum, air_q
// Weights from input layer to hidden layer
std::vector<std::vector<double>> inputToHiddenWeights = {
{-2.0, 9.5, 2.01}, // Hidden neuron 1
{-0.8, 7.2, 6.3}, // Hidden neuron 2
{-0.5, 0.4, 0.9} // Hidden neuron 3
};
// Biases for hidden layer neurons
std::vector<double> hiddenBiases = {0, 0, 0};
// Weights from hidden layer to output layer
std::vector<std::vector<double>> hiddenToOutputWeights = {
{-1.0, 1.15, 0.11}, // Sad prediction
{-0.18, 0.15, -0.01},// Sick prediction
{0.25, -0.25, -0.1} // Active prediction
};
// Biases for output layer neurons
std::vector<double> outputBiases = {0, 0, 0};
// Arrays to store the outputs of the hidden layer and final output
std::vector<double> hiddenOutputs(HID_LEN);
std::vector<double> predictedOutput(OUT_LEN);
std::vector<double> error(OUT_LEN);
// Ground truth values for the output
std::vector<double> groundTruth = {600, 10, -80};
// Task 1: Compute predictions based on the input and weights
void task1() {
NeuralNetwork nn;
// Step 1: Compute the hidden layer outputs
std::vector<double> flattenedInputToHiddenWeights;
for (const auto& row : inputToHiddenWeights) {
flattenedInputToHiddenWeights.insert(flattenedInputToHiddenWeights.end(), row.begin(), row.end());
}
nn.hiddenLayer(inputVector, flattenedInputToHiddenWeights, hiddenBiases, hiddenOutputs, IN_LEN, HID_LEN);
// Step 2: Compute the final output predictions using the hidden layer outputs
std::vector<double> flattenedHiddenToOutputWeights;
for (const auto& row : hiddenToOutputWeights) {
flattenedHiddenToOutputWeights.insert(flattenedHiddenToOutputWeights.end(), row.begin(), row.end());
}
nn.multipleInputMultipleOutput(hiddenOutputs, flattenedHiddenToOutputWeights, outputBiases, predictedOutput, HID_LEN, OUT_LEN);
// Print the predictions
std::cout << "Sad prediction: " << predictedOutput[0] << std::endl;
std::cout << "Sick prediction: " << predictedOutput[1] << std::endl;
std::cout << "Active prediction: " << predictedOutput[2] << std::endl;
}
// Task 2: Compute predictions and calculate errors compared to ground truth
void task2() {
NeuralNetwork nn;
// Step 1: Compute the hidden layer outputs
std::vector<double> flattenedInputToHiddenWeights;
for (const auto& row : inputToHiddenWeights) {
flattenedInputToHiddenWeights.insert(flattenedInputToHiddenWeights.end(), row.begin(), row.end());
}
nn.hiddenLayer(inputVector, flattenedInputToHiddenWeights, hiddenBiases, hiddenOutputs, IN_LEN, HID_LEN);
// Step 2: Compute the final output predictions using the hidden layer outputs
std::vector<double> flattenedHiddenToOutputWeights;
for (const auto& row : hiddenToOutputWeights) {
flattenedHiddenToOutputWeights.insert(flattenedHiddenToOutputWeights.end(), row.begin(), row.end());
}
nn.multipleInputMultipleOutput(hiddenOutputs, flattenedHiddenToOutputWeights, outputBiases, predictedOutput, HID_LEN, OUT_LEN);
// Step 3: Calculate the errors compared to the ground truth
nn.calculateError(predictedOutput, groundTruth, error);
// Step 4: Print the errors in the required format with high precision
std::cout << "\nError Analysis:\n";
for (int i = 0; i < OUT_LEN; i++) {
std::cout << "For ground truth " << groundTruth[i] << ", predicted output is " << predictedOutput[i]
<< ", error is " << error[i] << "\n";
}
}
// Task 3: Brute force learning to adjust weight
void task3() {
NeuralNetwork nn;
double input = 0.5; // Example input value
double weight = 0.5; // Initial weight
double expectedValue = 0.8; // Target (expected) output value
double learningRate = 0.001; // Learning rate
int maxEpochs = 1500; // Maximum number of epochs
// Perform brute-force learning to adjust the weight
nn.bruteForceLearning(input, weight, expectedValue, learningRate, maxEpochs);
}
int main() {
task1();
// task2();
// task3();
return 0;
}
File added
#ifndef NEURALNETWORK_H
#define NEURALNETWORK_H
#include <vector>
#include <string>
class NeuralNetwork {
public:
// Single neuron calculation
double singleNeuron(double input, double weight);
// Multiple inputs, single output
double multipleInputSingleOutput(std::vector<double> inputs, std::vector<double> weights, double bias);
// Single input, multiple outputs
void singleInputMultipleOutput(double input, std::vector<double> weights, double bias, std::vector<double>& outputs);
// Multiple inputs, multiple outputs
void multipleInputMultipleOutput(std::vector<double>& inputs, std::vector<double>& weights, std::vector<double>& biases, std::vector<double>& outputs, int inputSize, int outputSize);
// Hidden layer function
void hiddenLayer(std::vector<double>& inputs, std::vector<double>& hiddenWeights, std::vector<double>& hiddenBiases, std::vector<double>& hiddenOutputs, int inputSize, int hiddenSize);
// Error calculation
void calculateError(std::vector<double>& predictedOutput, std::vector<double>& groundTruth, std::vector<double>& error);
// Mean Squared Error (MSE)
double calculateMSE(std::vector<double>& error);
// Root Mean Squared Error (RMSE)
double calculateRMSE(double mse);
// Brute-force learning
void bruteForceLearning(double input, double& weight, double expectedValue, double learningRate, int maxEpochs);
// Activation functions (ReLU and Sigmoid)
double relu(double x);
double sigmoid(double x);
// Vectorized activation functions
void vectorReLU(std::vector<double>& inputVector, std::vector<double>& outputVector);
void vectorSigmoid(std::vector<double>& inputVector, std::vector<double>& outputVector);
// Save network
void saveNetwork(const std::string& filename, int numOfFeatures, int numOfHiddenNodes, int numOfOutputNodes,
std::vector<std::vector<double>>& inputToHiddenWeights, std::vector<double>& hiddenLayerBias,
std::vector<std::vector<double>>& hiddenToOutputWeights, std::vector<double>& outputLayerBias);
// Load network
void loadNetwork(const std::string& filename, int numOfFeatures, int numOfHiddenNodes, int numOfOutputNodes,
std::vector<std::vector<double>>& inputToHiddenWeights, std::vector<double>& hiddenLayerBias,
std::vector<std::vector<double>>& hiddenToOutputWeights, std::vector<double>& outputLayerBias);
};
#endif // NEURALNETWORK_H
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include "../includes/NeuralNetwork.h"
double NeuralNetwork::singleNeuron(double input, double weight)
{
return 0;
}
double NeuralNetwork::multipleInputSingleOutput(std::vector<double> inputs, std::vector<double> weights, double bias)
{
return 0;
}
void NeuralNetwork::singleInputMultipleOutput(double input, std::vector<double> weights, double bias, std::vector<double>& outputs)
{
return;
}
void NeuralNetwork::multipleInputMultipleOutput(std::vector<double>& inputs, std::vector<double>& weights, std::vector<double>& biases, std::vector<double>& outputs, int inputSize, int outputSize)
{
return;
}
void NeuralNetwork::hiddenLayer(std::vector<double>& inputs, std::vector<double>& hiddenWeights, std::vector<double>& hiddenBiases, std::vector<double>& hiddenOutputs, int inputSize, int hiddenSize)
{
return;
}
void NeuralNetwork::calculateError(std::vector<double>& predictedOutput, std::vector<double>& groundTruth, std::vector<double>& error)
{
return;
}
double NeuralNetwork::calculateMSE(std::vector<double>& error)
{
return 0;
}
double NeuralNetwork::calculateRMSE(double mse) {
return 0;
}
void NeuralNetwork::bruteForceLearning(double input, double& weight, double expectedValue, double learningRate, int maxEpochs)
{
return;
}
double NeuralNetwork::relu(double x)
{
return 0;
}
double NeuralNetwork::sigmoid(double x)
{
return 0;
}
void NeuralNetwork::vectorReLU(std::vector<double>& inputVector, std::vector<double>& outputVector)
{
return;
}
void NeuralNetwork::vectorSigmoid(std::vector<double>& inputVector, std::vector<double>& outputVector)
{
return;
}
void NeuralNetwork::saveNetwork(const std::string& filename, int numOfFeatures, int numOfHiddenNodes, int numOfOutputNodes,
std::vector<std::vector<double>>& inputToHiddenWeights, std::vector<double>& hiddenLayerBias,
std::vector<std::vector<double>>& hiddenToOutputWeights, std::vector<double>& outputLayerBias)
{
std::ofstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: Could not open file " << filename << " for writing.\n";
return;
}
file << "Hidden Layer Weights:\n";
for (int i = 0; i < numOfHiddenNodes; i++) {
for (int j = 0; j < numOfFeatures; j++) {
file << inputToHiddenWeights[i][j] << " ";
}
file << "\n";
}
file << "Hidden Layer Biases:\n";
for (int i = 0; i < numOfHiddenNodes; i++) {
file << hiddenLayerBias[i] << " ";
}
file << "\n";
file << "Output Layer Weights:\n";
for (int i = 0; i < numOfOutputNodes; i++) {
for (int j = 0; j < numOfHiddenNodes; j++) {
file << hiddenToOutputWeights[i][j] << " ";
}
file << "\n";
}
file << "Output Layer Biases:\n";
for (int i = 0; i < numOfOutputNodes; i++) {
file << outputLayerBias[i] << " ";
}
file << "\n";
file.close();
std::cout << "Network saved to file: " << filename << "\n";
}
void NeuralNetwork::loadNetwork(const std::string& filename, int numOfFeatures, int numOfHiddenNodes, int numOfOutputNodes,
std::vector<std::vector<double>>& inputToHiddenWeights, std::vector<double>& hiddenLayerBias,
std::vector<std::vector<double>>& hiddenToOutputWeights, std::vector<double>& outputLayerBias)
{
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: Could not open file " << filename << " for reading.\n";
return;
}
std::string temp;
file >> temp >> temp; // Skip "Hidden Layer Weights:"
for (int i = 0; i < numOfHiddenNodes; i++) {
for (int j = 0; j < numOfFeatures; j++) {
file >> inputToHiddenWeights[i][j];
}
}
file >> temp >> temp; // Skip "Hidden Layer Biases:"
for (int i = 0; i < numOfHiddenNodes; i++) {
file >> hiddenLayerBias[i];
}
file >> temp >> temp; // Skip "Output Layer Weights:"
for (int i = 0; i < numOfOutputNodes; i++) {
for (int j = 0; j < numOfHiddenNodes; j++) {
file >> hiddenToOutputWeights[i][j];
}
}
file >> temp >> temp; // Skip "Output Layer Biases:"
for (int i = 0; i < numOfOutputNodes; i++) {
file >> outputLayerBias[i];
}
file.close();
std::cout << "Network loaded from file: " << filename << "\n";
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment