<jupyter_start><jupyter_text>Conda Install Package From requirements.txt Run the following command in terminal:while read requirement; do conda install --yes $requirement; done < requirements.txt  9. Perceptron Trick<jupyter_code>x1 = 1
x2 = 1
l_rate = 0.1

a, b, c = 3, 4, -10

cnt = 0

while(True):
    v = a*x1 + b*x2 + c
    print(f'Step {cnt}\n--------------')
    print(f'a = {a*l_rate}, b = {b*l_rate}, c = {c*l_rate}')
    print('V:', v)
    y = 1 if v>=0 else 0
    if y:
        break;
    cnt += 1
    a += x1*l_rate
    b += x2*l_rate
    c += l_rate

cnt<jupyter_output>Step 0
--------------
a = 0.30000000000000004, b = 0.4, c = -1.0
V: -3
Step 1
--------------
a = 0.31000000000000005, b = 0.41, c = -0.9900000000000001
V: -2.700000000000001
Step 2
--------------
a = 0.32000000000000006, b = 0.41999999999999993, c = -0.9800000000000001
V: -2.4000000000000012
Step 3
--------------
a = 0.33000000000000007, b = 0.42999999999999994, c = -0.9700000000000002
V: -2.1000000000000014
Step 4
--------------
a = 0.3400000000000001, b = 0.4399999999999999, c = -0.9600000000000002
V: -1.8000000000000025
Step 5
--------------
a = 0.3500000000000001, b = 0.44999999999999984, c = -0.9500000000000002
V: -1.5000000000000036
Step 6
--------------
a = 0.3600000000000001, b = 0.4599999999999998, c = -0.9400000000000003
V: -1.2000000000000028
Step 7
--------------
a = 0.3700000000000001, b = 0.46999999999999975, c = -0.9300000000000003
V: -0.9000000000000039
Step 8
--------------
a = 0.3800000000000001, b = 0.47999999999999976, c = -0.9200000000000004
V: -0.600000000000005
S[...]<jupyter_text>10. Perceptron Algorithm<jupyter_code>import numpy as np
# Setting the random seed, feel free to change it and see different solutions.
np.random.seed(42)

def stepFunction(t):
    if t >= 0:
        return 1
    return 0

def prediction(X, W, b):
    return stepFunction((np.matmul(X,W)+b)[0])

# TODO: Fill in the code below to implement the perceptron trick.
# The function should receive as inputs the data X, the labels y,
# the weights W (as an array), and the bias b,
# update the weights and bias W, b, according to the perceptron algorithm,
# and return W and b.
def perceptronStep(X, y, W, b, learn_rate = 0.01):
    for i in range(len(X)):
        y1 = prediction(X[i], W, b)
        for j in range(len(W)):
            W[j] += (y[i] - y1)*X[i][j]*learn_rate
        b += (y[i] - y1)*learn_rate
    return W, b
    
# This function runs the perceptron algorithm repeatedly on the dataset,
# and returns a few of the boundary lines obtained in the iterations,
# for plotting purposes.
# Feel free to play with the learning rate and the num_epochs,
# and see your results plotted below.
def trainPerceptronAlgorithm(X, y, learn_rate = 0.01, num_epochs = 25):
    x_min, x_max = min(X.T[0]), max(X.T[0])
    y_min, y_max = min(X.T[1]), max(X.T[1])
    W = np.array(np.random.rand(2,1))
    b = np.random.rand(1)[0] + x_max
    # These are the solution lines that get plotted below.
    boundary_lines = []
    for i in range(num_epochs):
        # In each epoch, we apply the perceptron step.
        W, b = perceptronStep(X, y, W, b, learn_rate)
        boundary_lines.append((-W[0]/W[1], -b/W[1]))
    return boundary_lines<jupyter_output><empty_output><jupyter_text>12. Softmax<jupyter_code>import numpy as np

# Write a function that takes as input a list of numbers, and returns
# the list of values given by the softmax function.
def softmax(L):
    soft_output = []
    exp_l = np.exp(L)
    sum_exp_l = sum(exp_l)
    for item in exp_l:
        soft_output.append(item*1.0/sum_exp_l)
    return soft_output
    
    # denominator = 0
    # for item in L:
    #     denominator += np.exp(item)
    # for item in L:
    #     soft_output.append(np.exp(item)/denominator)
    # return soft_output<jupyter_output><empty_output><jupyter_text>20. Cross-Entropy 2<jupyter_code>import numpy as np

# Write a function that takes as input two lists Y, P,
# and returns the float corresponding to their cross-entropy.
def cross_entropy(Y, P):
    # Entropy and the likely to happen is inversly related
    
    Y = np.float_(Y)
    P = np.float_(P)
    return -np.sum(Y * np.log(P) + (1 - Y) * np.log(1 - P))
    
    
    # entropy = 0
    # for y, p in zip(Y, P):
    #     entropy += y*np.log(p) + (1-y)*np.log(1-p)
    # return -entropy
import numpy as np

a = np.array([[3,4,5], [6,7,8]])
b = np.array([1,2, 3])
print(np.dot(a,b))
# print(np.dot(b,a))
# print(np.matmul(a, b))
# print(np.matmul(b, a))
np.sum(a*b, axis=1)<jupyter_output>[26 44]<jupyter_text>26. Notebook: Gradient Descent<jupyter_code># Activation (sigmoid) function
def sigmoid(x):
    return 1/(1+np.exp(-x))

# Output (prediction) formula
def output_formula(features, weights, bias):
    val = np.dot(features, weights) + bias
    y_hat = sigmoid(val)
    return y_hat

# Error (log-loss) formula
def error_formula(y, output):
    return -y*np.log(output) - (1-y)*np.log(1-output)

# Gradient descent step
def update_weights(x, y, weights, bias, learnrate):
    y_hat = output_formula(x, weights, bias)
    error = y-y_hat
    weights += learnrate*error*x
    bias += learnrate*error
    return weights, bias<jupyter_output><empty_output><jupyter_text>31. Neural Network Architecture<jupyter_code>print(sigmoid(2*0.4 + 6*0.6 -2 ))
print(sigmoid(3*0.4 + 5*0.6 -2.2 ))
print(sigmoid(5*0.4 + 4*0.6 -3 ))<jupyter_output>0.9168273035060777
0.8807970779778823
0.8021838885585818<jupyter_text>35. Notebook: Analyzing Student Data  One-hot encoding the rank<jupyter_code># TODO:  Make dummy variables for rank
one_hot_data = pd.concat([data, pd.get_dummies(data['rank'], prefix='rank')], axis=1)

# TODO: Drop the previous rank column
one_hot_data = one_hot_data.drop(['rank'], axis=1)

# Print the first 10 rows of our data
one_hot_data[:10]<jupyter_output><empty_output><jupyter_text>Scaling the data<jupyter_code># Making a copy of our data
processed_data = one_hot_data[:]

# TODO: Scale the columns
processed_data['gpa'] = processed_data['gpa']/4.0
processed_data['gre'] = processed_data['gre']/800.0

# Printing the first 10 rows of our procesed data
processed_data[:10]<jupyter_output><empty_output><jupyter_text>Backpropagate the error<jupyter_code># TODO: Write the error term formula
def error_term_formula(x, y, output):
    return (y - output) * sigmoid_prime(x)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15004703668861713, 'fraction_numerical': 0.11665098777046096, 'mean_word_length': 3.3872077028885834, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 32, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '78041', 'license': 'MIT', 'n_tokens_mistral': 2911, 'n_tokens_neox': 2389, 'n_words': 651, 'path': 'introduction_to_neural_networks.ipynb', 'repo_name': 'MhmdRyhn/Intro-to-Deep-Learning-with-PyTorch', 'size': '6378'}