Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- def pattern_associator_delta(inputs, targets, weights=None, rate=0.25, max_steps=100):
- step = 0
- if weights is None:
- weights = [0] * len(inputs[0])
- weights = np.array(weights, dtype='float64', ndmin=2)
- inputs = [np.array(i, ndmin=2) for i in inputs]
- targets = [np.array(i, ndmin=2) for i in targets]
- while step < max_steps:
- errors = np.zeros(len(inputs), dtype='float64')
- # Train over all patterns
- for input_pattern, target_pattern in zip(inputs, targets):
- output_pattern = np.dot(input_pattern, weights.T)
- error = target_pattern - output_pattern
- delta = np.dot(error.T, input_pattern) * rate
- weights = weights + delta
- # Compute errors at current state
- for i, io in enumerate(zip(inputs, targets)):
- output_pattern = np.dot(io[0], weights.T)
- error = io[1] - output_pattern
- errors[i] = np.sqrt(np.mean(error**2))
- tss = sum(errors**2)
- print('tss: {}'.format(tss))
- if np.isclose(tss, 0):
- break
- step += 1
- # Validate
- for input_pattern, target_pattern in zip(inputs, targets):
- assert np.allclose(np.dot(input_pattern, weights.T), target_pattern, atol=1e-3)
- return weights
- pattern_associator_delta(inputs=[[1, 1, 1, 1], [1, 1, -1, -1]],
- targets=[[1], [1]])
- pattern_associator_delta(inputs=[[1, -1, 1, -1],
- [1, 1, 1, 1],
- [1, 1, 1, -1],
- [1, -1, -1, 1]],
- targets =[[1], [1], [-1], [-1]])
Add Comment
Please, Sign In to add comment