njs03332 / ml_study

3 stars 0 forks source link

2023/11/27 ~ 2023/12/07 #77

Open danbi5228 opened 9 months ago

danbi5228 commented 9 months ago
danbi5228 commented 9 months ago

assign roles -s 1127 -c 1 2 3

njs03332 commented 9 months ago
0 1 2
member 한단비 김유리 주선미
chapter 1 2 3
njs03332 commented 9 months ago

16.1.6 가짜 셰익스피어 텍스트를 생성하기

# 다음 글자를 선택하고 입력 텍스트에 추가
def next_char(text, temperature=1):
  X_new = preprocess([text])
  y_proba = model(X_new)[0. -1:, :]
  rescaled_logits = tf.math.log(y_proba) / temperature
  char_id = tf.random.categorical(rescaled_logits, num_samples=1) + 1
  return tokenizer.seqeunces_to_texts(char_id.numpy())[0]

# 위 함수를 반복 호출하여 다음 글자를 얻고 텍스트에 추가
def complete_text(text, n_chars=50, temperature=1):
  for _ in range(n_chars):
    text += next_char(text, temperature)
    return text 
danbi5228 commented 9 months ago

16.1.4 Char-RNN 모델 만들고 훈련하기

model = keras.models.Sequential([
    keras.layers.GRU(128, return_sequences=True, input_shape=[None, max_id],
                                  dropout=0.2, recurrent_dropout=0.2),
    keras.layers.GRU(128, return_sequences=True,
                                  dropout=0.2, recurrent_dropout=0.2),
    keras.layers.TimeDistributed(keras.layers.Dense(max_id, activation="softmax"))
])
model.compile(loss="sparse_categorical_crossentropy", optimizer="adam")
history = model.fit(dataset, epochs=20)

16.1.5 Char-RNN 모델 사용하기


# 텍스트 주입 전 전처리
def preprocess(texts):
    X = np.array(tokenizer.texts_to_sequences(texts)) - 1
    return tf.one_hot(X, max_id)

# model 을 사용한 텍스트 다음 글자 예측하기
X_new = preprocess(["How are yo"])
Y_pred = np.argmax(model(X_new), axis=-1)
tokenizer.sequences_to_texts(Y_pred+1)[0][-1] # ID가 1부터 시작하므로 +1
### output: 'u'  ==>  첫번째 문장의 마지막 글자
givitallugot commented 9 months ago

16.1.7 상태가 있는 RNN