Hi, you are nice guy.
i have met the same question when i ran my code, when i run to before_train = criterion(y_pred.squeeze(), test_output)
···
#!/usr/bin/env python
coding: utf-8
import pandas as pd
file_path = “data/shunfenbu1_huice_from_mongo.csv”
df = pd.read_csv(file_path)
columns = [“lunci”, “home_rank”, “away_rank”, “wangji_win”, “wangji_draw”, “wangji_loss”, “home_win”, “home_draw”,
“home_loss”, “away_win”, “away_draw”, “away_loss”, “start_win_sp”, “start_draw_sp”, “start_loss_sp”,
“end_win_sp”, “end_draw_sp”, “end_loss_sp”, “score”, ]
df_new = df.loc[:, columns]
def add_column_with_value_according_spec_col(source):
target = source.copy() # make a copy from source
target[‘result’] = 3 # create a new column with any value
for i, rows in target.iterrows():
home_score, away_score = rows[‘score’].split(“-”)
if home_score == away_score:
rows[‘result’] = 1
if int(home_score) > int(away_score):
rows[‘result’] = 3
if int(home_score) < int(away_score):
rows[‘result’] = 0
return target # return all rows, the last column
df_after_dealed = add_column_with_value_according_spec_col(df_new)
df_after_dealed.drop(‘score’, axis=1, inplace=True)
training = df_after_dealed.iloc[:-10] # select all the rows except the last 10
test = df_after_dealed.iloc[-10:] # elect the last 10 rows
print(training[:3]) # print to check
print(test[:3]) # print to test
normalize the data between 0 and 1
for e in range(len(training.columns)): # iterate for each column
num = max(training.iloc[:, e].max(), test.iloc[:, e].max()) # check the maximum value in each column
if num < 10:
training.iloc[:, e] /= 10
test.iloc[:, e] /= 10
elif num < 100:
training.iloc[:, e] /= 100
test.iloc[:, e] /= 100
elif num < 1000:
training.iloc[:, e] /= 1000
test.iloc[:, e] /= 1000
else:
print(“Error in normalization! Please check!”)
print(training[:3]) # print to check
print(test[:3]) # print to check
training = training.sample(frac=1) # shuffle the training data
test = test.sample(frac=1) # shuffle the test data
print(training[:3]) # print to check
print(test[:3]) # print to check
all rows, all columns except for the last 3 columns
training_input = training.iloc[:, :-1]
all rows, the last 3 columns
training_output = training.iloc[:, -1:]
print(training_output)
all rows, all columns except for the last 3 columns
test_input = test.iloc[:, :-1]
all rows, the last 3 columns
test_output = test.iloc[:, -1:]
print(test_input) #print to check
print(test_output) # print to check
import torch
class Net(torch.nn.Module):
def init(self, input_size, hidden_size):
super(Net, self).init()
self.input_size = input_size
self.hidden_size = hidden_size
self.fc1 = torch.nn.Linear(self.input_size, self.hidden_size)
self.relu = torch.nn.ReLU()
self.fc2 = torch.nn.Linear(self.hidden_size, 1)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
hidden = self.fc1(x)
relu = self.relu(hidden)
output = self.fc2(relu)
output = self.sigmoid(output)
output = output.squeeze(-1) # 根据文档https://blog.csdn.net/weixin_44482737/article/details/125636441 添加此行代码
return output
print(training_input.values)
training_input = torch.FloatTensor(training_input.values)
training_output = torch.FloatTensor(training_output.values)
test_input = torch.FloatTensor(test_input.values)
test_output = torch.FloatTensor(test_output.values)
print(input_size, hidden_size)
input_size = training_input.size()[1] # number of features selected
hidden_size = 30 # number of nodes/neurons in the hidden layer, 不明白这里为什么是30
model = Net(input_size, hidden_size) # create the model
criterion = torch.nn.BCELoss(reduction=‘mean’) # works for binary classification
without momentum parameter
optimizer = torch.optim.SGD(model.parameters(), lr=0.9)
with momentum parameter
optimizer = torch.optim.SGD(model.parameters(), lr=0.9, momentum=0.2)
model.eval()
y_pred = model(test_input)
before_train = criterion(y_pred.squeeze(), test_output)
print(‘Test loss before training’, before_train.item())
print(y_pred.size())
test_output.size()
model.train()
epochs = 5000
errors = []
for epoch in range(epochs):
optimizer.zero_grad()
# Forward pass
y_pred = model(training_input)
# Compute Loss
loss = criterion(y_pred, training_output)
errors.append(loss.item())
print(‘Epoch {}: train loss: {}’.format(epoch, loss.item()))
# Backward pass
loss.backward()
optimizer.step()
model.eval()
y_pred = model(test_input)
after_train = criterion(y_pred, test_output)
import matplotlib.pyplot as plt
import numpy as np
def plotcharts(errors):
errors = np.array(errors)
plt.figure(figsize=(12, 5))
graf02 = plt.subplot(1, 2, 1) # nrows, ncols, index
graf02.set_title(‘Errors’)
plt.plot(errors, ‘-’)
plt.xlabel(‘Epochs’)
graf03 = plt.subplot(1, 2, 2)
graf03.set_title(‘Tests’)
a = plt.plot(test_output.numpy(), ‘yo’, label=‘Real’)
plt.setp(a, markersize=10)
a = plt.plot(y_pred.detach().numpy(), ‘b+’, label=‘Predicted’)
plt.setp(a, markersize=10)
plt.legend(loc=7)
plt.show()
plotcharts(errors)
···