<jupyter_start><jupyter_text>DATASET<jupyter_code>import tensorflow as tf
from tensorflow.keras.datasets.boston_housing import load_data
(x_train, y_train), (x_test,y_test)=load_data(path='boston_housing.npz', test_split=0.2 ,seed=113)

x_train.shape, y_train.shape, x_test.shape,y_test.shape
type(x_train), type(y_train)<jupyter_output><empty_output><jupyter_text>연속형 이기 때문에 y의 값은 볼필요 없다x_train을 판다스에 넣고 어떤 상황인지 파악해보자<jupyter_code>import pandas as pd
dfr = pd.DataFrame(x_train)
dfr.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 404 entries, 0 to 403
Data columns (total 13 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   0       404 non-null    float64
 1   1       404 non-null    float64
 2   2       404 non-null    float64
 3   3       404 non-null    float64
 4   4       404 non-null    float64
 5   5       404 non-null    float64
 6   6       404 non-null    float64
 7   7       404 non-null    float64
 8   8       404 non-null    float64
 9   9       404 non-null    float64
 10  10      404 non-null    float64
 11  11      404 non-null    float64
 12  12      404 non-null    float64
dtypes: float64(13)
memory usage: 41.2 KB<jupyter_text>데이터타입(dtype)은 전부다 float-숫자로 되어있는 것 확인가능레인지인덱스가 404개 이고 각 컬럼이 404개인 것 확인가능(object 없다, null 없다, space 없다)so, 전부가 숫자인 것을 알수 있었다.  `>>>` DB에 저장하기<jupyter_code>import sqlite3
connect = sqlite3.connect('./db.sqlite3')<jupyter_output><empty_output><jupyter_text>`>>>`이렇게 앞으로는 습관적으로 데이타 로드하면 데이타프레임에 넣고 db에 담는 작업을 해준다.<jupyter_code>dfr.to_sql('boston_housing_resource', connect, if_exists='append',index=False)<jupyter_output><empty_output><jupyter_text>`>>>`db의 데이터 불러와서 사용한다.<jupyter_code>df = pd.read_sql_query('select * from boston_housing_resource',connect)
df.describe()<jupyter_output><empty_output><jupyter_text>mean 보면 단위가 다 다르기때문에 .StandardScaler 해줘야한다<jupyter_code>from sklearn.preprocessing import StandardScaler 
scaler = StandardScaler()<jupyter_output><empty_output><jupyter_text>fit은 트레이닝<jupyter_code>scaler.fit(x_train)<jupyter_output><empty_output><jupyter_text>x_train으로 훈련 완료transform은 바꿔주는 것<jupyter_code>x_train= scaler.transform(x_train)
x_test = scaler.transform(x_test)
x_train.shape, x_test.shape<jupyter_output><empty_output><jupyter_text>해당부분이 얼마나 평이하게 바뀌었는가 확인df.describe()결론적으로 이런 과정을 거치는 이유는 안정적인 단위로 만들어 준 것<jupyter_code>df= pd.DataFrame(x_train)
df.describe()<jupyter_output><empty_output><jupyter_text>`>>>` 스케일링 한 x_train 데이터를 db에 담기<jupyter_code>df.to_sql('boston_housing_scale_x_train', connect, if_exists='append', index=False )<jupyter_output><empty_output><jupyter_text>`>>>` db에 담았던 스케일링 한 x_train 데이터 가져오기<jupyter_code>dfr = pd.read_sql_query('select * from boston_housing_scale_x_train',connect)
dfr.describe()<jupyter_output><empty_output><jupyter_text>MODEL<jupyter_code>model = tf.keras.models.Sequential()<jupyter_output><empty_output><jupyter_text>`>>`input layer/ hidden layer/ output layer* hidden layer에서 사용하는 activationactivation : 뒤에 들어가는 싱글코테이션 안에 들어가는 글자는 펑션이다. 사용자가 사용하기 위해 알리야스(별칭)을 준 것이다.sigmoid(...): Sigmoid activation function, sigmoid(x) = 1 / (1 + exp(-x)).softmax(...): Softmax converts a vector of values to a probability distribution. 범주형 마지막 아웃풋에서 사용linear(...): Linear activation function (pass-through).relu(...): Applies the rectified linear unit activation function.tanh(...): Hyperbolic tangent activation function.<jupyter_code>model.add(tf.keras.Input(shape=(13))) 

# tf.keras.activations.relu()
model.add(tf.keras.layers.Dense(64, activation='relu')) #hidden layer
model.add(tf.keras.layers.Dense(64, activation='relu')) #hidden layer
model.add(tf.keras.layers.Dense(64, activation='relu')) #hidden layer

model.add(tf.keras.layers.Dense(1,)) 

# sqd= tf.keras.optimizers.SGD( 
#     learning_rate=0.001,
#     momentum=0.004
# )

# model.compile(optimizer='sgd', loss='mse' , metrics=['mae'])

model.compile(optimizer='adam', loss='mse' , metrics=['mae'])<jupyter_output>WARNING:tensorflow:Please add `keras.layers.InputLayer` instead of `keras.Input` to Sequential model. `keras.Input` is intended to be used by Functional model.<jupyter_text>검증용  * 연속형은 metrics = mae  *분류형은 어큐런시  * 둘다는 loss<jupyter_code>tf.keras.utils.plot_model(model, show_shapes=True)
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 64)                896       
_________________________________________________________________
dense_1 (Dense)              (None, 64)                4160      
_________________________________________________________________
dense_2 (Dense)              (None, 64)                4160      
_________________________________________________________________
dense_3 (Dense)              (None, 1)                 65        
=================================================================
Total params: 9,281
Trainable params: 9,281
Non-trainable params: 0
_________________________________________________________________<jupyter_text>핏팅 전 검증용 validation_split= 하는 이유는 실제로 쏠림현상(오버피팅) 있는지 확인하는것 데이터를 내부적으로 쪼개서 실제 70% 인 데이터가 loss 측정하고, val은 나머지 30%를 넣어 결과값을 비교한다.<jupyter_code>hist = model.fit(x_train, y_train, epochs=500, validation_split=0.3)<jupyter_output>Epoch 1/500
9/9 [==============================] - 0s 8ms/step - loss: 0.8103 - mae: 0.6069 - val_loss: 7.4348 - val_mae: 1.8770
Epoch 2/500
9/9 [==============================] - 0s 6ms/step - loss: 0.7227 - mae: 0.5621 - val_loss: 7.3188 - val_mae: 1.8565
Epoch 3/500
9/9 [==============================] - 0s 4ms/step - loss: 0.7911 - mae: 0.6148 - val_loss: 7.9908 - val_mae: 1.9584
Epoch 4/500
9/9 [==============================] - 0s 4ms/step - loss: 0.8430 - mae: 0.6256 - val_loss: 6.9732 - val_mae: 1.8832
Epoch 5/500
9/9 [==============================] - 0s 4ms/step - loss: 0.8163 - mae: 0.6445 - val_loss: 7.5277 - val_mae: 1.9034
Epoch 6/500
9/9 [==============================] - 0s 6ms/step - loss: 0.7608 - mae: 0.5791 - val_loss: 7.7756 - val_mae: 1.9307
Epoch 7/500
9/9 [==============================] - 0s 4ms/step - loss: 0.7249 - mae: 0.5570 - val_loss: 7.4182 - val_mae: 1.8767
Epoch 8/500
9/9 [==============================] - 0s 4ms/step - loss: 0.7549 - mae: 0.5733 - val[...]<jupyter_text>* activations = sigmoid optimizer = sgd 결과 100번째loss: 8.4236 - mae: 2.1771 * activations = reluoptimizer = adam 결과 100번째  loss: 3.7667 - mae: 1.4202  Evaluation 엑티베이션<jupyter_code>model.evaluate(x_test, y_test)
model.evaluate(x_train, y_train)<jupyter_output>13/13 [==============================] - 0s 1ms/step - loss: 3.6771 - mae: 1.4230<jupyter_text>검증 hist<jupyter_code>hist.history.keys()
import matplotlib.pyplot as plt

plt.plot(hist.history['loss'], 'b--')
plt.plot(hist.history['val_loss'])
plt.show()<jupyter_output><empty_output><jupyter_text>Service<jupyter_code>x_train[10]<jupyter_output><empty_output><jupyter_text>기존결과model.predict([[2.2876e-01, 0.0000e+00, 8.5600e+00, 0.0000e+00, 5.2000e-01,       6.4050e+00, 8.5400e+01, 2.7147e+00, 5.0000e+00, 3.8400e+02,       2.0900e+01, 7.0800e+01, 1.0630e+01]])       array([[20.722834]], dtype=float32) hidden layer 늘리고서 결과 model.predict([[2.2876e-01, 0.0000e+00, 8.5600e+00, 0.0000e+00, 5.2000e-01,       6.4050e+00, 8.5400e+01, 2.7147e+00, 5.0000e+00, 3.8400e+02,       2.0900e+01, 7.0800e+01, 1.0630e+01]])       array([[12.5560875]], dtype=float32) 표준화 시킨 결과model.predict([[-0.380999  , -0.48361547, -0.37402299, -0.25683275, -0.31887859,        0.1945502 ,  0.58730449, -0.50578021, -0.51114231, -0.13178499,        1.10299743, -3.02127105, -0.29132553]])        array([[18.246944]], dtype=float32) x_train[10]의 결과(트레이닝 값)model.predict([[0.63391647, -0.48361547,  1.0283258 , -0.25683275,  1.15788777,        0.19313958,  1.11048828, -1.03628262,  1.67588577,  1.5652875 ,        0.78447637,  0.22689422,  1.04466491]])        array([[12.658238]], dtype=float32)y_train[10]의 결과 (실제값)12.1<jupyter_code>model.predict([[0.63391647, -0.48361547,  1.0283258 , -0.25683275,  1.15788777,
        0.19313958,  1.11048828, -1.03628262,  1.67588577,  1.5652875 ,
        0.78447637,  0.22689422,  1.04466491]])
y_train[10]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17782049741679684, 'fraction_numerical': 0.12267211342064159, 'mean_word_length': 4.3290653008962865, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '228558', 'license': 'Apache-2.0', 'n_tokens_mistral': 4198, 'n_tokens_neox': 3814, 'n_words': 618, 'path': 'Nomal_saveDB_boston_housing_with_regression.ipynb', 'repo_name': 'ginttone/multi_deeplearning', 'size': '8323'}