A Neuron Learns the Camera چالش 6 از جلسهی 31، سخت
جلسهی قبل وزنهای [1, -1] را خودمان دادیم؛ حالا نورون خودش یاد بگیرد! data برای هر عکس نمونه (top, bottom, 1 یا 0) دارد (1 یعنی دست بالا). وزنها از صفر شروع میشوند. حلقهی آموزش را با قانون پرسپترون بنویس (حداکثر ۲۰ دور، با break وقتی دوری بیاشتباه بود) و هر دور rounds را یکی زیاد کن. بعد برنامه روی عکسهای امید میآزماید. خروجی:rounds: 2omid right: 8 of 8
import cam
def features(photo):
top = 0
bottom = 0
for row in photo[:8]:
top += sum(row)
for row in photo[8:]:
bottom += sum(row)
return top // 128, bottom // 128
data = []
for label, photo in cam.samples():
top, bottom = features(photo)
data.append((top, bottom, 1 if label == "up" else 0))
w1, w2, t = 0.0, 0.0, 0.0
rate = 0.1
rounds = 0
right = 0
for label, photo in cam.samples(who="omid", start=4):
top, bottom = features(photo)
guess = "up" if w1 * top + w2 * bottom >= t else "down"
if guess == label:
right += 1
print("rounds:", rounds)
print("omid right:", right, "of 8")
راهنما و جواب این چالش · برگشت به درس جلسهی 31: آموزش یعنی اصلاح اشتباه
بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال