This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 100 | |
for epoch in range(n): | |
epoch +=1 | |
#convert numpy array into torch variable: | |
inputs = Variable(torch.from_numpy(x_train)) | |
labels = Variable(torch.from_numpy(y_train)) | |
#clear gradient w.r.t parameters: | |
optimizer.zero_grad() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Loss function: | |
criterion = nn.MSELoss() | |
#optimizer: | |
learning_rate = 0.01 | |
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#create class: | |
class LinearRegressionModel (nn.Module): | |
def __init__(self, input_size, output_size): | |
super(LinearRegressionModel, self).__init__() | |
self.linear = nn.Linear(1, 1) | |
def forward(self, x): | |
out = self.linear(x) | |
return out | |
#create object (instance from the class) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
y = [2*i + 1 for i in range(11)] #y vector (y = 2x+1) | |
y_train = np.array(y, dtype=np.float32) #convert into numpy array | |
y_train = y_train.reshape(-1, 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = [i for i in range(11)] #x is a set | |
x_train = np.array(x, dtype=np.float32) #translate x into numpy array | |
x_train = x_train.reshape(-1, 1) #rasgape x_train to be an array of shape (11, 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#initialize the ANN | |
classifier = Sequential() | |
#add input layer and first hidden layer | |
classifier.add(Dense(activation="relu", input_dim=11, units=6, kernel_initializer="uniform")) | |
#add second hidden layer | |
classifier.add(Dense(activation="relu", units=6, kernel_initializer="uniform")) | |
#output layer: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Creating the NN: | |
import keras | |
#sequential => initialize the neural network | |
from keras.models import Sequential | |
#Dense => build layers of the neural network | |
from keras.layers import Dense |