Home The Little Machine That Wanted to Learn
Post
Cancel

The Little Machine That Wanted to Learn

Challenge Information

  • Advent of Cyber Day 14
  • THM link here

Explanation

Today’s topic is about Machine learning which I have no idea. There are alot of theory so make sure to read those yourself.

Question 1: What is the other term given for Artificial Intelligence or the subset of AI meant to teach computers how humans think or nature works?

The answer is Machine Learning

Question 2: What ML structure aims to mimic the process of natural selection and evolution?

The answer is Genetic Algorithm

Question 3: What is the name of the learning style that makes use of labelled data to train an ML structure?

The answer is supervised learning

Question 4: What is the name of the layer between the Input and Output layers of a Neural Network?

The answer is Hidden Layer

Question 5: What is the name of the process used to provide feedback to the Neural Network on how close its prediction was?

The answer is Back-Propagation

Question 6: What is the value of the flag you received after achieving more than 90% accuracy on your submitted predictions?

To get the prediction, modify the detector.py script into the script below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#These are the imports that we need for our Neural Network
#Numpy is a powerful array and matrix library used to format our data
import numpy as np
#Pandas is a machine learning library that also allows for reading and formatting data structures
import pandas as pd
#This will be used to split our data
from sklearn.model_selection import train_test_split
#This is used to normalize our data
from sklearn.preprocessing import StandardScaler
#This is used to encode our text data to integers
from sklearn.preprocessing import LabelEncoder
#This is our Multi-Layer Perceptron Neural Network
from sklearn.neural_network import MLPClassifier

#These are the colour labels that we will convert to int
colours = ["Red", "Blue", "Green", "Yellow", "Pink", "Purple", "Orange"]


#Read the training and testing data files
training_data = pd.read_csv("training_dataset.csv")
training_data.head()

testing_data = pd.read_csv("testing_dataset.csv")
testing_data.head()

#The Neural Network cannot take Strings as input, therefore we will encode the strings as integers
encoder = LabelEncoder()
encoder.fit(training_data["Colour Scheme"])
training_data["Colour Scheme"] = encoder.transform(training_data["Colour Scheme"])
testing_data["Colour Scheme"] = encoder.transform(testing_data["Colour Scheme"])



#Read our training data from the CSV file.
#First we read the data we will train on
X = np.asanyarray(training_data[['Height','Width','Length','Colour Scheme','Maker Elf ID','Checker Elf ID']])
#Now we read the labels of our training data
y = np.asanyarray(training_data['Defective'].astype('int'))

#Read our testing data
test_X = np.asanyarray(testing_data[['Height','Width','Length','Colour Scheme','Maker Elf ID','Checker Elf ID']])


###### INSERT DATASET SPLIT CODE HERE ######
train_X, validate_X, train_y, validate_y = train_test_split(X, y, test_size=0.2)

#Now we need to split our training dataset here

print ("Sample of our data:")
print("Features:\n{}\nDefective?:\n{}".format(train_X[:3], train_y[:3]))

###### INSERT NORMALISATION CODE HERE ######
scaler = StandardScaler()
scaler.fit(train_X)
 
train_X = scaler.transform(train_X)
validate_X = scaler.transform(validate_X)
test_X = scaler.transform(test_X)

#Now we normalize our dataset

print ("Sampe of our data after normalization:")
print("Features:\n{}\nDefective?:\n{}".format(train_X[:3], train_y[:3]))


##### INSERT CLASSIFIER CODE HERE ######

clf = MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(15, 2), max_iter=10000)

print ("Starting to training our Neural Network")

###### INSERT CLASSIFIER TRAINING CODE HERE ######

clf.fit(train_X, train_y)


###### INSERT CLASSIFIER VALIDATION PREDICTION CODE HERE #######
y_predicted = clf.predict(validate_X)


#This function tests how well your Neural Network performs with the validation dataset
count_correct = 0
count_incorrect = 0
for x in range(len(y_predicted)):

    if (y_predicted[x] == validate_y[x]):
        count_correct += 1
    else:
        count_incorrect += 1

print ("Training has been completed, validating neural network now....")
print ("Total Correct:\t\t" + str(count_correct))
print ("Total Incorrect:\t" + str(count_incorrect))

accuracy =  ((count_correct * 1.0) / (1.0 * (count_correct + count_incorrect)))

print ("Network Accuracy:\t" + str(accuracy * 100) + "%")

print ("Now we will predict the testing dataset for which we don't have the answers for...")

###### INSERT CLASSIFIER TESTING PREDICTION CODE HERE ######

y_test_predictions = clf.predict(test_X)

#This function will save your predictions to a textfile that can be uploaded for scoring
print ("Saving predictions to a file")

output = open("predictions.txt", 'w')

for value in y_test_predictions:
    output.write(str(value) + "\n")

print ("Predictions are saved, this file can now be uploaded to verify your Neural Network")
output.close()

After modifying it, upload the result into the given link and you should have a flag.

flag:THM{Neural.Networks.are.Neat!}

Things I learned from the challenge

  • Machine Learning
This post is licensed under CC BY 4.0 by the author.

To the Pots, Through the Walls

Bricks Login 1