rrayy commited on
Commit
5054620
·
1 Parent(s): 63591dc

Changes to be committed: 모델 구조 구상

Browse files
Files changed (1) hide show
  1. Models/Vector2MIDI.py +27 -0
Models/Vector2MIDI.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
2
+ import torch.nn as nn
3
+ import numpy as np
4
+ import torch
5
+
6
+ class Vector2MIDI(nn.Module):
7
+ def __init__(self, input_dim, hidden_dim, n_vocab, dropout=0.2):
8
+ super().__init__() # 부모 클래스 생성자 호출
9
+ self.input_fc = nn.Linear(input_dim, hidden_dim) # 입력 차원에서 은닉 차원으로 변환
10
+
11
+ self.lstm = nn.LSTM(hidden_dim, hidden_dim, num_layers=2, batch_first=True, dropout=dropout) # 과적합 방지 드롭아웃 LSTM
12
+
13
+ self.fc_mid = nn.Linear(hidden_dim, 256)
14
+ self.fc_out = nn.Linear(256, n_vocab)
15
+
16
+ def forward(self, x, lengths):
17
+ # x: [batch, seq_len, input_dim]
18
+ x = self.input_fc(x)
19
+
20
+ # Token 길이가 Midi마다 다르니까 PackedSequence 변환 후 LSTM 처리
21
+ packed = pack_padded_sequence(x, lengths.cpu(), batch_first=True, enforce_sorted=False) # 패딩 변환
22
+ LSTM_out, _ = self.lstm(packed) # LSTM 처리
23
+ padded, _ = pad_packed_sequence(LSTM_out, batch_first=True) # 패딩 복원
24
+
25
+ # 최종 출력
26
+ x = self.fc_mid(padded)
27
+ return self.fc_out(x) # [B, T, vocab_size]