forked from hse-aml/intro-to-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,685 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
import re | ||
import requests | ||
import json | ||
|
||
|
||
class Grader(object): | ||
def __init__(self, assignment_key, all_parts=()): | ||
""" | ||
Assignment key is the way to tell Coursera which problem is being submitted. | ||
""" | ||
self.submission_page = \ | ||
'https://www.coursera.org/api/onDemandProgrammingScriptSubmissions.v1' | ||
self.assignment_key = assignment_key | ||
self.answers = {part: None for part in all_parts} | ||
|
||
def submit(self, email, token): | ||
submission = { | ||
"assignmentKey": self.assignment_key, | ||
"submitterEmail": email, | ||
"secret": token, | ||
"parts": {} | ||
} | ||
for part, output in self.answers.items(): | ||
if output is not None: | ||
submission["parts"][part] = {"output": output} | ||
else: | ||
submission["parts"][part] = dict() | ||
request = requests.post(self.submission_page, data=json.dumps(submission)) | ||
response = request.json() | ||
if request.status_code == 201: | ||
print('Submitted to Coursera platform. See results on assignment page!') | ||
elif u'details' in response and u'learnerMessage' in response[u'details']: | ||
print(response[u'details'][u'learnerMessage']) | ||
else: | ||
print("Unknown response from Coursera: {}".format(request.status_code)) | ||
print(response) | ||
|
||
def set_answer(self, part, answer): | ||
"""Adds an answer for submission. Answer is expected either as string, number, or | ||
an iterable of numbers. | ||
Args: | ||
part - str, assignment part id | ||
answer - answer to submit. If non iterable, appends repr(answer). If string, | ||
is appended as provided. If an iterable and not string, converted to | ||
space-delimited repr() of members. | ||
""" | ||
if isinstance(answer, str): | ||
self.answers[part] = answer | ||
else: | ||
try: | ||
self.answers[part] = " ".join(map(repr, answer)) | ||
except TypeError: | ||
self.answers[part] = repr(answer) | ||
|
||
|
||
def array_to_grader(array, epsilon=1e-4): | ||
"""Utility function to help preparing Coursera grading conditions descriptions. | ||
Args: | ||
array: iterable of numbers, the correct answers | ||
epslion: the generated expression will accept the answers with this absolute difference with | ||
provided values | ||
Returns: | ||
String. A Coursera grader expression that checks whether the user submission is in | ||
(array - epsilon, array + epsilon)""" | ||
res = [] | ||
for element in array: | ||
if isinstance(element, int): | ||
res.append("[{0}, {0}]".format(element)) | ||
else: | ||
res.append("({0}, {1})".format(element - epsilon, element + epsilon)) | ||
return " ".join(res) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../grading.py |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
import re | ||
|
||
def model_total_params(model): | ||
""" | ||
Total params for Keras model | ||
""" | ||
summary = [] | ||
model.summary(print_fn=lambda x: summary.append(x)) | ||
for line in summary: | ||
m = re.match("Total params: ([\d,]+)", line) | ||
if m: | ||
return int(re.sub(",", "", m.groups()[0])) | ||
return 0 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.