Dimension problem

kosicsd12176: <pre><code class="lang-auto">#!/usr/bin/env python
# coding: utf-8

# In[125]:


import torch
import numpy as np
import os
import pandas as pd
import glob
from collections import Counter
from string import punctuation



#import train dataset
df = pd.read_csv('train_dataset.csv')
df2 = pd.read_csv('test_dataset.csv')
frames = [df ,df2]
train = pd.concat(frames)
train = train[train.author != 'unknown']


#create train numpy array
train = np.array(train)
total_counts = Counter()



#split numpy array to text and labels
train_text= train[:,0]
train_labels = train[:,1]




#create vollector or every word
train_text=np.array(train_text)
for i in range(len(train_text)):
        for word in train_text[i]:   
            total_counts[word] += 1



# create new list and remove new lines
import re
text_train = []
for c in train_text:
    text_train.append(re.sub(r"[\n\t]*", "", c))



#create new list with lowercasses and remove punctuation
train_text = []
for c in text_train:
    if c not in punctuation:
        c = c.lower()
        sentence = re.sub('[^a-zA-Z]', ' ', c)

        # Single character removal
        sentence = re.sub(r"\s+[a-zA-Z]\s+", ' ', sentence)

        # Removing multiple spaces
        sentence = re.sub(r'\s+', ' ', sentence)
        train_text.append(sentence)
        




docs_split = []
for c in train_text:
    docs_split.append(c)


all_text = ''.join([c for c in docs_split])




words = all_text.split()



## Build a dictionary that maps words to integers
counts = Counter(words)
vocab = sorted(counts, key=counts.get, reverse=True)
vocab_to_int = {word: ii for ii, word in enumerate(vocab, 1)}

vocab_to_int.update( {'bann' : 9523} )
vocab_to_int.update( {'brisk' : 9524} )
vocab_to_int.update( {'t' : 9525} )
vocab_to_int.update( {'zawame' : 9526} )
vocab_to_int.update( {'proudly' : 9527} )
vocab_to_int.update( {'fascination' : 9528} )
vocab_to_int.update( {'arrest' : 9529} )
vocab_to_int.update( {'fortunate' : 9530} )
vocab_to_int.update( {'defiance' : 9531} )
vocab_to_int.update( {'meter' : 9532} )
vocab_to_int.update( {'smothered' : 9533} )
vocab_to_int.update( {'clavicle' : 9534} )
vocab_to_int.update( {'berates' : 9535} )
vocab_to_int.update( {'nomadic' : 9535} )
vocab_to_int.update( {'hairdryer' : 9536} )
vocab_to_int.update( {'exiting' : 9537} )
vocab_to_int.update( {'nomadic' : 9535} )
vocab_to_int.update( {'nomadic' : 9535} )




## use the dict to tokenize each review in reviews_split
## store the tokenized reviews in reviews_ints
docs_ints = []
for doc in docs_split:
    docs_ints.append([vocab_to_int[word] for word in doc.split()])



print('Unique words: ', len((vocab_to_int)))
print()

# print tokens in first review
print('Tokenized docs: \n', docs_ints[2])




encoded_labels = []
for label in train_labels:
    if(label == 'candidate00001'):
        encoded_labels.append(1)
    elif(label == 'candidate00002'):
        encoded_labels.append(2)
    elif(label == 'candidate00003'):
        encoded_labels.append(3)
    elif(label == 'candidate00004'):
        encoded_labels.append(4)
    elif(label == 'candidate00005'):
        encoded_labels.append(5)
    elif(label == 'candidate00006'):
        encoded_labels.append(6)
    elif(label == 'candidate00007'):
        encoded_labels.append(7)
    elif(label == 'candidate00008'):
        encoded_labels.append(8)
    elif(label == 'candidate00009'):
        encoded_labels.append(9)
    elif(label == 'candidate00010'):
        encoded_labels.append(10)
    elif(label == 'candidate00011'):
        encoded_labels.append(11)
    elif(label == 'candidate00012'):
        encoded_labels.append(12)
    elif(label == 'candidate00013'):
        encoded_labels.append(13)
    elif(label == 'candidate00014'):
        encoded_labels.append(14)
    elif(label == 'candidate00015'):
        encoded_labels.append(15)
    elif(label == 'candidate00016'):
        encoded_labels.append(16)
    elif(label == 'candidate00017'):
        encoded_labels.append(17)
    elif(label == 'candidate00018'):
        encoded_labels.append(18)
    elif(label == 'candidate00019'):
        encoded_labels.append(19)
    elif(label == 'candidate00020'):
        encoded_labels.append(20)



encoded_labels=np.array(encoded_labels)



# outlier review stats
review_lens = Counter([len(x) for x in docs_ints])
print("Zero-length reviews: {}".format(review_lens[0]))
print("Maximum review length: {}".format(max(review_lens)))



def pad_features(docs_ints, seq_length):
    ''' Return features of review_ints, where each review is padded with 0's 
        or truncated to the input seq_length.
    '''
    
    # getting the correct rows x cols shape
    features = np.zeros((len(docs_ints), seq_length), dtype=int)

    # for each review, I grab that review and 
    for i, row in enumerate(docs_ints):
        features[i, -len(row):] = np.array(row)[:seq_length]
    
    return features





# Test your implementation!

seq_length = 500

features = pad_features(docs_ints, seq_length=seq_length)

## test statements - do not change - ##
assert len(features)==len(docs_ints), "Your features should have as many rows as reviews."
assert len(features[0])==seq_length, "Each feature row should contain seq_length values."





docs_ints = np.array(docs_ints)




docs_ints.shape





split_idx = 140
train_x, remaining_x = features[:split_idx], features[split_idx:]
train_y, remaining_y = encoded_labels[:split_idx], encoded_labels[split_idx:]

test_idx = int(len(remaining_x)*0.5)
val_x, test_x = remaining_x[:test_idx], remaining_x[test_idx:]
val_y, test_y = remaining_y[:test_idx], remaining_y[test_idx:]

## print out the shapes of your resultant feature data
print("\t\t\tFeature Shapes:")
print("Train set: \t\t{}".format(train_x.shape), 
      "\nValidation set: \t{}".format(val_x.shape),
      "\nTest set: \t\t{}".format(test_x.shape))




import torch
from torch.utils.data import TensorDataset, DataLoader

# create Tensor datasets
train_data = TensorDataset(torch.from_numpy(train_x), torch.from_numpy(train_y))
valid_data = TensorDataset(torch.from_numpy(val_x), torch.from_numpy(val_y))
test_data = TensorDataset(torch.from_numpy(test_x), torch.from_numpy(test_y))

# dataloaders
batch_size = 5

# make sure the SHUFFLE your training data
train_loader = DataLoader(train_data, shuffle=True, batch_size=batch_size)
valid_loader = DataLoader(valid_data, shuffle=True, batch_size=batch_size)
test_loader = DataLoader(test_data, shuffle=True, batch_size=batch_size)





# obtain one batch of training data
dataiter = iter(train_loader)
sample_x, sample_y = dataiter.next()

print('Sample input size: ', sample_x.size()) # batch_size, seq_length
print('Sample input: \n', sample_x)
print()
print('Sample label size: ', sample_y.size()) # batch_size
print('Sample label: \n', sample_y)




# First checking if GPU is available
train_on_gpu=torch.cuda.is_available()

if(train_on_gpu):
    print('Training on GPU.')
else:
    print('No GPU available, training on CPU.')




import torch.nn as nn

class SentimentRNN(nn.Module):
    """
    The RNN model that will be used to perform Sentiment analysis.
    """

    def __init__(self, vocab_size, output_size, embedding_dim, hidden_dim, n_layers, drop_prob=0.5):
        """
        Initialize the model by setting up the layers.
        """
        super(SentimentRNN, self).__init__()

        self.output_size = output_size
        self.n_layers = n_layers
        self.hidden_dim = hidden_dim
        
        # embedding and LSTM layers
        self.embedding = nn.Embedding(vocab_size, embedding_dim)
        self.lstm = nn.LSTM(embedding_dim, hidden_dim, n_layers, 
                            dropout=drop_prob, batch_first=True)
        
        # dropout layer
        self.dropout = nn.Dropout(0.3)
        
        # linear and sigmoid layers
        self.fc = nn.Linear(hidden_dim, output_size)
        self.sig = nn.Sigmoid()
        

    def forward(self, x, hidden):
        """
        Perform a forward pass of our model on some input and hidden state.
        """
        batch_size = x.size(0)

        # embeddings and lstm_out
        x = x.long()
        embeds = self.embedding(x)
        lstm_out, hidden = self.lstm(embeds, hidden)
    
        # stack up lstm outputs
        lstm_out = lstm_out.contiguous().view(-1, self.hidden_dim)
        
        # dropout and fully-connected layer
        out = self.dropout(lstm_out)
        out = self.fc(out)
        # sigmoid function
        sig_out = self.sig(out)
        
        # reshape to be batch_size first
        sig_out = sig_out.view(batch_size, -1)
        sig_out = sig_out[:, -1] # get last batch of labels
        
        # return last sigmoid output and hidden state
        return sig_out, hidden
    
    
    def init_hidden(self, batch_size):
        ''' Initializes hidden state '''
        # Create two new tensors with sizes n_layers x batch_size x hidden_dim,
        # initialized to zero, for hidden state and cell state of LSTM
        weight = next(self.parameters()).data
        
        if (train_on_gpu):
            hidden = (weight.new(self.n_layers, batch_size, self.hidden_dim).zero_().cuda(),
                  weight.new(self.n_layers, batch_size, self.hidden_dim).zero_().cuda())
        else:
            hidden = (weight.new(self.n_layers, batch_size, self.hidden_dim).zero_(),
                      weight.new(self.n_layers, batch_size, self.hidden_dim).zero_())
        
        return hidden




# Instantiate the model w/ hyperparams
vocab_size = len(vocab_to_int)+1 # +1 for the 0 padding + our word tokens
output_size = 20
embedding_dim = 400
hidden_dim = 256
n_layers = 2

net = SentimentRNN(vocab_size, output_size, embedding_dim, hidden_dim, n_layers)

print(net)




# loss and optimization functions
lr=0.001

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=lr)




epochs = 4 # 3-4 is approx where I noticed the validation loss stop decreasing

counter = 0
print_every = 100
clip=5 # gradient clipping

# move model to GPU, if available
if(train_on_gpu):
    net.cuda()

net.train()
# train for some number of epochs
for e in range(epochs):
    # initialize hidden state
    h = net.init_hidden(batch_size)

    # batch loop
    for inputs, labels in train_loader:
        counter += 1

        if(train_on_gpu):
            inputs, labels = inputs.cuda(), labels.cuda()

        # Creating new variables for the hidden state, otherwise
        # we'd backprop through the entire training history
        h = tuple([each.data for each in h])

        # zero accumulated gradients
        net.zero_grad()

        # get the output from the model
        output, h = net(inputs, h)
        print(output.unsqueeze(0))
        print(labels)
        # calculate the loss and perform backprop
        loss = criterion(output.squeeze(), labels)
        loss.backward()
        # `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
        nn.utils.clip_grad_norm_(net.parameters(), clip)
        optimizer.step()

        # loss stats
        if counter % print_every == 0:
            # Get validation loss
            val_h = net.init_hidden(batch_size)
            val_losses = []
            net.eval()
            for inputs, labels in valid_loader:

                # Creating new variables for the hidden state, otherwise
                # we'd backprop through the entire training history
                val_h = tuple([each.data for each in val_h])

                if(train_on_gpu):
                    inputs, labels = inputs.cuda(), labels.cuda()

                output, val_h = net(inputs, val_h)
                val_loss = criterion(output.unsqueeze(0), labels.float())

                val_losses.append(val_loss.item())

            net.train()
            print("Epoch: {}/{}...".format(e+1, epochs),
                  "Step: {}...".format(counter),
                  "Loss: {:.6f}...".format(loss.item()),
                  "Val Loss: {:.6f}".format(np.mean(val_losses)))






</code></pre>
<p>IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)</p>

ptrblck: <p>This error is raised, if you try to index a 1-dimensional tensor with any other index than 0 or -1:</p>
<pre><code class="lang-python">x = torch.randn(10)
x.index_select(0, torch.tensor(0)) # works
x.index_select(-1, torch.tensor(0)) # works
x.index_select(1, torch.tensor(0)) # yields your error
</code></pre>