2023 中原電機知識馬拉松競賽
===
# 1. 機器學習中的演算法
## 1.1 線性迴歸(Linear Regression):
Date:2023/05/23
>線性迴歸屬於++監督式學習++,可以拆分成「線性」和「迴歸」來了解,前者為模型類型,稱「線性模型」,後者為問題類型,稱「迴歸問題」。線性模型能用大家熟悉的y = ax + b 來理解,而迴歸問題是一種統計方法,用於表示相依變數和獨立變數之間的關係,通常相依變數會隨著獨立變數變化,所以結合上述兩者能得出++線性迴歸++是用來預測一個連續的值,目標是想找一條直線可以逼近真實的資料。
#待修改
### 1.1.1 訓練步驟:
>**STEP1** : 求出模型參數 W
>**STEP2** : 損失函數最小化(使真實值與預測值的誤差總和最小化)
>**STEP3** : 梯度下降
### 1.1.2 線性迴歸**python**應用範例:
#### **範例 1:**
**使用套件**
```python=
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
```
**生成資料並利用matplotlib顯示資料分布**
```python=+
X,Y = make_regression(n_samples=200, n_features=1, noise=50,
random_state =42)
#畫出網格線
plt.grid()
#畫出所有資料
plt.scatter(X,Y)
```
>
>
**利用train_test_split進行資料分割,分成訓練集70%測試集30%**
```python=+
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3,
random_state=0)
```
**建立模型和訓練模型,並顯示預測的直線**
``` python=+
#建立模型
model = linear_model.LinearRegression()
#訓練模型
model.fit(X_train, Y_train)
#畫出網格線
plt.grid()
#劃出訓練集資料
plt.scatter(X_train, Y_train, color='blue')
#畫出訓練好的直線
plt.plot(X_train, model.predict(X_train),color='red',linewidth=1)
plt.show()
```
>
**計算直線方程式**
```python=+
#計算截距
W0 = model.intercept_
#計算特徵係數
W1 = model.coef_
print("y = ",W0," + ",W1,"* x")
```
y = 4.996594357444268 + [87.22045235] * x
```python=+
#使用訓練好的模型預測測試集中的Y
Y_pred = model.predict(X_test)
#畫出測試集資料
plt.scatter(X_test, Y_test, color='red')
#畫出網格線
plt.grid()
#畫出預測Y與測試集X的關係
plt.plot(X_test, Y_pred,color='blue',linewidth=1)
plt.show()```

**使用訓練好的模型分別帶回訓練集和測試集計算其準確率**
```python=+
train_score = model.score(X_train,Y_train)
test_score = model.score(X_test, Y_test)
print('train_score: '+ str(train_score*100) + '%')
print('test_score: ' + str(test_score*100) + '%')
```
train_score: 71.69772873715996%
test_score: 70.39215598647498%
```python=+
# The mean squared error
#利用mse計算預測值與誤差值的總和(越小越好)
print("Mean squared error: %.2f" % mean_squared_error(Y_test, Y_pred))
#The coefficient of determination: 1 is perfect prediction
#利用r2_score判斷迴歸模型的解釋力(越接近於 1 最好)
print("Coefficient of determination: %.2f" % r2_score(Y_test, Y_pred))
```
Mean squared error: 2601.96
Coefficient of determination: 0.70
### 1.1.3 參考資料:
* https://zhuanlan.zhihu.com/p/80887841
* https://scikit-learn.org/stable/auto_examples/linear_model/plot_ols.html
* https://chwang12341.medium.com/machine-learning-linear-regression%E8%BF%B4%E6%AD%B8%E6%A8%A1%E5%9E%8B-%E5%BC%B7%E5%A4%A7%E7%9A%84sklearn-%E7%B0%A1%E5%96%AE%E7%B7%9A%E6%80%A7%E8%BF%B4%E6%AD%B8%E6%A8%A1%E5%9E%8B-%E5%A4%9A%E9%A0%85%E5%BC%8F%E8%BF%B4%E6%AD%B8%E6%A8%A1%E5%9E%8B-%E5%A4%9A%E5%85%83%E8%BF%B4%E6%AD%B8%E6%A8%A1%E5%9E%8B-%E5%AE%8C%E6%95%B4%E5%AF%A6%E4%BD%9C%E6%95%99%E5%AD%B8-984c73ab5e05
## 1.2 邏輯迴歸(Logistic Regression):
邏輯迴歸屬於監督式學習,用於二分類問題,其判斷方式是用線性迴歸的輸出來進行分類,用一個簡單的思維來描述,就是將直角坐標系上的任一點帶入迴歸線,利用輸出值>=0以及<0將結果分成兩類,而實際上在邏輯迴歸運作時是在輸出迴歸線後添加了sigmoid的函數才能使輸出具有分類效果。
* sigmoid函數:

### 1.2.1 邏輯迴歸**python**應用範例:
## 1.3 單純貝氏(Naive Bayes):
## 1.4 決策樹(Decision Tree):
## 1.5 隨機森林(Random Forest):
## 1.6 支援向量機(Support Vector Machine):
## 1.7 GDBT(Gradient Boosting Trees):
# 2. 意外的小插曲:
## 2.1 VLSI LAB 8×8 SRAM 64BITS:
### 2.1.1 前言:
這是在學校上的VLSI系統設計實習的期末作業,老師要求我們製作出一個8×8或是8×6的sram。這項作業是與同一堂課的一位朋友共同完成。
### 2.1.2 繳交項目
* 1-bit SRAM 電路圖、sp檔、模擬結果
* 8×8-bits SRAM 電路圖、sp檔、模擬結果
* Precharge 電路圖、sp檔、模擬結果
* Decoder 電路圖、sp檔、模擬結果
* Senseamp 電路圖、sp檔、模擬結果
* Outbuffer 電路圖、sp檔、模擬結果
* Write Control 電路圖、sp檔、模擬結果
### 2.1.2 What is sram ?
靜態隨機存取記憶體**sram**(Static random-access memory)是一種靜態、揮發性、隨機存取的半導體記憶體,且具有讀寫操作。
sram是一種靜態記憶體,這表示他不需要定期整理,而就資料的保存持久性,只要維持在通電的情況下,儲存在裡面的資料就會一直保留。
* 完整 sram 的結構

### 2.1.3 1-bit sram 的電路結構和 mos 尺寸
* mos尺寸參照 SRAM Ref
* PMOS 4u/340n
* NMOS 2.5u/340n

* 1-bit sram input

* 1-bit sram 電路模擬結果

### 2.1.4 8×8 bit sram結構
* 完整 8×8 bit sram結構
* Precharge
* Row_Decoder
* Col_Decoder
* Col_Select
* Write_Control
* SenseAmplifier
* OutBuffer

### 2.1.5 Precharge 電路及功能
Pre-charge電路確保在W/R動作時BL及BLB位於高電位。W/R操作前,Pre-charge會先將BL及BLB置為VDD,且當Write為低電位時,Senseam及OutputBufferr透過CLK Posedge將WL資料輸出。

* Precharge Layout DRC

* PreCharge Layout LVS

### 2.1.6 Write Control 電路及功能
透過CLK將資料輸入至 BL 及 BLB 中使 Cell儲存這些資料。

* Write Control Layout DRC

* Write Control Layout LVS

### 2.1.7 Decoder 電路及功能
3 to 8 Decoder 由三個 inverter 及 8 個 AND Gate 組成,使輸入 3 bit 能轉換成 8 bit 的控制訊號,用以選擇 Row 及 Column 欲操作的 SRAM Cell。

* Decoder 所需的 AND Layout DRC

* Decoder 所需的 AND Layout LVS

### 2.1.8 SenseAmplifier 電路及功能
用於在Read Operation時,由於讀取前Precharge會先將BL及BLB充電至高電位,當Cell內部存0時,BL會緩慢下降,Senseamp透過差動放大器比較BL及BLB差值,能更快的輸出正確的電位。

### 2.1.9 OutputBuffer 電路及功能
用以調整不同CLK時脈的輸出速度。

* OutputBuffer Layout DRC

* OutputBuffer Layout LVS

### 2.1.10 Final 64 bits Sram電路
* 64-bits SRAM 組成
1. Precharge*8
1. ROW_Decoder*1
1. COL_Decoder*1
1. COL_Select*8
1. Write_Control*8
1. SenseAmplifier*8
1. OutputBuffer*8
1. Cell*64

## 2.2 人工智慧於電網之應用
### 2.2.1
{"title":"2023 中原電機知識馬拉松競賽","breaks":true,"metaMigratedAt":"2023-06-19T09:40:32.380Z","metaMigratedFrom":"Content","contributors":"[{\"id\":\"5f2b98cb-ccb0-42a0-8a47-ca74e27dd546\",\"add\":7307,\"del\":1262}]"}