Learning · Robot club · about 15 min
Saving and loading a model with JSON; models trained elsewhere.
[1 mark]What does this program print?
import json sample = ([16, 16], "wall-ahead") back = json.loads(json.dumps(sample)) print(back) print(back == sample)
[1 mark]What do json.dumps and json.loads do?
dumps turns data into text; loads turns text back into datadumps deletes a model; loads restores itdumps saves to a file; loads downloads from the internet[1 mark]What does this program print?
import json
MODEL = '{"kind": "nearest-neighbour", "samples": [{"x": [16, 16], "label": "wall-ahead"}, {"x": [50, 50], "label": "open"}]}'
model = json.loads(MODEL)
data = [(s["x"], s["label"]) for s in model["samples"]]
print(model["kind"], len(data), data[1][1])[1 mark]Why does the saved model include a kind field?
[1 mark]How do you get a numpy weights table into a form JSON can write?
W1.tolist()json.dumps(W1) works directlystr(W1)W1.shape[1 mark]Which of these can JSON write down as they are?
Tick every answer that is true.
Load the model in MODEL, classify the current view with it, and print model says: <label>. Do not drive.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
import json
MODEL = '{"kind": "nearest-neighbour", "features": "depth grid rows 2 and 3", "samples": [{"x": [54, 52, 51, 51, 51, 51, 52, 54, 54, 52, 51, 51, 51, 51, 52, 54], "label": "open"}, {"x": [16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16], "label": "wall-ahead"}, {"x": [36, 47, 46, 46, 46, 16, 16, 16, 36, 47, 46, 46, 46, 16, 16, 16], "label": "gap-left"}, {"x": [16, 16, 16, 46, 46, 46, 47, 36, 16, 16, 16, 46, 46, 46, 47, 36], "label": "gap-right"}]}'
model = json.loads(MODEL)
print(model['kind'], len(model['samples']), 'samples')Plan your program here, then type it in and press Run.
kind set to network, and write predict(model, view) that works for both kinds.trained_on field with today's date and print it when the model loads.k in the model and vote among the k nearest samples.