53.pdf

    1. # -*- coding: utf-8 -*-
    2. """lstm
    3. Automatically generated by Colaboratory.
    4. Original file is located at
    5. https://colab.research.google.com/drive/1GX0Rqur8T45MSYhLU9MYWAbycfLH4-Fu
    6. """
    7. !pip install torch
    8. !pip install torchtext
    9. !python -m spacy download en
    10. # K80 gpu for 12 hours
    11. import torch
    12. from torch import nn, optim
    13. from torchtext import data, datasets
    14. print('GPU:', torch.cuda.is_available())
    15. torch.manual_seed(123)
    16. TEXT = data.Field(tokenize='spacy')
    17. LABEL = data.LabelField(dtype=torch.float)
    18. train_data, test_data = datasets.IMDB.splits(TEXT, LABEL)
    19. print('len of train data:', len(train_data))
    20. print('len of test data:', len(test_data))
    21. print(train_data.examples[15].text)
    22. print(train_data.examples[15].label)
    23. # word2vec, glove
    24. TEXT.build_vocab(train_data, max_size=10000, vectors='glove.6B.100d')
    25. LABEL.build_vocab(train_data)
    26. batchsz = 30
    27. device = torch.device('cuda')
    28. train_iterator, test_iterator = data.BucketIterator.splits(
    29. (train_data, test_data),
    30. batch_size = batchsz,
    31. device=device
    32. )
    33. class RNN(nn.Module):
    34. def __init__(self, vocab_size, embedding_dim, hidden_dim):
    35. """
    36. """
    37. super(RNN, self).__init__()
    38. # [0-10001] => [100]
    39. self.embedding = nn.Embedding(vocab_size, embedding_dim)
    40. # [100] => [256]
    41. self.rnn = nn.LSTM(embedding_dim, hidden_dim, num_layers=2,
    42. bidirectional=True, dropout=0.5)
    43. # [256*2] => [1]
    44. self.fc = nn.Linear(hidden_dim*2, 1)
    45. self.dropout = nn.Dropout(0.5)
    46. def forward(self, x):
    47. """
    48. x: [seq_len, b] vs [b, 3, 28, 28]
    49. """
    50. # [seq, b, 1] => [seq, b, 100]
    51. embedding = self.dropout(self.embedding(x))
    52. # output: [seq, b, hid_dim*2]
    53. # hidden/h: [num_layers*2, b, hid_dim]
    54. # cell/c: [num_layers*2, b, hid_di]
    55. output, (hidden, cell) = self.rnn(embedding)
    56. # [num_layers*2, b, hid_dim] => 2 of [b, hid_dim] => [b, hid_dim*2]
    57. hidden = torch.cat([hidden[-2], hidden[-1]], dim=1)
    58. # [b, hid_dim*2] => [b, 1]
    59. hidden = self.dropout(hidden)
    60. out = self.fc(hidden)
    61. return out
    62. rnn = RNN(len(TEXT.vocab), 100, 256)
    63. pretrained_embedding = TEXT.vocab.vectors
    64. print('pretrained_embedding:', pretrained_embedding.shape)
    65. rnn.embedding.weight.data.copy_(pretrained_embedding)
    66. print('embedding layer inited.')
    67. optimizer = optim.Adam(rnn.parameters(), lr=1e-3)
    68. criteon = nn.BCEWithLogitsLoss().to(device)
    69. rnn.to(device)
    70. import numpy as np
    71. def binary_acc(preds, y):
    72. """
    73. get accuracy
    74. """
    75. preds = torch.round(torch.sigmoid(preds))
    76. correct = torch.eq(preds, y).float()
    77. acc = correct.sum() / len(correct)
    78. return acc
    79. def train(rnn, iterator, optimizer, criteon):
    80. avg_acc = []
    81. rnn.train()
    82. for i, batch in enumerate(iterator):
    83. # [seq, b] => [b, 1] => [b]
    84. pred = rnn(batch.text).squeeze(1)
    85. #
    86. loss = criteon(pred, batch.label)
    87. acc = binary_acc(pred, batch.label).item()
    88. avg_acc.append(acc)
    89. optimizer.zero_grad()
    90. loss.backward()
    91. optimizer.step()
    92. if i%10 == 0:
    93. print(i, acc)
    94. avg_acc = np.array(avg_acc).mean()
    95. print('avg acc:', avg_acc)
    96. def eval(rnn, iterator, criteon):
    97. avg_acc = []
    98. rnn.eval()
    99. with torch.no_grad():
    100. for batch in iterator:
    101. # [b, 1] => [b]
    102. pred = rnn(batch.text).squeeze(1)
    103. #
    104. loss = criteon(pred, batch.label)
    105. acc = binary_acc(pred, batch.label).item()
    106. avg_acc.append(acc)
    107. avg_acc = np.array(avg_acc).mean()
    108. print('>>test:', avg_acc)
    109. for epoch in range(10):
    110. eval(rnn, test_iterator, criteon)
    111. train(rnn, train_iterator, optimizer, criteon)

    lstm.ipynb