last changes and token
This commit is contained in:
Binary file not shown.
File diff suppressed because one or more lines are too long
@@ -39,7 +39,7 @@ class PerceptronModel(object):
|
|||||||
Returns: 1 or -1
|
Returns: 1 or -1
|
||||||
"""
|
"""
|
||||||
"*** YOUR CODE HERE ***"
|
"*** YOUR CODE HERE ***"
|
||||||
result = nn.as_scalar(nn.DotProduct(x, self.w))
|
result = nn.as_scalar(self.run(x))
|
||||||
if (result >= 0):
|
if (result >= 0):
|
||||||
return 1
|
return 1
|
||||||
return -1
|
return -1
|
||||||
@@ -49,12 +49,17 @@ class PerceptronModel(object):
|
|||||||
Train the perceptron until convergence.
|
Train the perceptron until convergence.
|
||||||
"""
|
"""
|
||||||
"*** YOUR CODE HERE ***"
|
"*** YOUR CODE HERE ***"
|
||||||
batch_size = 1
|
batch_size, cont = 1, True
|
||||||
|
while(cont):
|
||||||
|
result = 0
|
||||||
for x, y in dataset.iterate_once(batch_size):
|
for x, y in dataset.iterate_once(batch_size):
|
||||||
print(x)
|
if (self.get_prediction(x) == nn.as_scalar(y)):
|
||||||
print(y)
|
result += 0
|
||||||
result_y = self.run(x)
|
else:
|
||||||
self.w.update(y, .2)
|
self.w.update(x, nn.as_scalar(y))
|
||||||
|
result += 1
|
||||||
|
if (result == 0):
|
||||||
|
cont = False
|
||||||
|
|
||||||
|
|
||||||
class RegressionModel(object):
|
class RegressionModel(object):
|
||||||
@@ -67,21 +72,15 @@ class RegressionModel(object):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
# Initialize your model parameters here
|
# Initialize your model parameters here
|
||||||
"*** YOUR CODE HERE ***"
|
"*** YOUR CODE HERE ***"
|
||||||
model = object.__init__(self)
|
|
||||||
self.get_data_and_monitor = backend.RegressionDataset(model)
|
|
||||||
|
|
||||||
# Remember to set self.learning_rate!
|
# Remember to set self.learning_rate!
|
||||||
# You may use any learning rate that works well for your architecture
|
# You may use any learning rate that works well for your architecture
|
||||||
"*** YOUR CODE HERE ***"
|
"*** YOUR CODE HERE ***"
|
||||||
self.learning_rate = 0.1
|
self.l1b = nn.Parameter(1, 100)
|
||||||
self.hidden_size = 300
|
self.l1 = nn.Parameter(1, 100)
|
||||||
|
self.two = nn.Parameter(100, 1)
|
||||||
|
self.l2b = nn.Parameter(1, 1)
|
||||||
|
|
||||||
self.w1 = nn.Parameter(1, self.hidden_size)
|
self.multiplier = .01
|
||||||
self.w2 = nn.Parameter(self.hidden_size, self.hidden_size)
|
|
||||||
self.w3 = nn.Parameter(self.hidden_size, 1)
|
|
||||||
self.b1 = nn.Parameter(self.hidden_size)
|
|
||||||
self.b2 = nn.Parameter(self.hidden_size)
|
|
||||||
self.b3 = nn.Parameter(1)
|
|
||||||
|
|
||||||
def run(self, x):
|
def run(self, x):
|
||||||
"""
|
"""
|
||||||
@@ -93,45 +92,9 @@ class RegressionModel(object):
|
|||||||
A node with shape (batch_size x 1) containing predicted y-values
|
A node with shape (batch_size x 1) containing predicted y-values
|
||||||
"""
|
"""
|
||||||
"*** YOUR CODE HERE ***"
|
"*** YOUR CODE HERE ***"
|
||||||
self.graph = nn.DataNode(
|
# //composition of layers
|
||||||
[self.w1, self.w2, self.w3, self.b1, self.b2, self.b3])
|
return nn.AddBias(nn.Linear(
|
||||||
|
nn.ReLU(nn.AddBias(nn.Linear(x, self.l1), self.l1b)), self.two), self.l2b)
|
||||||
if y is not None:
|
|
||||||
# At training time, the correct output `y` is known.
|
|
||||||
# Here, you should construct a loss node, and return the nn.Graph
|
|
||||||
# that the node belongs to. The loss node must be the last node
|
|
||||||
# added to the graph.
|
|
||||||
"*** YOUR CODE HERE ***"
|
|
||||||
input_x = nn.Constant(x)
|
|
||||||
input_y = nn.Constant(y)
|
|
||||||
xw1 = nn.MatrixMultiply(self.graph, input_x, self.w1)
|
|
||||||
xw1_plus_b1 = nn.MatrixVectorAdd(self.graph, xw1, self.b1)
|
|
||||||
l1 = nn.ReLU(self.graph, xw1_plus_b1)
|
|
||||||
l1w2 = nn.MatrixMultiply(self.graph, l1, self.w2)
|
|
||||||
l2w2_plus_b2 = nn.MatrixVectorAdd(self.graph, l1w2, self.b2)
|
|
||||||
l2 = nn.ReLU(self.graph, l2w2_plus_b2)
|
|
||||||
l2w3 = nn.MatrixMultiply(self.graph, l2, self.w3)
|
|
||||||
l2w3_plus_b3 = nn.MatrixVectorAdd(self.graph, l2w3, self.b3)
|
|
||||||
loss = nn.SquareLoss(self.graph, l2w3_plus_b3, input_y)
|
|
||||||
|
|
||||||
return self.graph
|
|
||||||
|
|
||||||
else:
|
|
||||||
# At test time, the correct output is unknown.
|
|
||||||
# You should instead return your model's prediction as a numpy array
|
|
||||||
"*** YOUR CODE HERE ***"
|
|
||||||
input_x = nn.Input(self.graph, x)
|
|
||||||
|
|
||||||
xw1 = nn.MatrixMultiply(self.graph, input_x, self.w1)
|
|
||||||
xw1_plus_b1 = nn.MatrixVectorAdd(self.graph, xw1, self.b1)
|
|
||||||
l1 = nn.ReLU(self.graph, xw1_plus_b1)
|
|
||||||
l1w2 = nn.MatrixMultiply(self.graph, l1, self.w2)
|
|
||||||
l2w2_plus_b2 = nn.MatrixVectorAdd(self.graph, l1w2, self.b2)
|
|
||||||
l2 = nn.ReLU(self.graph, l2w2_plus_b2)
|
|
||||||
l2w3 = nn.MatrixMultiply(self.graph, l2, self.w3)
|
|
||||||
l2w3_plus_b3 = nn.MatrixVectorAdd(self.graph, l2w3, self.b3)
|
|
||||||
|
|
||||||
return self.graph.get_output(l2w3_plus_b3)
|
|
||||||
|
|
||||||
def get_loss(self, x, y):
|
def get_loss(self, x, y):
|
||||||
"""
|
"""
|
||||||
@@ -144,12 +107,26 @@ class RegressionModel(object):
|
|||||||
Returns: a loss node
|
Returns: a loss node
|
||||||
"""
|
"""
|
||||||
"*** YOUR CODE HERE ***"
|
"*** YOUR CODE HERE ***"
|
||||||
|
pred_y = self.run(x)
|
||||||
|
loss = nn.SquareLoss(pred_y, y)
|
||||||
|
return loss
|
||||||
|
|
||||||
def train(self, dataset):
|
def train(self, dataset):
|
||||||
"""
|
"""
|
||||||
Trains the model.
|
Trains the model.
|
||||||
"""
|
"""
|
||||||
"*** YOUR CODE HERE ***"
|
"*** YOUR CODE HERE ***"
|
||||||
|
for x, y in dataset.iterate_forever(5):
|
||||||
|
fn_loss = self.get_loss(x, y)
|
||||||
|
if nn.as_scalar(fn_loss) <= 0.000007:
|
||||||
|
break
|
||||||
|
# //get gradients and continuosly udpate weights
|
||||||
|
grad_1, grad_b1, grad_2, grad_b2 = nn.gradients(
|
||||||
|
fn_loss, [self.l1, self.l1b, self.two, self.l2b])
|
||||||
|
self.l1.update(grad_1, -self.multiplier)
|
||||||
|
self.l1b.update(grad_b1, -self.multiplier)
|
||||||
|
self.two.update(grad_2, -self.multiplier)
|
||||||
|
self.l2b.update(grad_b2, -self.multiplier)
|
||||||
|
|
||||||
|
|
||||||
class DigitClassificationModel(object):
|
class DigitClassificationModel(object):
|
||||||
|
|||||||
Reference in New Issue
Block a user