<jupyter_start><jupyter_text>set data<jupyter_code>from keras.datasets import mnist
(X_train0, y_train0), (X_test0, y_test0) = mnist.load_data()
print(X_train0.shape, X_train0.dtype)
print(y_train0.shape, y_train0.dtype)
print(X_test0.shape, X_test0.dtype)
print(y_test0.shape, y_test0.dtype)
%matplotlib inline
plt.figure(figsize=(2, 2))
plt.imshow(X_train0[0], cmap=mpl.cm.bone_r)
plt.grid(False)
plt.xticks([])
plt.yticks([])
plt.show()
X_train = X_train0.reshape(60000, 784).astype('float32') / 255.0
X_test = X_test0.reshape(10000, 784).astype('float32') / 255.0
print(X_train.shape, X_train.dtype)
y_train0[:5]
from keras.utils import np_utils

Y_train = np_utils.to_categorical(y_train0, 10)
Y_test = np_utils.to_categorical(y_test0, 10)
Y_train[:5]<jupyter_output><empty_output><jupyter_text>----  dense layer == FCN (Fully Connected Network)CNN (컨볼루셔널, dense x)<jupyter_code>from keras.models import Sequential
from keras.layers.core import Dense # dense 레이어 , FCN 
from keras.optimizers import SGD

np.random.seed(0)

model = Sequential()<jupyter_output><empty_output><jupyter_text>첫 레이어는 인풋 필요activation = 'sigmoid'  -> 0 과 1 사이 실수값- OvR용으로 사용하겠다는 의미<jupyter_code>model.add(Dense(15, input_dim=784, activation="sigmoid"))
model.add(Dense(10, activation="sigmoid"))<jupyter_output><empty_output><jupyter_text>lr = learning rateloss = 손실함수metrics<jupyter_code>model.compile(optimizer=SGD(lr=0.2), loss='mean_squared_error', metrics=["accuracy"])
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot

SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))<jupyter_output><empty_output><jupyter_text>trainable params : flag를 걸어서 업데이트 안하는 변수를 정해줄 수 있음<jupyter_code>model.summary()
l1 = model.layers[0] # 각각의 레이어 오브젝트
l2 = model.layers[1]
l1.name, type(l1), l1.output_shape, l1.activation.__name__, l1.count_params()
l2.name, type(l1), l2.output_shape, l2.activation.__name__, l2.count_params()
%%time
hist = model.fit(X_train, Y_train, 
                 epochs=30, batch_size=100, 
                 validation_data=(X_test, Y_test), # 에포크 끝날때 마다 cv
                 verbose=2) # progress 체크
plt.plot(hist.history['loss'])
plt.show()
plt.plot(hist.history['acc'], 'b-', label="training")
plt.plot(hist.history['val_acc'], 'r:', label="test")
plt.legend()
plt.show()
hist.history['val_acc'][-1:]
w1 = l1.get_weights()
w1[0].shape, w1[1].shape<jupyter_output><empty_output><jupyter_text>---<jupyter_code>model2 = Sequential()
model2.add(Dense(5, input_dim=784, activation="sigmoid"))
model2.add(Dense(10, activation="sigmoid"))
model2.compile(optimizer=SGD(lr=0.2), loss='mean_squared_error', metrics=["accuracy"])
model2.summary()
%%time
hist2 = model2.fit(X_train, Y_train, 
                 epochs=30, batch_size=100, 
                 validation_data=(X_test, Y_test), 
                 verbose=2)
hist.history['val_acc'][-1:]
hist2.history['val_acc'][-1:]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15680272108843538, 'fraction_numerical': 0.04013605442176871, 'mean_word_length': 6.052757793764988, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '652993', 'license': 'MIT', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1295, 'n_words': 185, 'path': 'Past/DSS/Neural_Network/02.TensorFlow.ipynb', 'repo_name': 'Moons08/TIL', 'size': '2940'}