|
334 | 334 | "train_data, test_data = train_test_split(list(zip(features, labels)))"
|
335 | 335 | ]
|
336 | 336 | },
|
| 337 | + { |
| 338 | + "cell_type": "code", |
| 339 | + "execution_count": null, |
| 340 | + "metadata": {}, |
| 341 | + "outputs": [], |
| 342 | + "source": [ |
| 343 | + "# let's define the pieces that we'll \n", |
| 344 | + "# need for the model\n", |
| 345 | + "\n", |
| 346 | + "# we'll start with an embedding layer\n", |
| 347 | + "# the input size is the size of our vocabulary \n", |
| 348 | + "# (we'll need a row for every word in the input)\n", |
| 349 | + "# and the output size is the dimension that\n", |
| 350 | + "# we'll want for our word vectors\n", |
| 351 | + "embedding = Embedding(num_embeddings=vocab_size, embedding_dim=100)\n", |
| 352 | + "\n", |
| 353 | + "# once we've converted our tokens to \n", |
| 354 | + "# vectors via an embedding layer, we'll\n", |
| 355 | + "# want to run a sequence of these vectors\n", |
| 356 | + "# through an LSTM layer. The input size of\n", |
| 357 | + "# the LSTM is our embedding dimension, \n", |
| 358 | + "# and the hidden dimension can be chosen by us\n", |
| 359 | + "\n", |
| 360 | + "lstm = LSTM(input_size=100, hidden_size=50)\n", |
| 361 | + "\n", |
| 362 | + "# because the forward pass of the LSTM\n", |
| 363 | + "# requires the hidden state from the previous\n", |
| 364 | + "# step as input, we'll have to initialize\n", |
| 365 | + "# the hidden state vectors. this will\n", |
| 366 | + "# need to be done at the beginning of each iteration\n", |
| 367 | + "# before we run any new sequence through the LSTM\n", |
| 368 | + "\n", |
| 369 | + "h0 = torch.zeros(1, 1, 50)\n", |
| 370 | + "c0 = torch.zeros(1, 1, 50)\n", |
| 371 | + "lstm_hidden = h0, c0\n", |
| 372 | + "\n", |
| 373 | + "# we'll be taking the last output of \n", |
| 374 | + "# the LSTM sequence which will be the \n", |
| 375 | + "# same dimension as the hidden layer.\n", |
| 376 | + "# We'll then need a single linear layer \n", |
| 377 | + "# to act as a classifier. The input size \n", |
| 378 | + "# should then be the same as the hidden dim \n", |
| 379 | + "# of the LSTM, and the output size should be \n", |
| 380 | + "# the same as out number of classes for the \n", |
| 381 | + "# classification task\n", |
| 382 | + "\n", |
| 383 | + "linear = Linear(50, label_size)\n", |
| 384 | + "\n", |
| 385 | + "# lastly, we'll want to normalize the final output\n", |
| 386 | + "# to a softmax distribution\n", |
| 387 | + "\n", |
| 388 | + "softmax = LogSoftmax(dim=0)" |
| 389 | + ] |
| 390 | + }, |
337 | 391 | {
|
338 | 392 | "cell_type": "code",
|
339 | 393 | "execution_count": null,
|
|
0 commit comments