Exemple #1
0
def fx(func, i, nodes, debug):
  if func is "sigm":
    res = nnlib.sigmoid(nodes[i])
  elif func is "tanh":
    res = nnlib.hyper_tan(nodes[i])

  log.d(debug, log.hl("f(%d)  \t(%.2f)\t---> %s \t--->   (%.2f) " %(i, nodes[i], func, res),2))
  nodes[i] = res
Exemple #2
0
def _do_with_retry(raw):
    # NOTE in list mode, will ignore all Errors
    retry_max = etc['list_retry']
    retry_count = 0
    def check_should_retry():
        if retry_max < 0:	# -1 means retry forever
            return True
        if retry_count <= retry_max:
            return True
    while check_should_retry():
        retry_info = str(retry_count) + '/' + str(retry_max)
        # print retry info
        if retry_count > 0:
            log.i('[list] start retry ' + retry_info)
        # count info
        count = len(raw)
        count_ok = 0
        count_err = 0
        # do each task
        log.i('[list] start ' + str(count) + ' task ')
        for i in range(count):
            task_info = str(i + 1) + ' / ' + str(count)
            item = raw[i]
            log.p('')	# NOTE for better print
            log.i('[list] (ok ' + str(count_ok) + ', err ' + str(count_err) + ') start task ' + task_info + ', ' + item + ' ')
            try:
                _do_one_task(item)
            except Exception as e:
                log.e('[list] task ' + task_info + ' failed, ' + str(e) + ' \n')
                # NOTE check feature list_ignore_task_err
                if not conf.FEATURES['list_ignore_task_err']:
                    log.d('disabled feature list_ignore_task_err')
                    raise	# not ignore Error here
                # ignore Error here
                count_err += 1
            else:
                log.o('[list] task ' + task_info + ' finished \n')
                count_ok += 1
        # check retry
        retry_text = ''
        if retry_count > 0:
            retry_text = ', retry ' + retry_info
        if count_err == 0:	# no Error, not retry
            log.o('[list] all task finished ' + str(count_ok) + ' / ' + str(count) + retry_text + ' ')
            break	# not retry
        # should retry
        log.e('[list] task failed ' + str(count_err) + ' / ' + str(count) + retry_text + ' ')
        # update retry count
        retry_count += 1
        # check should retry
        if check_should_retry():
            log.i('[list] before next retry wait ' + str(conf.list_retry_wait_s) + ' seconds ')
            time.sleep(conf.list_retry_wait_s)
        else:
            log.e('[list] task retry failed' + retry_text + ' ')
Exemple #3
0
def propagate(msg, i, nodes, debug, con):

  log.d(debug, msg)
  for c in con[i]:
    c.propagate(nodes)
    log.d(debug, c)
  log.d(debug, "")
Exemple #4
0
def main():

    """
    eta and alpha are parameters that can be tweaked.
    """
    eta = 0.90
    alpha = 0.24

    """
    maxIter represents the limit of how many times the data can be
    rerun and the weights can be adjusted before giving up.
    """
    maxIter = 500000

    """
    stop_err represents the acceptable value for giving up
    and will kick in if the error value becomes smaller and
    maxIter still has not stopped the script.
    """
    stop_err = 0.001

    """
    The data set is splitted into:
    [[input],[output]]
    """
    dataset = input("Name of dataset: ")
    data = io.readDataset("data/%s.data" % dataset)


    inodes = 1 # This must match the number of input fields in the data set
    hnodes = 4
    onodes = 1 # This must match the number of output fields in the data set


    """
    The bias node will be placed at the end of the node-array
    and will have connections to all nodes in the hidden layer
    and the output layer
    """
    b = inodes + hnodes + onodes
    wtot = inodes*hnodes+hnodes+hnodes*onodes+onodes


    """
    The initial weight values for each connection will
    be set randomly and later changed by the backpropagation.
    """
    weights = [random.random() for r in range(wtot)]


    """
    The following is index ranges for the different layers.
    Do not change!
    """
    ilayer = range(0,inodes)
    hlayer = range(inodes,inodes+hnodes)
    olayer = range(inodes+hnodes,b)


    """
    Debug is turned on during the last iteration if turned off.
    Otherwise, the debug information will be printed for each
    entry in the data set every time.
    """
    debug = False

    con = [[],[]]

    """
    Add connections between the nodes in the hidden layer and the nodes in
    the input layer.
    """
    net.connect_nodes(0, hlayer, ilayer, con, weights, b)
    net.connect_nodes(1, olayer, hlayer, con, weights, b)

    lastIter = False
    err = 0
    minNErr = 1
    idata = False
    for times in range(maxIter):
      ###########################
      # Propagating
      ###########################

      if err != 0 and err < stop_err:
        lastIter = True
        #debug = True
        idata = []


      random.shuffle(data)

      """
      Testing all rows of the data set
      """
      (err, t, nodes) = net.testData(data, False, lastIter, idata, b, ilayer, hlayer, olayer, debug, con, inodes, hnodes)

      if err < minNErr:
        minNErr = err
        print(log.hl("%i  \t> Error = %.8f\n" % (times, err),1))

      if lastIter:
        for ida in idata:
          print(ida)
        log.d(debug, "  Stopped after %s iterations.\n\n" % log.hl(times+1, 1))
        break


      ###########################
      # backpropagating
      ###########################

      """
      inserting gradient.
      """

      hGrad = net.mkarr(b)
      for i in olayer:
        hGrad[i] = nnlib.tanh_grad(t[i-(inodes+hnodes)], nodes[i])

      for c in con[1]:
        c.backpropagate(hGrad)

      for i in hlayer:
        hGrad[i] = nnlib.sig_grad(hGrad[i], nodes[i])
        #log.d(debug, "  hGrad[%d] = %.2f" % (i, hGrad[i]))

      for c in con[0]:
        c.backpropagate(hGrad)

      net.adjust_weights(1, eta, hGrad, nodes, alpha, con)
      net.adjust_weights(0, eta, hGrad, nodes, alpha, con)

    try:

      testdata = io.readDataset("data/%s.test" % dataset)
      (err, t, nodes) = net.testData(testdata, True, False, [], b, ilayer, hlayer, olayer, con. inodes, hnodes)

      print(log.hl("\nRunning Testdata...",2))
      print("--------------------------------")
      print("MAE: %.8f\n" % (err))

      while True:
        inrow = io.splitTestData(input("Manual input:"));
        for new_data in inrow:
          print("In: %.4f" % new_data)

        nodes = net.propagateData(inrow, b, ilayer, hlayer, olayer, debug, con)
        for i in olayer:
          print("Out: %.4f" % nodes[i])

    except KeyboardInterrupt:
      print("Quitting...")