Пример #1
0
def train_model_scenario_3(n, model_fname, training_examples_fname, m=0, alpha=0.001, beta=0.9, iterations=10000):
  debug = False

  modelInstance = model.load(model_fname)
  W = modelInstance['W']
  b = modelInstance['b']
  # (W, b) = model.initializeWeights(n)
  vdW = np.zeros(W.shape)
  vdb = np.zeros(b.shape)
  ex = training.read_training_examples(m, fname=training_examples_fname)

  X = ex['X']
  # assert X.shape == (9, 500)

  Y = ex['Y']
  # assert Y.shape == (9, 500)


  # L is a number of NN layers
  # (L = 3 for a model 9x18x18x9)
  L = len(n) - 1
  assert len(W) == L


  for i in range(0, iterations):
    # (dW, db) = model.calcGradients(W, b, X, Y)
    (dW, db, _) = model.back_propagation(n, W, b, X, Y)

    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

    if i % 30 == 0:
      print('iteration ' + str(i))
      (aL, _) = model.forward_propagation(W, b, X)
      cost = model.cost_function(Y, aL)
      # cost = model.costFunction(Y, A[L])
      # print('alpha')
      # print(alpha)
      print('cost')
      print(cost)

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

        if not is_back_prop_correct:
          print("BP is not correct")
          exit()
    if i % 1000 == 0:
      model.save(n, W, b, model_fname)

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

  model.save(n, W, b, model_fname)
Пример #2
0
def train_model_scenario_2(n, model_fname, opponet_model_fname, alpha=0.1, iterations=5000):
  alpha0 = alpha
  decay_rate = 0.01
  modelInstance = model.load(opponet_model_fname)
  W0 = modelInstance['W']
  b0 = modelInstance['b']

  if model_fname == opponet_model_fname:
    W = W0
    b = b0
  else:
    make_movement_fn = lambda x: model.predict2(W0, b0, x)
    (W, b) = model.initialize_weights(n)

  for i in range(0, iterations):
    if model_fname == opponet_model_fname:
      make_movement_fn = lambda x: model.predict2(W, b, x)

    ex = training.make_training_examples(make_movement_fn)

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

    # displayTrainingExamples(X, Y)

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

    alpha = alpha0 / (1 + decay_rate * i)

    model.update_weights(W, dW, b, db, alpha)

    if i % 100 == 0:
      print('iteration ' + str(i))
      (aL, _) = model.forward_propagation(W, b, X)
      cost = model.cost_function(Y, aL)
      print('cost')
      print(cost)
      print('alpha')
      print(alpha)

    # if i % 1000 == 0:
    #   is_back_prop_correct = model.checkBackPropagation(n, W, b, X, Y)

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

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

  model.save(n, W, b, model_fname)
Пример #3
0
def train_model_scenario_1(n, model_fname, training_examples_fname, m=0, alpha=0.001, iterations=10000):
  debug = False

  (W, b) = model.initialize_weights(n)
  ex = training.read_training_examples(m, fname=training_examples_fname)

  X = ex['X']
  # assert X.shape == (9, 500)

  Y = ex['Y']
  # assert Y.shape == (9, 500)


  # L is a number of NN layers
  # (L = 3 for a model 9x18x18x9)
  L = len(n) - 1
  assert len(W) == L


  for i in range(0, iterations):
    # (dW, db) = model.calcGradients(W, b, X, Y)
    (dW, db, _) = model.back_propagation(n, W, b, X, Y)

    model.update_weights(W, dW, b, db, alpha)

    if i % 300 == 0:
      print('iteration ' + str(i))
      (aL, _) = model.forward_propagation(W, b, X)
      cost = model.cost_function(Y, aL)
      # cost = model.costFunction(Y, A[L])
      # print('alpha')
      # print(alpha)
      print('cost')
      print(cost)

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

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

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

  model.save(n, W, b, model_fname)
Пример #4
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)
Пример #5
0
def train(training_features):
    features = tf.placeholder(
        dtype=tf.float32,
        shape=[BATCH_SIZE, IMG_SIZE, IMG_SIZE, INPUT_DEPTH],
        name='features')
    targets = tf.placeholder(
        dtype=tf.float32,
        shape=[BATCH_SIZE, IMG_SIZE, IMG_SIZE, INPUT_DEPTH],
        name='targets')
    regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_COST)

    pred = model.forward_propagation(features, regularizer)

    global_step = tf.Variable(0, trainable=False)
    variable_averages = tf.train.ExponentialMovingAverage(
        MOVING_AVERAGE_DECAY, global_step)
    # Moving average for all trainable variables
    variables_averages_op = variable_averages.apply(tf.trainable_variables())

    # The cost function -- norm
    matrix_pow_2 = tf.pow(tf.subtract(targets, pred), 2)
    matrix_norm = tf.reduce_sum(matrix_pow_2, axis=[1, 2, 3])

    norm_cost = tf.reduce_mean(matrix_norm) / 2

    # The regularization score
    reg_score = tf.add_n(tf.get_collection('reg_score'))
    tot_loss = norm_cost + reg_score

    # Number of videos used for training
    num_videos = training_features.shape[0]
    # Length of each video
    size_videos = np.array([v.shape[0] for v in training_features])

    # Number of frames for each video that can be used for training
    training_num = size_videos - BATCH_SIZE - INPUT_DEPTH + 1
    # Total
    tot_training_num = sum(training_num)
    decay_steps = tot_training_num // BATCH_SIZE

    # Exponential decay of learning rate
    learning_rate = tf.train.exponential_decay(
        learning_rate=LEARNING_RATE_BASE,
        global_step=global_step,
        decay_steps=decay_steps,
        decay_rate=LEARNING_RATE_DECAY,
        staircase=False)

    train_step = tf.train.AdagradOptimizer(learning_rate).minimize(
        tot_loss, global_step=global_step)
    train_op = tf.group(train_step, variables_averages_op)

    saver = tf.train.Saver(max_to_keep=6)
    with tf.Session() as sess:
        tf.global_variables_initializer().run()

        train_losses = []

        #Train
        cur_step = 0

        while (cur_step < TRAINING_STEPS):
            for cur_video_index in range(num_videos):
                first_flag = 1
                cur_training_features = training_features[cur_video_index]
                for i in range(training_num[cur_video_index]):
                    start = i
                    end = start + BATCH_SIZE

                    if (first_flag):
                        train_features = [
                            np.transpose(
                                cur_training_features[j:j + INPUT_DEPTH],
                                axes=[1, 2, 0]) for j in range(BATCH_SIZE)
                        ]
                        first_flag = 0
                    else:
                        train_features.pop(0)
                        train_features.append(
                            np.transpose(
                                cur_training_features[end - 1:end - 1 +
                                                      INPUT_DEPTH],
                                axes=[1, 2, 0]))

                    train_targets = train_features.copy()
                    train_feed = {
                        features: train_features,
                        targets: train_targets
                    }

                    if (cur_step % 50 == 0):
                        train_loss = sess.run(tot_loss, feed_dict=train_feed)
                        reg_loss = sess.run(reg_score, feed_dict=train_feed)

                        print(
                            "After %d training step(s), loss on training is: %g, regularization cost is:  %g"
                            % (cur_step, train_loss, reg_loss))
                        print(
                            "The current video is: %d, the batch frame is from %d to %d"
                            % (cur_video_index, start, end))

                        train_losses.append(train_loss)

                    if (cur_step % 100 == 0):
                        saver.save(sess,
                                   os.path.join(MODEL_PATH, MODEL_NAME),
                                   global_step=global_step)
                    #if(cur_step%1000 == 0):
                    sess.run(train_op, feed_dict=train_feed)
                    cur_step = cur_step + 1
                    if (cur_step >= TRAINING_STEPS):
                        break

                if (cur_step >= TRAINING_STEPS):
                    break
    print(train_losses)
Пример #6
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)
Пример #7
0
# validation_init_op = iterator.make_initializer(validation_dataset)

training_init_op = iterator.make_initializer(Train_ds)
validation_init_op = iterator.make_initializer(Test_ds)



###################################################################

EPOCHS = 100
# BATCH_SIZE = 

parameters = initialize_parameters(3)
# Z3 = CNN_encoder(features,parameters=parameters)

Z3 = forward_propagation(features,parameters=parameters)

#Z3 = spectrum_cnn2(features)

# fscore , update= tf.contrib.metrics.f1_score(labels,Z3)
test_scores = []
train_scores = []

cost = compute_cost(Z3,labels)
train_op = tf.train.AdamOptimizer().minimize(cost)
# optimiser = tf.train.AdamOptimizer().minimize(fscore)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    steps_per_epoch = tf.cast(tf.ceil(training_buffer_size/BATCH_SIZE),tf.int32).eval()
    print("Steps per epoch: "+str(steps_per_epoch))