Recent Posts
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 티스토리챌린지
- PytestPluginManager
- tolerated
- JanusGateway
- kotlin
- VARCHAR (1)
- Value too long for column
- Spring Batch
- mp4fpsmod
- JanusWebRTCGateway
- preemption #
- 깡돼후
- taint
- table not found
- 코루틴 빌더
- PersistenceContext
- 오블완
- addhooks
- pytest
- JanusWebRTC
- terminal
- 자원부족
- 개성국밥
- 달인막창
- 헥사고날아키텍처 #육각형아키텍처 #유스케이스
- JanusWebRTCServer
- 겨울 부산
- vfr video
- python
- 코루틴 컨텍스트
Archives
너와 나의 스토리
모두를 위한 딥러닝 - RNN 실습(2) 본문
반응형
모두를 위한 딥러닝 ML lab12-2: RNN - Hi Hello Training 강의 정리&코드
전체 코드: https://github.com/hunkim/DeepLearningZeroToAll/blob/master/lab-12-1-hello-rnn.py
1. RNN model
적합한 것 골라 쓰기
2. Data creation
idx2char = ['h', 'i', 'e', 'l', 'o']
# Teach hello: hihell -> ihello
x_data = [[0, 1, 0, 2, 3, 3]] # hihell
x_one_hot = [[[1, 0, 0, 0, 0], # h 0
[0, 1, 0, 0, 0], # i 1
[1, 0, 0, 0, 0], # h 0
[0, 0, 1, 0, 0], # e 2
[0, 0, 0, 1, 0], # l 3
[0, 0, 0, 1, 0]]] # l 3
y_data = [[1, 0, 2, 3, 3, 4]] # ihello
num_classes = 5
input_dim = 5 # one-hot size
hidden_size = 5 # output from the LSTM. 5 to directly predict one-hot
batch_size = 1 # one sentence
sequence_length = 6 # |ihello| == 6
learning_rate = 0.1
3. Feed to RNN
X = tf.placeholder(
tf.float32, [None, sequence_length, input_dim]) # X one-hot
Y = tf.placeholder(tf.int32, [None, sequence_length]) # Y label
cell = tf.contrib.rnn.BasicLSTMCell(num_units=hidden_size, state_is_tuple=True)
initial_state = cell.zero_state(batch_size, tf.float32)
outputs, _states = tf.nn.dynamic_rnn(
cell, X, initial_state=initial_state, dtype=tf.float32)
4. Cost: sequence_loss
# FC layer
X_for_fc = tf.reshape(outputs, [-1, hidden_size])
# fc_w = tf.get_variable("fc_w", [hidden_size, num_classes])
# fc_b = tf.get_variable("fc_b", [num_classes])
# outputs = tf.matmul(X_for_fc, fc_w) + fc_b
outputs = tf.contrib.layers.fully_connected(
inputs=X_for_fc, num_outputs=num_classes, activation_fn=None)
#reshape out for sequence_loss
outputs = tf.reshape(outputs, [batch_size, sequence_length, num_classes])
weights = tf.ones([batch_size, sequence_length])
sequence_loss = tf.contrib.seq2seq.sequence_loss(
logits=outputs, targets=Y, weights=weights)
loss = tf.reduce_mean(sequence_loss)
train = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
prediction = tf.argmax(outputs, axis=2)
5. session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(50):
l, _ = sess.run([loss, train], feed_dict={X: x_one_hot, Y: y_data})
result = sess.run(prediction, feed_dict={X: x_one_hot})
print(i, "loss:", l, "prediction: ", result, "true Y: ", y_data)
# print char using dic
result_str = [idx2char[c] for c in np.squeeze(result)]
print("\tPrediction str: ", ''.join(result_str))
반응형
'Data Analysis > Machine learning' 카테고리의 다른 글
모두를 위한 딥러닝 - RNN 실습(4) (0) | 2019.08.07 |
---|---|
모두를 위한 딥러닝 - RNN 실습(3) (0) | 2019.08.07 |
모두를 위한 딥러닝 - RNN 실습(1) (0) | 2019.08.07 |
RNN - 입력과 출력 시퀀스 (0) | 2019.08.07 |
머신러닝 기초 지식 - Hands On (0) | 2019.08.01 |
Comments