Exemple #1
0
 def update_mini_batch(self, mini_batch):
     w_grads = {i: 0 for i in range(self.net.NumConnections())}
     b_grads = {i: 0 for i in range(self.net.NumNeurons())}
     for inputs, target in mini_batch:
         batch_w_grads, batch_b_grads = self.backprop(inputs, target)
         for idx, grad in batch_w_grads.items():
             w_grads[idx] += grad
         for idx, grad in batch_b_grads.items():
             b_grads[idx] += grad
     for idx, grad in w_grads.items():
         old_weight = self.net.connections[idx].weight
         self.net.connections[idx].weight = util.constraint(old_weight - grad * (self.lr / self.batch_size),
                                                            self.maxweight)
     for idx, grad in b_grads.items():
         old_bias = self.net.neurons[idx].bias
         self.net.neurons[idx].bias = util.constraint(old_bias - grad * (self.lr / self.batch_size), self.maxbias)
import matplotlib.pyplot as plt
import sympy
from util import constraint, displayBoard
from IPython.display import display

#init_printing()
# Declare any required symbolic variables
N = 5
rows = sympy.S("row:"+str(N))
cols = sympy.S("col:"+str(N))

#raise NotImplementedError("TODO: declare symbolic variables for the constraint generators")

# Define diffRow and diffDiag constraints
#raise NotImplementedError("TODO: create the diffRow and diffDiag constraint generators")
diffRow = constraint(...)
diffDiag = constraint(...)

# Test diffRow and diffDiag
_x = sympy.symbols("x:3")

# generate a diffRow instance for testing
diffRow_test = _x[0]^_x[1]

assert(len(diffRow_test.free_symbols) == 2)
assert(diffRow_test.subs({_x[0]: 0, _x[1]: 1}) == True)
assert(diffRow_test.subs({_x[0]: 0, _x[1]: 0}) == False)
assert(diffRow_test.subs({_x[0]: 0}) != False)  # partial assignment is not false
print("Passed all diffRow tests.")

diffDiag_test = None
Exemple #3
0
# **But substituting values for all symbols returns a value type.** (Tip: You can substitute multiple symbols in the `subs()` command by providing a mapping (dict) from current symbols to new symbols or values.)

# In[16]:

print(type(relation), type(relation.subs({x: 0, y: 1})))
print(type(relation) != type(relation.subs({x: 0, y: 1})))

# ## Example 4: Constraints
# 
# **Constraints are a construct of this lab exercise (not part of sympy) that combine symbolic Functions with Expressions for evaluation. The `constraint()` function (defined in the `util` module) takes a name and an expression and returns a "named expression" -- a constraint.**

# In[17]:

x, y = symbols(['x', 'y'])
sameAs = constraint("SameAs", Eq(x, y))
display(sameAs)

# **Constraints are evaluated using the .subs method, just like an expression. If the resulting
# expression has unbound (free) symbols, then the result is a new constraint.**

# In[ ]:

display(sameAs.subs(x, 0), type(sameAs.subs(x, 0)))

# **If the resulting expression has no free symbols, then the result is only the evaluated expression.**

# In[ ]:

display(sameAs.subs({x: 0, y: 0}), type(sameAs.subs({x: 0, y: 0})))
"""
Created on Mon Mar 13 10:28:40 2017

@author: jfm
"""

from sympy.core import symbols
from sympy import *
#from sympy.abc import x, y
from sympy.logic.boolalg import *
from util import constraint

a, b = symbols("a b")  # symbols for 2 columns
k, l = symbols("k l")  # symbols for 2 indexes

diffRow = constraint("diffRow", Ne(a, b))
diffDiag = constraint("diffDiag", Ne(abs(a - b), abs(k - l)))
N = 10
_vars = symbols("_vars:" + str(N))
#print(_vars)
constraints = {x: set() for x in _vars}
domains = {x: set(range(N)) for x in _vars}
indexes = [(i, j) for i in range(N) for j in range(N) if i != j]
for (i, j) in indexes:
    constraints[_vars[i]].add(diffRow.subs({a: _vars[i], b: _vars[j]}))
    constraints[_vars[j]].add(
        diffDiag.subs({
            a: _vars[i],
            b: _vars[j],
            k: i,
            l: j
Exemple #5
0
# Declare any required symbolic variables
from sympy import symbols, Eq, Ne

from util import constraint, displayBoard

X = symbols("X:2")
Y = symbols("Y:2")

# Define diffRow and diffDiag constraints
diffRow = constraint("diffRow", Ne(X[0], X[1]))
diffDiag = constraint("diffDiag", Ne(abs(X[0] - X[1]), abs(Y[0] - Y[1])))


class NQueensCSP:
    """CSP representation of the N-queens problem

    Parameters
    ----------
    N : Integer
        The side length of a square chess board to use for the problem, and
        the number of queens that must be placed on the board
    """
    def __init__(self, N):
        _vars = symbols('x:%d' % N)
        _domain = set(range(N))
        self.size = N
        self.variables = _vars
        self.domains = {v: _domain for v in _vars}
        self._constraints = {x: set() for x in _vars}

        print(self.variables)
Exemple #6
0
import matplotlib as mpl
import matplotlib.pyplot as plt

from util import constraint, displayBoard
from sympy import *
from IPython.display import display

init_printing()
# %matplotlib inline
import copy

# Declare symbolic variables for the constraint generators
x1, x2, diff = symbols("x1, x2, diff")

# Define diffRow and diffDiag constraint generators
diffRow = constraint("diffRow", Ne(x1, x2))
diffDiag = constraint("diffDiag", Ne(abs(x1 - x2), diff))


class NQueensCSP:
    def __init__(self, N):
        # Declare symbolic variables in the CSP constructor"
        _vars = symbols("col:" + str(N))
        _domain = set(range(N))
        self.size = N
        self.variables = _vars
        self.domains = {v: set(_domain) for v in _vars}
        self._constraints = {x: set() for x in _vars}

        # Add constraints - for each pair of variables xi and xj, create
        # a diffRow(xi, xj) and a diffDiag(xi, xj) instance, and add them
# 
# ### Define Symbolic Expressions for the Problem Constraints
# Before implementing the board class, we need to construct the symbolic constraints that will be used in the CSP.  Declare any symbolic terms required, and then declare two generic constraint generators:
# - `diffRow` - generate constraints that return True if the two arguments do not match
# - `diffDiag` - generate constraints that return True if two arguments are not on the same diagonal (Hint: you can easily test whether queens in two columns are on the same diagonal by testing if the difference in the number of rows and the number of columns match)
# 
# Both generators should produce binary constraints (i.e., each should have two free symbols) once they're bound to specific variables in the CSP.  For example, Eq((a + b), (b + c)) is not a binary constraint, but Eq((a + b), (b + c)).subs(b, 1) _is_ a binary constraint because one of the terms has been bound to a constant, so there are only two free variables remaining. 

# In[ ]:

# Declare any required symbolic variables
raise NotImplementedError("TODO: declare symbolic variables for the constraint generators")

# Define diffRow and diffDiag constraints
raise NotImplementedError("TODO: create the diffRow and diffDiag constraint generators")
diffRow = constraint(...)
diffRow = constraint(...)


# In[ ]:

# Test diffRow and diffDiag
_x = symbols("x:3")

# generate a diffRow instance for testing
raise NotImplementedError("TODO: use your diffRow constraint to generate a diffRow constraint for _x[0] and _x[1]")
diffRow_test = None

assert(len(diffRow_test.free_symbols) == 2)
assert(diffRow_test.subs({_x[0]: 0, _x[1]: 1}) == True)
assert(diffRow_test.subs({_x[0]: 0, _x[1]: 0}) == False)
    return result


#result = backtracking_search(NQueensCSP(8))
#print(len(result))
#print(result)

#d =UniversalDict(list(range(8)))
#print(d)
#print(d[999])

# Define diffRow and diffDiag constraints
r1, r2 = symbols('r1 r2')
c1, c2 = symbols('c1 c2')

diffRow = constraint("diffRow", ~Eq(r1, r2))
diffDiag = constraint("diffDiag", ~Eq(abs(c1 - c2), abs(r1 - r2)))

N = 8

_vars = []
i = 0
strx = 'r'
while i < N:
    str1 = strx + str(i)
    _vars.append(symbols(str1))
    i = i + 1

display(_vars)
print(_vars[0], len(_vars))
_domain = set(range(N))
Exemple #9
0
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 15 12:46:03 2017

@author: jfm
"""

from sympy.core import symbols
from sympy import *
from sympy.abc import x, y
from sympy.logic.boolalg import *
from util import constraint

N = 2
a, b = symbols("a b")  
diffRow = constraint("diffRow",Ne(a,b))

vars = symbols("vars:" + str(N))

constraints = {x: set() for x in vars}
domains = {x : set(range(N)) for x in vars}
indexes = [(i,j) for i in range(N) for j in range(N) if i != j]
for (i, j) in indexes :
    constraints[vars[i]].add(diffRow.subs({a:vars[i],b:vars[j]}))



def revise(var1, var2, constraint):
    revised = False
    for x in set(domains[var1]) :
        if all([constraint.subs({var1:x,var2:y}) == False for y in domains[var2]]) :
Exemple #10
0
display(relation)

display(relation.subs(x, z))

a = symbols("a:5")
b = symbols("b:5")
display([relation.subs({x: _a, y: _b}) for _a, _b in zip(a, b)])

print(type(relation), type(relation.subs(x, z)))
print(type(relation) == type(relation.subs(x, z)))

print(type(relation), type(relation.subs({x: 0, y: 1})))
print(type(relation) != type(relation.subs({x: 0, y: 1})))

x, y = symbols(['x', 'y'])
sameAs = constraint("SameAs", Eq(x, y))
display(sameAs)

display(sameAs.subs(x, 0), type(sameAs.subs(x, 0)))

display(sameAs.subs({x: 0, y: 0}), type(sameAs.subs({x: 0, y: 0})))

A = symbols("A:3")

# test for completion
assert (len(A) == 3)
assert (all([type(v) == Symbol for v in A]))
print("All tests passed!")

x, y = symbols("x,y")
E = x ^ y