コード例 #1
0
ファイル: model.py プロジェクト: evpozdniakov/tic-tac-toe
def predict3(W, b, x):
  assert isinstance(W, np.ndarray)
  assert isinstance(b, np.ndarray)
  assert isinstance(x, np.ndarray)

  assert training.is_proper_training_X_data(x)

  debug = False

  if debug:
    received_position = position.transform_vector_into_position(x)
    print("received position:")
    position.print_position(received_position)

  (aL, _) = forward_propagation(W, b, x)

  if debug:
    print("received FP results:")
    print(aL)

  y = np.zeros((9, 1))

  maxIndex = aL.argmax()

  # if aL[maxIndex] > 0.5:
  y[maxIndex] = 1

  if debug:
    y_position = position.transform_vector_into_position(y)
    print("prediction:")
    position.print_position(y_position)

    raw_input("...")

  return (y, aL[maxIndex], aL)
コード例 #2
0
def test_model_on_static_examples(model_fname, training_examples_fname, m=0):
  modelInstance = model.load(model_fname)

  # n = model9x9['n']
  W = modelInstance['W']
  b = modelInstance['b']

  trainingExamples = training.read_training_examples(m, fname=training_examples_fname)

  if m == 0:
    m = trainingExamples['X'].shape[1]

  for i in range(1000):
    # i = round(np.random.rand() * m)
    x = trainingExamples['X'].T[i]
    nextPosition = position.transform_vector_into_position(x)

    position.print_position(nextPosition)

    print(' predicted ')

    x = position.transform_position_into_vector(nextPosition)
    movement = model.predict(W, b, x)
    position.print_movement(movement)

    print(' expected ')

    y = trainingExamples['Y'].T[i]
    position.print_movement(y.reshape(9, 1))

    raw_input("Press Enter to continue...")
コード例 #3
0
def is_proper_training_X_data(x):
    assert isinstance(x, np.ndarray)
    assert x.shape == (9, 1)

    pos_from_vec = position.transform_vector_into_position(x)
    inv_pos = position.invert_position(pos_from_vec)

    return position.is_real_position(inv_pos)
コード例 #4
0
def display_signle_training_example(x, y):
  nextPosition = position.transform_vector_into_position(x)

  position.print_position(nextPosition)

  print(' expected ')

  position.print_movement(y.reshape(9, 1))

  raw_input("Press Enter to continue...")
コード例 #5
0
def make_movement(game_position, make_movement_fn, do_random_movement=False):
    assert isinstance(game_position, np.ndarray)
    assert game_position.shape == (3, 3)
    assert position.is_real_position(game_position)

    if game_position.sum() == 0:
        # main player makes movement
        position_vector = position.transform_position_into_vector(
            game_position)
    else:
        # opponent makes movement
        inverted_position = position.invert_position(game_position)
        position_vector = position.transform_position_into_vector(
            inverted_position)

    (movement_vector, highest_al) = make_movement_fn(position_vector)

    if do_random_movement:
        random_movement = position.make_random_movement(game_position)
        random_movement['highest_al'] = highest_al
        return random_movement

    movement_position = position.transform_vector_into_position(
        movement_vector)

    for i in range(3):
        for j in range(3):
            if (movement_position[i][j] == 1):
                coords = (i, j)
                break

    i2 = coords[0]
    j2 = coords[1]

    # choose random empty cell if make_movement_fn failed to choose an empty cell
    if game_position[i2][j2] != 0:
        random_movement = position.make_random_movement(game_position)
        random_movement['highest_al'] = highest_al
        return random_movement

    result_position = position.clone(game_position)

    result_position[i2][j2] = 1 if game_position.sum() == 0 else -1

    movement = {
        'coords': coords,
        'result_position': result_position,
        'highest_al': highest_al,
    }

    return movement
コード例 #6
0
def test_position(model_fname):
  print('\ntest model: %s' % (model_fname))
  model_instance = model.load(model_fname)
  W = model_instance['W']
  b = model_instance['b']


  x = np.array([
    -1,-1, 0,
     0, 1, 0,
     0, 0, 0,
  ]).reshape(9, 1)

  print('\n')
  position.print_position(position.transform_vector_into_position(x))

  (aL, _) = model.forward_propagation(W, b, x)
  print("aL")
  print(aL)
  movement = model.predict(W, b, x)
  position.print_movement(movement)
コード例 #7
0
def train_model_scenario_4(model_fname, alpha0=1, iterations=500000, beta=0.9):
  debug = False

  model_instance = model.load(model_fname)

  n = model_instance['n']
  W = model_instance['W']
  b = model_instance['b']
  
  vdW = np.zeros(W.shape)
  vdb = np.zeros(b.shape)

  decay_rate = 9.0 / iterations # it will reduce final alpha 10 times

  for i in range(0, iterations):
    if i % 1000 == 0:
      print('i: %d' % (i))

    # debug = True if i % 500 == 0 else False

    make_movement_fn = lambda x: model.predict2(W, b, x)

    ex = training.make_training_examples(make_movement_fn)

    X = ex['X']
    Y = ex['Y']

    if debug:
      print(X)
      print(Y)
      for j in range(len(X.T) - 1, -1, -1):
        x = X.T[j]
        nextPosition = position.transform_vector_into_position(x)
        x = position.transform_position_into_vector(nextPosition)

        position.print_position(nextPosition)

        (aL, _) = model.forward_propagation(W, b, x)

        print("\naL")
        print(aL)

        print('\n predicted ')

        movement = model.predict(W, b, x)
        position.print_movement(movement)

        print(' expected ')

        y = Y.T[j]
        position.print_movement(y.reshape(9, 1))

        raw_input("Press Enter to continue...")

    # displayTrainingExamples(X, Y)

    # (dW, db) = model.calcGradients(W, b, X, Y)
    (dW, db, _) = model.back_propagation(n, W, b, X, Y)

    if debug:
      if i > 0 and i % 3000 == 0:
        is_back_prop_correct = model.check_back_propagation(n, W, b, X, Y)

        if is_back_prop_correct:
          print("BP is OK")
        else:
          print("BP is not correct")
          exit()

    alpha = alpha0 / (1.0 + decay_rate * i)

    # if debug:
    if i % 1000 == 0:
      print("alpha:")
      print(alpha)

    vdW = beta * vdW + (1 - beta) * dW
    vdb = beta * vdb + (1 - beta) * db

    # model.updateWeights(W, dW, b, db, alpha)

    # W = W - alpha * vdW
    # b = b - alpha * vdb
    W = W - alpha * dW
    b = b - alpha * db
    
    if i > 0 and i % 50 == 0:
      model.save(n, W, b, model_fname)
      if debug:
        print("model saved")

  print('------ end -------')

  model.save(n, W, b, model_fname)