Demo entry 6647228

homework

   

Submitted by anonymous on Oct 19, 2017 at 15:57
Language: Python. Code size: 888 Bytes.

import numpy as np
import matplotlib.pyplot as plt

learning_rate = 0.2
weight = 0.3
targets = [(3, 1.4), (1, 2.0)]
init_val = (targets[0][0], targets[0][0] * weight)
accuracy = 0.01

x_range = np.arange(-2, 7, 0.1)
plt.axis([-2, 7, -1, 7])
plt.grid()
plt.xlabel('Input Value')
plt.ylabel('Prediction Value')
plt.plot(init_val[0], init_val[1], 'ro')
plt.plot(targets[1][0], targets[1][1], 'ro')
plt.annotate('Initial Value', xy=init_val, \
			xytext=(4.5, -0.5), \
			arrowprops=dict(facecolor='black', shrink=0.001))
plt.annotate('Target Value', xy=targets[1], \
			xytext=(-0.5, 3.5), \
			arrowprops=dict(facecolor='black', shrink=0.001))

for i in range(len(targets)):
	(x, t) = targets[i]
	while True:
		y = weight * x
		error = t - y
		delta_w = learning_rate * error / x
		plt.plot(x_range, x_range * weight)
		weight = weight + delta_w
		if error < accuracy:
			break

plt.show()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).