application_vnd.jupyter.stderr_output_1.png
from collections import Counter

from sklearn import preprocessing

from torch import nn

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import torch

csv = pd.read_csv('/kaggle/input/trip-advisor-hotel-reviews/tripadvisor_hotel_reviews.csv')
csv = csv.rename(columns={'Review': 'review', 'Rating': 'rating'})
csv['rating'] = csv['rating'] - 1

reviews = csv['review'].tolist()
ratings = csv['rating'].tolist()
features = {}

top200 = sorted(features, key=features.get, reverse=True)[:1000]
top400 = sorted(features, key=features.get, reverse=True)[:400]

train_test_split = 0.8
reviews_train = reviews[:round(train_test_split * len(reviews))]
reviews_test = reviews[round(train_test_split * len(reviews)):]
ratings_train = ratings[:round(train_test_split * len(ratings))]
ratings_test = ratings[round(train_test_split * len(ratings)):]

M200 = []
M400 = []
for review in reviews_train:
    c = Counter([review[i:i + 3] for i in range(len(review) - 2)])
    M200.append([c[g3] for g3 in top200])
    M400.append([c[g3] for g3 in top400])
M200 = np.array(M200)
M400 = np.array(M400)

M200_test = []
M400_test = []
for review in reviews_test:
    c = Counter([review[i:i + 3] for i in range(len(review) - 2)])
    M200_test.append([c[g3] for g3 in top200])
    M400_test.append([c[g3] for g3 in top400])
M200_test = np.array(M200_test)
M400_test = np.array(M400_test)

scl = preprocessing.StandardScaler()
M200_s = torch.tensor(scl.fit_transform(M200))
M400_s = torch.tensor(scl.fit_transform(M400))
M200_test_s = torch.tensor(scl.fit_transform(M200_test))
M400_test_s = torch.tensor(scl.fit_transform(M400_test))
ratings_train = torch.tensor(ratings_train).long()
ratings_test = torch.tensor(ratings_test).long()

model200 = nn.Sequential(nn.Linear(1000, 512), nn.ReLU(), nn.Dropout(0.8), nn.Linear(512, 32), nn.ReLU(), nn.Dropout(0.8), nn.Linear(32, 5))
learning_rate = 0.001
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model200.parameters(), lr=learning_rate)

epochs = 200
for epoch in range(epochs):
    model200.train()
    pred = model200(M200_s.float())
    loss = loss_fn(pred, ratings_train)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    model200.eval()
    if epoch % 30 == 0:
        train = (model200(M200_s.data.float()).argmax(1) == ratings_train).sum().item() / len(M200)
        test = (model200(M200_test_s.data.float()).argmax(1) == ratings_test).sum().item() / len(M200_test)
        print('Epoch {}.'.format(epoch + 1))
        print('Loss: {:.3f}'.format(loss.item()))
        print('Train Score: {:.3f}'.format(train))
        print('Test Score: {:.3f}\n'.format(test))