Kaggle competition: ods_class_cs231n
<SOS> metric: multiclassloss Kaggle data source: ods_class_cs231n<find_best_params>
%matplotlib inline
w2v_weights = word2VecModel.vectors<choose_model_class>
torch.__version__
vocab_size, embedding_size = w2v_weights.shape
lstm_model = Sequential()
lstm_model.add(Embedding(input_dim=vocab_size,
output_dim=300,
weights=[w2v_weights],
input_length=700,
mask_zero=True,
trainable=True))
lstm_model.add(Bidirectional(LSTM(128, return_sequences = True)))
 lstm_model.add(Bidirectional(LSTM(128)))
 lstm_model.add(Dense(n_categories, activation='softmax'))
lstm_model
lstm_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'] )<train_model>
from torch.autograd import Variable
history = lstm_model.fit(train_x, train_y, epochs=5, batch_size=256,
verbose=1 )<load_from_csv>
use_gpu = torch.cuda.is_available()
use_gpu
test_data = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv", encoding='utf-8' )<data_type_conversions>
!tar -zxvf.. /input/cifar10-python/cifar-10-python.tar.gz
test_data['keyword'] = test_data['keyword'].astype(str)
 test_data.loc[test_data['keyword'] == 'nan','keyword'] = ''<feature_engineering>
!ls .
test_data['location'] = test_data['location'].astype(str)
 test_data.loc[test_data['location'] == 'nan','location'] = ''<feature_engineering>
transform = transforms.Compose(
 [transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5)) ])
 trainset = torchvision.datasets.CIFAR10(root='.', train=True,
download=False, transform=transform)
 trainloader = torch.utils.data.DataLoader(trainset, batch_size=32,
shuffle=True, num_workers=2)
 testset = torchvision.datasets.CIFAR10(root='.', train=False,
download=False, transform=transform)
 testloader = torch.utils.data.DataLoader(testset, batch_size=32,
shuffle=False, num_workers=2)
 classes =('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck' )
pt_time1 = time.time()
test_data['preprocessed_text'] = test_data['text'].apply(text_preprocessing)
 pt_time2 = time.time()
test_data.head(10 )<define_variables>
def imshow(img):
img = img / 2 + 0.5
npimg = img.numpy()
plt.imshow(np.transpose(npimg,(1, 2, 0)))
 dataiter = iter(trainloader)
 images, labels = dataiter.next()
imshow(torchvision.utils.make_grid(images))
print(' '.join('%5s' % classes[labels[j]] for j in range(32)) )
pt_time2 - pt_time1<feature_engineering>
class Net(nn.Module):
def __init__(self):
super(Net, self ).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
 self.pool = nn.MaxPool2d(2, 2)
 self.conv2 = nn.Conv2d(6, 16, 5)
 self.fc1 = nn.Linear(16 * 5 * 5, 120)
 self.fc2 = nn.Linear(120, 84)
 self.fc3 = nn.Linear(84, 10)
 def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
 x = self.pool(F.relu(self.conv2(x)))
 x = x.view(-1, 16 * 5 * 5)
 x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
 return x
net = Net()
if torch.cuda.is_available() :
net.cuda()
test_data['target'] = 2<prepare_x_and_y>
criterion = nn.CrossEntropyLoss()
if use_gpu:
criterion = criterion.cuda()
optimizer = optim.SGD(net.parameters() , lr=0.001, momentum=0.9 )
def make_lstm_format_output(X):
string1 = X
string1 = pd.DataFrame(string1, columns =['preprocessed_text'])
 string1['target'] = "X"
sequence1 = SequenceIterator(string1, DROP_THRESHOLD, MAX_SEQUENCE_LENGTH)
 cat_dict = {k: v for k, v in zip(sequence1.categories, range(len(sequence1.categories)))}
set_x = []
set_y = []
for w, c in sequence1:
set_x.append(w)
 set_y.append(cat_dict[c])
 set_x = pad_sequences(set_x, maxlen=MAX_SEQUENCE_LENGTH, padding='pre', value=0)
 return set_x<categorify>
for epoch in tqdm_notebook(range(10)) :
running_loss = 0.0
for i, data in tqdm_notebook(enumerate(trainloader, 0)) :
inputs, labels = data
if torch.cuda.is_available() :
inputs, labels = Variable(inputs ).cuda() , Variable(labels ).cuda()
optimizer.zero_grad()
outputs = net(inputs)
 loss = criterion(outputs, labels)
 loss.backward()
optimizer.step()
running_loss += loss.data[0]
if i % 2000 == 1999:
print('[%d, %5d] loss: %.3f' % 
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training' )
test = make_lstm_format_output(test_data )<predict_on_test>
dataiter = iter(testloader)
 images, labels = dataiter.next()
imshow(torchvision.utils.make_grid(images))
print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(32)) )
predicted_values = history.model.predict(test )<prepare_output>
outputs = net(Variable(images ).cuda())if use_gpu else net(Variable(images))
predicted_values = list(np.argmax(predicted_values, axis = 1))<merge>
_, predicted = torch.max(outputs.data, 1)
 print('Predicted: ', ' '.join('%5s' % classes[predicted[j]]
for j in range(32)) )
predictions = pd.merge(test_data[['id']], pd.DataFrame(predicted_values, columns = ['target']),left_index= True, right_index = True )<save_to_csv>
all_pred = np.empty(( 0, 10), float )
predictions.to_csv('Submission.csv',index = False )<import_modules>
for data in tqdm_notebook(testloader):
images, _ = data
if use_gpu:
images = images.cuda()
outputs = net(Variable(images))
curr_pred = F.softmax(outputs ).data.cpu().numpy()
all_pred = np.vstack([all_pred, curr_pred] )
import pandas as pd
import numpy as np
import os
import lightgbm as lgb<load_from_csv>
pd.DataFrame(all_pred, columns=classes ).to_csv('baseline.csv', index_label='id' )