diff --git a/Exercise1/exercise1.ipynb b/Exercise1/exercise1.ipynb index 38a9faed..6f921a48 100755 --- a/Exercise1/exercise1.ipynb +++ b/Exercise1/exercise1.ipynb @@ -371,7 +371,7 @@ "\n", "As you perform gradient descent to learn minimize the cost function $J(\\theta)$, it is helpful to monitor the convergence by computing the cost. In this section, you will implement a function to calculate $J(\\theta)$ so you can check the convergence of your gradient descent implementation. \n", "\n", - "Your next task is to complete the code for the function `computeCost` which computes $J(\\theta)$. As you are doing this, remember that the variables $X$ and $y$ are not scalar values. $X$ is a matrix whose rows represent the examples from the training set and $y$ is a vector whose each elemennt represent the value at a given row of $X$.\n", + "Your next task is to complete the code for the function `computeCost` which computes $J(\\theta)$. As you are doing this, remember that the variables $X$ and $y$ are not scalar values. $X$ is a matrix whose rows represent the examples from the training set and $y$ is a vector whose each element represent the value at a given row of $X$.\n", "" ] }, diff --git a/Exercise1/utils.py b/Exercise1/utils.py index d0c909d5..b92e3cf5 100755 --- a/Exercise1/utils.py +++ b/Exercise1/utils.py @@ -19,7 +19,9 @@ def __init__(self): 'Computing Cost (for multiple variables)', 'Gradient Descent (for multiple variables)', 'Normal Equations'] - super().__init__('linear-regression', part_names) + part_names_key = ['DCRbJ', 'BGa4S', 'b65eO', 'BbS8u', 'FBlE2', 'RZAZC', '7m5Eu'] + assignment_key = 'UkTlA-FyRRKV5ooohuwU6A' + super().__init__('linear-regression', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 8): diff --git a/Exercise2/utils.py b/Exercise2/utils.py index 7c52dbe4..8e5e6a98 100755 --- a/Exercise2/utils.py +++ b/Exercise2/utils.py @@ -119,7 +119,9 @@ def __init__(self): 'Predict', 'Regularized Logistic Regression Cost', 'Regularized Logistic Regression Gradient'] - super().__init__('logistic-regression', part_names) + part_names_key = ['sFxIn', 'yvXBE', 'HerlY', '9fxV6', 'OddeL', 'aUo3H'] + assignment_key = 'JvOPouj-S-ys8KjYcPYqrg' + super().__init__('logistic-regression', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 7): diff --git a/Exercise3/exercise3.ipynb b/Exercise3/exercise3.ipynb index 33e782af..044d3634 100755 --- a/Exercise3/exercise3.ipynb +++ b/Exercise3/exercise3.ipynb @@ -373,7 +373,7 @@ "$$\n", "\\begin{align*}\n", "& \\frac{\\partial J(\\theta)}{\\partial \\theta_0} = \\frac{1}{m} \\sum_{i=1}^m \\left( h_\\theta\\left( x^{(i)} \\right) - y^{(i)} \\right) x_j^{(i)} & \\text{for } j = 0 \\\\\n", - "& \\frac{\\partial J(\\theta)}{\\partial \\theta_0} = \\left( \\frac{1}{m} \\sum_{i=1}^m \\left( h_\\theta\\left( x^{(i)} \\right) - y^{(i)} \\right) x_j^{(i)} \\right) + \\frac{\\lambda}{m} \\theta_j & \\text{for } j \\ge 1\n", + "& \\frac{\\partial J(\\theta)}{\\partial \\theta_j} = \\left( \\frac{1}{m} \\sum_{i=1}^m \\left( h_\\theta\\left( x^{(i)} \\right) - y^{(i)} \\right) x_j^{(i)} \\right) + \\frac{\\lambda}{m} \\theta_j & \\text{for } j \\ge 1\n", "\\end{align*}\n", "$$\n", "\n", diff --git a/Exercise3/utils.py b/Exercise3/utils.py index 633a5636..4bf99715 100755 --- a/Exercise3/utils.py +++ b/Exercise3/utils.py @@ -79,8 +79,9 @@ def __init__(self): 'One-vs-All Classifier Training', 'One-vs-All Classifier Prediction', 'Neural Network Prediction Function'] - - super().__init__('multi-class-classification-and-neural-networks', part_names) + part_names_key = ['jzAIf', 'LjDnh', '3yxcY', 'yNspP'] + assignment_key = '2KZRbGlpQnyzVI8Ki4uXjw' + super().__init__('multi-class-classification-and-neural-networks', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 5): diff --git a/Exercise4/utils.py b/Exercise4/utils.py index 6b7c3bdc..6d18b86f 100755 --- a/Exercise4/utils.py +++ b/Exercise4/utils.py @@ -193,7 +193,9 @@ def __init__(self): 'Sigmoid Gradient', 'Neural Network Gradient (Backpropagation)', 'Regularized Gradient'] - super().__init__('neural-network-learning', part_names) + part_names_key = ['aAiP2', '8ajiz', 'rXsEO', 'TvZch', 'pfIYT'] + assignment_key = 'xolSVXukR72JH37bfzo0pg' + super().__init__('neural-network-learning', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 6): diff --git a/Exercise5/exercise5.ipynb b/Exercise5/exercise5.ipynb index c0ca4f5d..5da0c5f5 100755 --- a/Exercise5/exercise5.ipynb +++ b/Exercise5/exercise5.ipynb @@ -396,7 +396,7 @@ " A vector of shape m. error_train[i] contains the training error for\n", " i examples.\n", " error_val : array_like\n", - " A vecotr of shape m. error_val[i] contains the validation error for\n", + " A vector of shape m. error_val[i] contains the validation error for\n", " i training examples.\n", " \n", " Instructions\n", @@ -687,9 +687,9 @@ "\n", "### 3.2 Optional (ungraded) exercise: Adjusting the regularization parameter\n", "\n", - "In this section, you will get to observe how the regularization parameter affects the bias-variance of regularized polynomial regression. You should now modify the the lambda parameter and try $\\lambda = 1, 100$. For each of these values, the script should generate a polynomial fit to the data and also a learning curve.\n", + "In this section, you will get to observe how the regularization parameter affects the bias-variance of regularized polynomial regression. You should now modify the lambda parameter and try $\\lambda = 1, 100$. For each of these values, the script should generate a polynomial fit to the data and also a learning curve.\n", "\n", - "For $\\lambda = 1$, the generated plots should look like the the figure below. You should see a polynomial fit that follows the data trend well (left) and a learning curve (right) showing that both the cross validation and training error converge to a relatively low value. This shows the $\\lambda = 1$ regularized polynomial regression model does not have the high-bias or high-variance problems. In effect, it achieves a good trade-off between bias and variance.\n", + "For $\\lambda = 1$, the generated plots should look like the figure below. You should see a polynomial fit that follows the data trend well (left) and a learning curve (right) showing that both the cross validation and training error converge to a relatively low value. This shows the $\\lambda = 1$ regularized polynomial regression model does not have the high-bias or high-variance problems. In effect, it achieves a good trade-off between bias and variance.\n", "\n", "\n", " \n", diff --git a/Exercise5/utils.py b/Exercise5/utils.py index b2340ad7..da8b9be4 100755 --- a/Exercise5/utils.py +++ b/Exercise5/utils.py @@ -138,7 +138,9 @@ def __init__(self): 'Learning Curve', 'Polynomial Feature Mapping', 'Validation Curve'] - super().__init__('regularized-linear-regression-and-bias-variance', part_names) + part_names_key = ['a6bvf', 'x4FhA', 'n3zWY', 'lLaa4', 'gyJbG'] + assignment_key = '-wEfetVmQgG3j-mtasztYg' + super().__init__('regularized-linear-regression-and-bias-variance', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 6): diff --git a/Exercise6/exercise6.ipynb b/Exercise6/exercise6.ipynb index cdc43975..e7e1ffac 100755 --- a/Exercise6/exercise6.ipynb +++ b/Exercise6/exercise6.ipynb @@ -395,7 +395,7 @@ " You can use `svmPredict` to predict the labels on the cross\n", " validation set. For example, \n", " \n", - " predictions = svmPredict(model, Xval)\n", + " predictions = utils.svmPredict(model, Xval)\n", "\n", " will return the predictions on the cross validation set.\n", " \n", diff --git a/Exercise6/utils.py b/Exercise6/utils.py index 6d99cec8..4ff2c2a8 100755 --- a/Exercise6/utils.py +++ b/Exercise6/utils.py @@ -695,7 +695,9 @@ def __init__(self): 'Parameters (C, sigma) for Dataset 3', 'Email Processing', 'Email Feature Extraction'] - super().__init__('support-vector-machines', part_names) + part_names_key = ['drOLk', 'JYt9Q', 'UHwLk', 'RIiFh'] + assignment_key = 'xHfBJWXxTdKXrUG7dHTQ3g' + super().__init__('support-vector-machines', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 5): diff --git a/Exercise7/utils.py b/Exercise7/utils.py index 81109173..668802b9 100755 --- a/Exercise7/utils.py +++ b/Exercise7/utils.py @@ -211,7 +211,9 @@ def __init__(self): 'PCA', 'Project Data (PCA)', 'Recover Data (PCA)'] - super().__init__('k-means-clustering-and-pca', part_names) + part_names_key = ['7yN0U', 'G1WGM', 'ixOMV', 'AFoJK', 'vf9EL'] + assignment_key = 'rGGTuM9gQoaikOnlhLII1A' + super().__init__('k-means-clustering-and-pca', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 6): diff --git a/Exercise8/utils.py b/Exercise8/utils.py index 938a7bb9..5edf19cf 100755 --- a/Exercise8/utils.py +++ b/Exercise8/utils.py @@ -235,7 +235,9 @@ def __init__(self): 'Collaborative Filtering Gradient', 'Regularized Cost', 'Regularized Gradient'] - super().__init__('anomaly-detection-and-recommender-systems', part_names) + part_names_key = ['WGzrg', '80Tcg', 'KDzSh', 'wZud3', 'BP3th', 'YF0u1'] + assignment_key = 'gkyVYM98RcWlmQ9s84QNKA' + super().__init__('anomaly-detection-and-recommender-systems', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 7): diff --git a/environment.yml b/environment.yml index 07a7a45d..624707a3 100644 --- a/environment.yml +++ b/environment.yml @@ -5,5 +5,6 @@ dependencies: - jupyter=1.0.0 - matplotlib=2.1.2 - numpy=1.13.3 + - requests=2.26.0 - python=3.6.4 - scipy=1.0.0 diff --git a/submission.py b/submission.py index 10113e47..2236f4f2 100755 --- a/submission.py +++ b/submission.py @@ -1,21 +1,21 @@ -from urllib.parse import urlencode -from urllib.request import urlopen -import pickle import json +import os +import pickle from collections import OrderedDict + import numpy as np -import os +import requests class SubmissionBase: - - submit_url = 'https://www-origin.coursera.org/api/' \ - 'onDemandProgrammingImmediateFormSubmissions.v1' + submit_url = 'https://www.coursera.org/api/onDemandProgrammingScriptSubmissions.v1?includes=evaluation' save_file = 'token.pkl' - def __init__(self, assignment_slug, part_names): + def __init__(self, assignment_slug, assignment_key, part_names, part_names_key): self.assignment_slug = assignment_slug + self.assignment_key = assignment_key self.part_names = part_names + self.part_names_key = part_names_key self.login = None self.token = None self.functions = OrderedDict() @@ -28,24 +28,25 @@ def grade(self): # Evaluate the different parts of exercise parts = OrderedDict() for part_id, result in self: - parts[str(part_id)] = {'output': sprintf('%0.5f ', result)} - result, response = self.request(parts) + parts[self.part_names_key[part_id - 1]] = {'output': sprintf('%0.5f ', result)} + response = self.request(parts) response = json.loads(response.decode("utf-8")) # if an error was returned, print it and stop - if 'errorMessage' in response: - print(response['errorMessage']) + if 'errorCode' in response: + print(response['message'], response['details']['learnerMessage']) return # Print the grading table print('%43s | %9s | %-s' % ('Part Name', 'Score', 'Feedback')) print('%43s | %9s | %-s' % ('---------', '-----', '--------')) - for part in parts: - part_feedback = response['partFeedbacks'][part] - part_evaluation = response['partEvaluations'][part] + for index, part in enumerate(parts): + part_feedback = response['linked']['onDemandProgrammingScriptEvaluations.v1'][0]['parts'][str(part)][ + 'feedback'] + part_evaluation = response['linked']['onDemandProgrammingScriptEvaluations.v1'][0]['parts'][str(part)] score = '%d / %3d' % (part_evaluation['score'], part_evaluation['maxScore']) - print('%43s | %9s | %-s' % (self.part_names[int(part) - 1], score, part_feedback)) - evaluation = response['evaluation'] + print('%43s | %9s | %-s' % (self.part_names[int(index)], score, part_feedback)) + evaluation = response['linked']['onDemandProgrammingScriptEvaluations.v1'][0] total_score = '%d / %d' % (evaluation['score'], evaluation['maxScore']) print(' --------------------------------') print('%43s | %9s | %-s\n' % (' ', total_score, ' ')) @@ -71,18 +72,15 @@ def login_prompt(self): pickle.dump((self.login, self.token), f) def request(self, parts): - params = { - 'assignmentSlug': self.assignment_slug, + payload = { + 'assignmentKey': self.assignment_key, + 'submitterEmail': self.login, 'secret': self.token, - 'parts': parts, - 'submitterEmail': self.login} - - params = urlencode({'jsonBody': json.dumps(params)}).encode("utf-8") - f = urlopen(self.submit_url, params) - try: - return 0, f.read() - finally: - f.close() + 'parts': dict(eval(str(parts)))} + headers = {} + + r = requests.post(self.submit_url, data=json.dumps(payload), headers=headers) + return r.content def __iter__(self): for part_id in self.functions: