<jupyter_start><jupyter_text>5. Hyper Parameter의 잦은 변경을 적용하기 위해 변수를 사용* cf) 재시작후 모두 실행하는 단축키 : ctl+m,r  Hyper Parameter 세팅미리 hyper parameter 를 변수로 빼두면 변경하기가 쉽다.이 부분에 interact 를 적용해보는 것도 하나의 방법일 수 있다.[2.AnalyticProcess/Interact.ipynb](https://github.com/Finfra/TensorflowStudyExample/blob/master/s2.5/2.AnalyticProcess/Interact.ipynb)(변경 후 위의 cf 를 통해 전체를 재시작해주면 된다.)<jupyter_code>batch_size = 50
num_classes = 3
epochs = 200<jupyter_output><empty_output><jupyter_text>모델 라이브러리 선언<jupyter_code>import numpy as np
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import matplotlib.pyplot as plt
def hist_view(hist):
  print('## training loss and acc ##')
  fig, loss_ax = plt.subplots()
  acc_ax = loss_ax.twinx()

  loss_ax.plot(hist.history['loss'], 'y', label='train loss')
  loss_ax.plot(hist.history['val_loss'], 'r', label='val loss')

  loss_ax.set_xlabel('epoch')
  loss_ax.set_ylabel('loss')
  loss_ax.legend(loc='center')

  acc_ax.plot(hist.history['accuracy'], 'b', label='train acc')
  acc_ax.plot(hist.history['val_accuracy'], 'g', label='val acc')
  acc_ax.set_ylabel('accuracy')
  acc_ax.legend(loc='center right')

  plt.show()<jupyter_output><empty_output><jupyter_text>데이터세팅<jupyter_code>from sklearn import datasets
iris = datasets.load_iris()
x=iris.data

# num_classes
y= keras.utils.to_categorical(iris.target, num_classes)

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=0)
x_train.shape,x_test.shape,y_train.shape,y_test.shape<jupyter_output><empty_output><jupyter_text>모델 설계<jupyter_code>model = Sequential()
model.add(Dense(5, activation='relu', input_shape=(4,)))
#num_classes
model.add(Dense(num_classes, activation='softmax'))
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 5)                 25        
_________________________________________________________________
dense_1 (Dense)              (None, 3)                 18        
=================================================================
Total params: 43
Trainable params: 43
Non-trainable params: 0
_________________________________________________________________<jupyter_text>모델의 function 설정<jupyter_code>model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Hyper Parameter 변경점위에서 미리 세팅한 변수들을 여기에 적용해서 위에서 바꿀때마다 세팅이 바뀐다.<jupyter_code># batch_size,epochs
hist=model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))<jupyter_output>Epoch 1/200
3/3 [==============================] - 3s 79ms/step - loss: 1.3393 - accuracy: 0.3048 - val_loss: 1.3266 - val_accuracy: 0.3333
Epoch 2/200
3/3 [==============================] - 0s 10ms/step - loss: 1.3096 - accuracy: 0.3048 - val_loss: 1.3008 - val_accuracy: 0.3333
Epoch 3/200
3/3 [==============================] - 0s 12ms/step - loss: 1.2844 - accuracy: 0.2857 - val_loss: 1.2746 - val_accuracy: 0.3333
Epoch 4/200
3/3 [==============================] - 0s 9ms/step - loss: 1.2601 - accuracy: 0.2952 - val_loss: 1.2512 - val_accuracy: 0.3556
Epoch 5/200
3/3 [==============================] - 0s 10ms/step - loss: 1.2375 - accuracy: 0.2952 - val_loss: 1.2298 - val_accuracy: 0.2889
Epoch 6/200
3/3 [==============================] - 0s 11ms/step - loss: 1.2160 - accuracy: 0.2952 - val_loss: 1.2097 - val_accuracy: 0.2889
Epoch 7/200
3/3 [==============================] - 0s 21ms/step - loss: 1.1968 - accuracy: 0.2952 - val_loss: 1.1897 - val_accuracy: 0.2667
Epoch 8/200
3/3 [====[...]<jupyter_text>결과 확인<jupyter_code>hist_view(hist)
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

decoder = {k:v for k,v in enumerate( iris.target_names )}
r=np.argmax(model.predict(x_test[:10,:]), axis=-1)
[decoder[i] for i in r]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2008949599623175, 'fraction_numerical': 0.05723033443240697, 'mean_word_length': 4.906815020862309, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 1, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '428261', 'license': 'MIT', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1704, 'n_words': 296, 'path': '03.MLP/05.MLP_HyperParameterVariable.ipynb', 'repo_name': 'Finfra/DeepLearningCourse', 'size': '4246'}