feat: repository pattern
This commit is contained in:
parent
27f29d7632
commit
ddcf4d0c90
Binary file not shown.
|
@ -0,0 +1,12 @@
|
||||||
|
# /blueprints/user.py
|
||||||
|
|
||||||
|
from flask import Blueprint, jsonify
|
||||||
|
from controllers.user_controller import UserController
|
||||||
|
|
||||||
|
user_blueprint = Blueprint("user", __name__)
|
||||||
|
user_controller = UserController()
|
||||||
|
|
||||||
|
|
||||||
|
@user_blueprint.route("/users", methods=["GET"])
|
||||||
|
def get_users():
|
||||||
|
return user_controller.get_users()
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,17 @@
|
||||||
|
|
||||||
|
|
||||||
|
# # Sample Data Preparation
|
||||||
|
# input_dim = 3 # Example input dimension
|
||||||
|
# hidden_dim = 5 # Example hidden dimension
|
||||||
|
# num_samples = 1000
|
||||||
|
# sequence_length = 10
|
||||||
|
|
||||||
|
# # Generate dummy data for training
|
||||||
|
# X = np.random.randn(num_samples, input_dim, 1) # Shape (num_samples, input_dim, 1)
|
||||||
|
# y = np.random.randn(
|
||||||
|
# num_samples, hidden_dim, 1
|
||||||
|
# ) # Shape (num_samples, hidden_dim, 1)
|
||||||
|
|
||||||
|
# # Initialize and train the LSTM
|
||||||
|
# lstm = LSTM(input_dim, hidden_dim)
|
||||||
|
# lstm.train(X, y, num_epochs=10, learning_rate=0.01)
|
|
@ -0,0 +1,12 @@
|
||||||
|
# /controllers/user_controller.py
|
||||||
|
from flask import jsonify
|
||||||
|
from services.user_service import UserService
|
||||||
|
|
||||||
|
|
||||||
|
class UserController:
|
||||||
|
def __init__(self):
|
||||||
|
self.user_service = UserService()
|
||||||
|
|
||||||
|
def get_users(self):
|
||||||
|
users = self.user_service.get_all_users()
|
||||||
|
return jsonify(users)
|
58
lstm.py
58
lstm.py
|
@ -1,58 +0,0 @@
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
class LSTM:
|
|
||||||
def __init__(self, input_dim, hidden_dim):
|
|
||||||
# Initialize weights and biases
|
|
||||||
self.Wf = np.random.rand(hidden_dim, hidden_dim + input_dim)
|
|
||||||
self.bf = np.random.rand(hidden_dim, 1)
|
|
||||||
|
|
||||||
self.Wi = np.random.rand(hidden_dim, hidden_dim + input_dim)
|
|
||||||
self.bi = np.random.rand(hidden_dim, 1)
|
|
||||||
|
|
||||||
self.WC = np.random.rand(hidden_dim, hidden_dim + input_dim)
|
|
||||||
self.bC = np.random.rand(hidden_dim, 1)
|
|
||||||
|
|
||||||
self.Wo = np.random.rand(hidden_dim, hidden_dim + input_dim)
|
|
||||||
self.bo = np.random.rand(hidden_dim, 1)
|
|
||||||
|
|
||||||
def sigmoid(self, x):
|
|
||||||
return 1 / (1 + np.exp(-x))
|
|
||||||
|
|
||||||
def tanh(self, x):
|
|
||||||
return np.tanh(x)
|
|
||||||
|
|
||||||
def forward(self, x_t, h_prev, C_prev):
|
|
||||||
# Combine previous hidden state and current input
|
|
||||||
combined = np.vstack((h_prev, x_t))
|
|
||||||
|
|
||||||
# Forget gate
|
|
||||||
f_t = self.sigmoid(np.dot(self.Wf, combined) + self.bf)
|
|
||||||
|
|
||||||
# Input gate
|
|
||||||
i_t = self.sigmoid(np.dot(self.Wi, combined) + self.bi)
|
|
||||||
C_tilde = self.tanh(np.dot(self.WC, combined) + self.bC)
|
|
||||||
|
|
||||||
# Cell state
|
|
||||||
C_t = f_t * C_prev + i_t * C_tilde
|
|
||||||
|
|
||||||
# Output gate
|
|
||||||
o_t = self.sigmoid(np.dot(self.Wo, combined) + self.bo)
|
|
||||||
h_t = o_t * self.tanh(C_t)
|
|
||||||
|
|
||||||
return h_t, C_t
|
|
||||||
|
|
||||||
# Example usage
|
|
||||||
input_dim = 5 # Input feature size
|
|
||||||
hidden_dim = 3 # Number of hidden units
|
|
||||||
lstm = LSTM(input_dim, hidden_dim)
|
|
||||||
|
|
||||||
# Sample inputs
|
|
||||||
h_prev = np.zeros((hidden_dim, 1)) # Previous hidden state
|
|
||||||
C_prev = np.zeros((hidden_dim, 1)) # Previous cell state
|
|
||||||
x_t = np.random.rand(input_dim, 1) # Current input
|
|
||||||
|
|
||||||
# Forward pass
|
|
||||||
h_t, C_t = lstm.forward(x_t, h_prev, C_prev)
|
|
||||||
|
|
||||||
print("Current hidden state:", h_t)
|
|
||||||
print("Current cell state:", C_t)
|
|
9
main.py
9
main.py
|
@ -1,12 +1,9 @@
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
from blueprints.user import user_blueprint
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@app.route('/')
|
app.register_blueprint(user_blueprint, url_prefix="/api")
|
||||||
def home():
|
|
||||||
return "Hello, World!"
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,3 @@
|
||||||
|
class UserRepository:
|
||||||
|
def get_all_users(self):
|
||||||
|
return [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,53 @@
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
class LSTM:
|
||||||
|
def __init__(self, input_dim, hidden_dim):
|
||||||
|
self.input_dim = input_dim
|
||||||
|
self.hidden_dim = hidden_dim
|
||||||
|
self.Wf = np.random.randn(hidden_dim, input_dim + hidden_dim) * 0.01
|
||||||
|
self.Wi = np.random.randn(hidden_dim, input_dim + hidden_dim) * 0.01
|
||||||
|
self.Wc = np.random.randn(hidden_dim, input_dim + hidden_dim) * 0.01
|
||||||
|
self.Wo = np.random.randn(hidden_dim, input_dim + hidden_dim) * 0.01
|
||||||
|
self.bf = np.zeros((hidden_dim, 1))
|
||||||
|
self.bi = np.zeros((hidden_dim, 1))
|
||||||
|
self.bc = np.zeros((hidden_dim, 1))
|
||||||
|
self.bo = np.zeros((hidden_dim, 1))
|
||||||
|
self.h = np.zeros((hidden_dim, 1))
|
||||||
|
self.c = np.zeros((hidden_dim, 1))
|
||||||
|
|
||||||
|
def sigmoid(self, x):
|
||||||
|
return 1 / (1 + np.exp(-x))
|
||||||
|
|
||||||
|
def tanh(self, x):
|
||||||
|
return np.tanh(x)
|
||||||
|
|
||||||
|
def forward(self, x_t):
|
||||||
|
combined = np.vstack((self.h, x_t))
|
||||||
|
f_t = self.sigmoid(np.dot(self.Wf, combined) + self.bf)
|
||||||
|
i_t = self.sigmoid(np.dot(self.Wi, combined) + self.bi)
|
||||||
|
C_tilde_t = self.tanh(np.dot(self.Wc, combined) + self.bc)
|
||||||
|
self.c = f_t * self.c + i_t * C_tilde_t
|
||||||
|
o_t = self.sigmoid(np.dot(self.Wo, combined) + self.bo)
|
||||||
|
self.h = o_t * self.tanh(self.c)
|
||||||
|
return self.h
|
||||||
|
|
||||||
|
def backward(self, x_t, h_t, y_t, learning_rate):
|
||||||
|
# Your backward pass implementation here
|
||||||
|
pass
|
||||||
|
|
||||||
|
def train(self, X, y, num_epochs, learning_rate):
|
||||||
|
for epoch in range(num_epochs):
|
||||||
|
for i in range(len(X)):
|
||||||
|
x_t = X[i]
|
||||||
|
y_t = y[i]
|
||||||
|
|
||||||
|
# Forward pass
|
||||||
|
h_t = self.forward(x_t)
|
||||||
|
|
||||||
|
# Calculate loss and perform backward pass
|
||||||
|
loss = np.mean((h_t - y_t) ** 2) # Example loss
|
||||||
|
self.backward(x_t, h_t, y_t, learning_rate)
|
||||||
|
|
||||||
|
if i % 100 == 0: # Print loss every 100 samples
|
||||||
|
print(f"Epoch {epoch}, Sample {i}, Loss: {loss}")
|
|
@ -0,0 +1,11 @@
|
||||||
|
# /services/user_service.py
|
||||||
|
|
||||||
|
from repositories.user_repository import UserRepository
|
||||||
|
|
||||||
|
|
||||||
|
class UserService:
|
||||||
|
def __init__(self):
|
||||||
|
self.user_repository = UserRepository()
|
||||||
|
|
||||||
|
def get_all_users(self):
|
||||||
|
return self.user_repository.get_all_users()
|
Loading…
Reference in New Issue