Skip to content

Commit

Permalink
week 3 init
Browse files Browse the repository at this point in the history
  • Loading branch information
ZEMUSHKA committed Oct 15, 2017
1 parent bd1d262 commit 6d2fc86
Show file tree
Hide file tree
Showing 9 changed files with 1,685 additions and 0 deletions.
73 changes: 73 additions & 0 deletions grading.py
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)
1 change: 1 addition & 0 deletions week3/grading.py
15 changes: 15 additions & 0 deletions week3/grading_utils.py
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
Binary file added week3/images/center_crop.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added week3/images/cifar10.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added week3/images/flowers.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added week3/images/inceptionv3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 6d2fc86

Please sign in to comment.