Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- model = Sequential()
- model.add(Embedding(vocab_size, embedding_size, input_length=55, weights=[pretrained_weights]))
- model.add(Bidirectional(LSTM(units=len(X_train))))
- model.add(Dense(n_classes, activation='softmax'))
- model.compile(loss='categorical_crossentropy',
- optimizer = RMSprop(lr=0.0005),
- metrics=['accuracy'])
- model.fit(np.array(X_train), np.array(y_train), epochs=100, validation_data=(np.array(X_val), np.array(y_val)))
- M = torch.tensor(X_train)
- # Bidirectional recurrent neural network (many-to-one)
- class BiRNN(nn.Module):
- def __init__(self, input_size, hidden_size, num_layers, num_classes):
- super(BiRNN, self).__init__()
- self.hidden_size = hidden_size
- self.num_layers = num_layers
- self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, bidirectional=True)
- self.fc = nn.Linear(hidden_size*2, num_classes) # 2 for bidirection
- def forward(self, x):
- # Set initial states
- h0 = torch.zeros(self.num_layers*2, x.size(0), self.hidden_size).to(device) # 2 for bidirection
- c0 = torch.zeros(self.num_layers*2, x.size(0), self.hidden_size).to(device)
- # Forward propagate LSTM
- out, _ = self.lstm(x, (h0, c0)) # out: tensor of shape (batch_size, seq_length, hidden_size*2)
- # Decode the hidden state of the last time step
- out = self.fc(out[:, -1, :])
- return out
- model = BiRNN(input_size, hidden_size, num_layers, num_classes).to(device)
- # Loss and optimizer
- criterion = nn.CrossEntropyLoss()
- optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
Add Comment
Please, Sign In to add comment