민팽로그

9월 28일 언어 지능 실습 정리 본문

머신러닝&딥러닝

9월 28일 언어 지능 실습 정리

민팽 2021. 9. 28. 22:15
from keras.preprocessing import sequence
from keras.datasets import imdb //불러올 데이터셋
from keras import layers, models

max_features=20000
maxlen=80
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)

x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)

#functional 방식 (layer를 add하는 방식은 sequential)
x = layers.Input((maxlen,))
h = layers.Embedding(max_features, 128)(x)
h = layers.SimpleRNN(128)(h)
y = layers.Dense(1, activation='sigmoid')(h)

model = models.Model(x, y)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
batch_Size=32
epochs=3
model.fit(x_train, y_train,
          batch_size=batch_Size,
          epochs=epochs,
          validation_data=(x_test, y_test))

 

1. imdb.load_data(num_words=max_features) : num_words에 넘겨준 값 안에 해당하는 등장 빈도? 중요도?를 갖는 단어만 불러옴

 

2. layers.Input((maxlen, )) : input 갯수를 maxlen으로 설정

 

3. layers.Embedding(max_features, 128)(x) : input으로 들어온 숫자를 output_dim 차원의 dense vector로 변환

(참고)

https://www.tensorflow.org/api_docs/python/tf/keras/layers/Embedding

 

tf.keras.layers.Embedding  |  TensorFlow Core v2.6.0

Turns positive integers (indexes) into dense vectors of fixed size.

www.tensorflow.org

tf.keras.layers.Embedding(
    input_dim, output_dim, embeddings_initializer='uniform',
    embeddings_regularizer=None, activity_regularizer=None,
    embeddings_constraint=None, mask_zero=False, input_length=None, **kwargs
)

- input_dim : input으로 들어올 정수의 최댓값?  maximum integer index + 1

- output_dim : 임베딩된 벡터의 차수

 

4. models.Model(x, y) : x(입력), y(출력)로 모델 구성

 

5. model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

- binary_crossentropy : 이진 분류를 할 때 사용하는 손실 함수로, output layer의 활성화 함수는 sigmoid 함수여야 함

- cater : 2가지 이상의 분류를 할 때 사용하는 손실 함수로, output layer의 활성화 함수는 softmax 함수여야 함

'머신러닝&딥러닝' 카테고리의 다른 글

9월 30일 언어 지능 실습  (0) 2021.10.01
9월 29일 언어 지능 실습  (0) 2021.09.29
Recurrent Neural Network(RNN)  (0) 2021.09.28
활성화 함수(Activation Function)  (0) 2021.09.28
선형 회귀(Linear Regression)  (0) 2021.09.27
Comments