#!/usr/bin/env python # coding: utf-8 # In[90]: import torch as t from torch import nn import torch.nn.functional as F import pandas as pd import sklearn as sc from sklearn.feature_extraction import DictVectorizer as DV import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.family'] = 'serif' plt.rcParams['font.serif'] = 'FreeSerif' plt.rcParams['lines.linewidth'] = 2 plt.rcParams['lines.markersize'] = 12 plt.rcParams['xtick.labelsize'] = 24 plt.rcParams['ytick.labelsize'] = 24 plt.rcParams['legend.fontsize'] = 24 plt.rcParams['axes.titlesize'] = 26 plt.rcParams['axes.labelsize'] = 24 # In[91]: columnNames = ['WKingLetter','WKingPos', 'WRookLetter', 'WRookPos', 'BKingLetter', 'BKingPos', 'optimal depth-of-win for White in 0 to 16 moves/draw'] data = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/chess/king-rook-vs-king/krkopt.data', names = columnNames, header=None) # In[92]: data.head() # In[93]: data.shape[0] # In[94]: X = data.drop(['optimal depth-of-win for White in 0 to 16 moves/draw'], axis=1) X.head() # In[95]: strings = ['WKingLetter', 'WRookLetter', 'BKingLetter'] for s in strings: X[s].replace(to_replace=dict(a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8), inplace=True) # In[96]: X.head() # In[97]: target = data['optimal depth-of-win for White in 0 to 16 moves/draw'] target.replace(to_replace=dict(zero=0, one=1, two=2, three=3, four=4, five=5, six=6, seven=7, eight=8, nine=9, ten=10, eleven=11, twelve=12, thirteen=13, fourteen=14, fifteen=15, sixteen=16, draw=17), inplace=True) target.head() # In[98]: model = nn.Sequential() model.add_module('l1', nn.Linear(6, 1)) model.add_module('l2', nn.Sigmoid()) print("Weight shapes:", [w.shape for w in model.parameters()]) # In[ ]: X_data_train, X_data_test, y_train, y_test = train_test_split(X_data, y_data, test_size=0.5, random_state=k) x = torch.tensor(X_train[:3], dtype=torch.float32) y = torch.tensor(y_train[:3], dtype=torch.float32) y_predicted = model(x)[:, 0] y_predicted