Expected object of scalar type Float but got scalar type Long for argument #2 'mat1' in call to _th_addmm
The error Expected object of scalar type Float but got scalar type Long for argument #2 'mat1' in call to _th_addmm is being displayed after running the code below. 

import numpy as np
import pandas as pd
import nltk  
from nltk.stem import WordNetLemmatizer 
nltk.download('wordnet') 
import re 
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader, TensorDataset


... (created a pandas dataframe containing a 'tweet', 'sentiment' and 47 one-hot bag of words cols)

# create train_target data set containing just the target
train_target = torch.tensor(df_train['sentiment'].values, dtype=torch.long)

# create train predictor features data set containing just the predictor features
train = torch.tensor(df_train.drop(['tweet','sentiment'], axis = 1).values, dtype=torch.long)

# convert to torch tensor and define data loader (train)
train_tensor = TensorDataset(train, train_target) 
trainset = torch.utils.data.DataLoader(train_tensor, batch_size=2, shuffle=False)

# create test_target data set containing just the target
test_target = torch.tensor(df_test['sentiment'].values, dtype=torch.long)

# create test predictor features data set containing just the predictor features
test = torch.tensor(df_test.drop(['tweet','sentiment'], axis = 1).values, dtype=torch.long)

# convert to torch tensor and define data loader (test)
test_tensor = TensorDataset(test, test_target) 
testset = torch.utils.data.DataLoader(test_tensor, batch_size=2, shuffle=False)

input_length = 47

class Net(nn.Module):   # create a new class called Net that inherits from nn's "Module" class

    # initialise our Net class
    def __init__(self):    
        super().__init__()    # run the initialisation function of the nn.Module class (the parent class)
        self.fc1 = nn.Linear(input_length, 768)   # define the first fully connected layer. 
        self.fc2 = nn.Linear(768, 768)    # define the 2nd layer
        self.fc3 = nn.Linear(768, 2)    # output layer. 2 classes so output is size 2

    # define how data will flow through feed-forward network
    def forward(self, x):
        x = F.relu(self.fc1(x))  # x becomes the output of running the 1st layer, after relu activation is applied
        x = F.relu(self.fc2(x))    # x then becomes the output of the 2nd layer...
        x = self.fc3(x)    

        return F.log_softmax(x, dim=1)  # we want a probability distribution across the 2 classes so using softmax
                                        # dim=1 because we want the probabilities across the classes, not
                                        # the batches

# create a Net object called net
net = Net()
print(net)   

# Train the model (net)
optimiser = optim.Adam(net.parameters(),  # net.parameters is everything thats adjustable in our model
                      lr=0.001)  # learning rate 
epochs = 10
for epoch in range(epochs):
    for batch_data in trainset:
        X, y = batch_data  # set X as the input and y as the label
        net.zero_grad() # start with gradients of zero
        output = net(X.view(-1, input_length))  # run the model (could put batch size in instead of -1)
        loss = F.nll_loss(output, y)   # how wrong were we? Calculate loss. 
        loss.backward()    # backpropagate the loss (how much did each weight contribute to the loss?)
        optimiser.step()    # adjust the weights based on the backpropagation
    print('Epoch: ', epoch, ' Loss: ', loss)  



What do I need to do to fix this? I'm trying to create a neural network to do sentiment classification on some text data from tweets. 

The inputs to the nn.Linear layers, and therefore your model, need to be floats, not longs.

You need to change the features to use dtype=torch.float:

train = torch.tensor(df_train.drop(['tweet','sentiment'], axis = 1).values, dtype=torch.float)

test = torch.tensor(df_test.drop(['tweet','sentiment'], axis = 1).values, dtype=torch.float)


That only applies to the input features, whereas your targets need to remain torch.long for the NLL loss.
