Exemplo n.º 1
0
 def __call__(self, output, std):
     assert (len(output) == len(std))
     lengthStd = len(std)
     self.ans = None
     for i in range(lengthStd):
         v = op.Value("const", std[i])
         if self.ans == None:
             self.ans = -v * op.log(output[i])
         else:
             self.ans = self.ans - v * op.log(output[i])
     return self
Exemplo n.º 2
0
 def __init__(self, shape, net, initialWeights=None):
     self.net = net
     thisNum = len(self.net.linearNames)
     self.name = "_Linear" + str(thisNum)
     self.net.linearNames[self.name] = shape
     if len(shape) != 2:
         raise ValueError
     self.input_shape = shape[0]
     self.output_shape = shape[1]
     self.params = []
     self.net.weights[self.name] = self.params
     for i in range(shape[0]):
         self.params.append([])
         for j in range(shape[1]):
             if initialWeights == None:
                 self.params[i].append(
                     op.Value(self.name + str(i) + str(j), 0))
             else:
                 self.params[i].append(
                     op.Value(self.name + str(i) + str(j),
                              initialWeights[i][j]))
     self.net.weights[self.name] = self.params
Exemplo n.º 3
0
 def __call__(self, inputArray):
     if self.input_shape != len(inputArray):
         raise ValueError
     output = []
     if not isinstance(inputArray[0], op.Op):
         inputArray = list(map(lambda t: op.Value("const", t), inputArray))
     for j in range(self.output_shape):
         res = None
         for i in range(self.input_shape):
             if res == None:
                 res = self.params[i][j] * inputArray[i]
             else:
                 res += self.params[i][j] * inputArray[i]
         output.append(res)
     return output
Exemplo n.º 4
0
# test homework function
import op
import numpy as np
import math as m


def npFunc(x1, x2, x3):
    return (np.sin(x1 + 1) + np.cos(2 * x2)) * np.tan(np.log(x3)) \
        + (np.sin(x2 + 1) + np.cos(2 * x1)) * np.exp(1 + np.sin(x3))


x1_ = x2_ = x3_ = 2.5

# define a function
x1 = op.Value("x1", 2.5)
x2 = op.Value("x2", 2.5)
x3 = op.Value("x3", 2.5)
const1 = op.Value("const1", 1)
const2 = op.Value("const2", 2)
func = (op.sin(x1 + const1) + op.cos(const2 * x2)) * op.tan(op.log(x3)) \
    + (op.sin(x2 + const1) + op.cos(const2 * x1)) * op.exp(const1 + op.sin(x3))

print("test getRes(): ")
print(func.getRes())
print(npFunc(x1_, x2_, x3_))

print("test Df(): ")
print([func.Df("x1"), func.Df("x2"), func.Df("x3")])
print([
    m.cos(x1_ + 1) * m.tan(m.log(x3_)) -
    2 * m.sin(2 * x1_) * m.exp(1 + m.sin(x3_)),
Exemplo n.º 5
0
 def forward(self, x):
     init_x = list(map(lambda t: op.Value("const", t), x))
     x = Sigmoid(self.fc1(x))
     x = listAdd(self.fc2(x), init_x)
     # x = self.fc2(x)
     return x