Esempio n. 1
0
def train(epochs, batch_size):

    train_ds = tf.data.Dataset.from_tensor_slices(
        (tf.cast(X_train, tf.float32), tf.cast(Y_train, tf.float32))).shuffle(500).batch(batch_size)

    test_ds = tf.data.Dataset.from_tensor_slices(
        (tf.cast(X_test, tf.float32), tf.cast(Y_test, tf.float32))).shuffle(200).batch(batch_size)

    model = FM()
    optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)
    accuracy = BinaryAccuracy(threshold=0.5)

    for i in range(epochs):
        loss_history = []

        for x, y in train_ds:
            loss = train_on_batch(model, optimizer, accuracy, x, y)
            loss_history.append(loss)

        if i % 2== 0:
            print("스텝 {:03d}에서 Loss: {:.4f}".format(i, np.mean(loss_history)))
            print("스텝 {:03d}에서 누적 train 정확도: {:.4f}".format(i, accuracy.result().numpy()))


    test_accuracy = BinaryAccuracy(threshold=0.5)
    for x, y in test_ds:
        y_pred = model(x)
        test_accuracy.update_state(y, y_pred)

    print("테스트 정확도: {:.4f}".format(test_accuracy.result().numpy()))
Esempio n. 2
0
def train(epochs):
    train_ds, test_ds, field_dict, field_index = get_data()

    model = DeepFM(embedding_size=config.EMBEDDING_SIZE, num_feature=len(field_index),
                   num_field=len(field_dict), field_index=field_index)

    optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)

    print("Start Training: Batch Size: {}, Embedding Size: {}".format(config.BATCH_SIZE, config.EMBEDDING_SIZE))
    start = perf_counter()
    for i in range(epochs):
        acc = BinaryAccuracy(threshold=0.5)
        auc = AUC()
        loss_history = []

        for x, y in train_ds:
            loss = train_on_batch(model, optimizer, acc, auc, x, y)
            loss_history.append(loss)

        print("Epoch {:03d}: 누적 Loss: {:.4f}, Acc: {:.4f}, AUC: {:.4f}".format(
            i, np.mean(loss_history), acc.result().numpy(), auc.result().numpy()))

    test_acc = BinaryAccuracy(threshold=0.5)
    test_auc = AUC()
    for x, y in test_ds:
        y_pred = model(x)
        test_acc.update_state(y, y_pred)
        test_auc.update_state(y, y_pred)

    print("테스트 ACC: {:.4f}, AUC: {:.4f}".format(test_acc.result().numpy(), test_auc.result().numpy()))
    print("Batch Size: {}, Embedding Size: {}".format(config.BATCH_SIZE, config.EMBEDDING_SIZE))
    print("걸린 시간: {:.3f}".format(perf_counter() - start))
    model.save_weights('weights/weights-epoch({})-batch({})-embedding({}).h5'.format(
        epochs, config.BATCH_SIZE, config.EMBEDDING_SIZE))
Esempio n. 3
0
    def train(self, save=True):
        x, y = zip(*islice(ImageProvider().provide(), 20000))
        x, y = np.array(x), np.array(y)
        X_train, X_test, y_train, y_test = train_test_split(x,
                                                            y,
                                                            train_size=.66)

        X_train = X_train.transpose(1, 0, 2, 3)
        X_test = X_test.transpose(1, 0, 2, 3)

        self.net.fit(
            [X_train[0][..., np.newaxis], X_train[1][..., np.newaxis]],
            y_train,
            batch_size=16,
            validation_split=0.1,
            epochs=30)

        if save:
            self.net.save_weights('imagenet')

        m = BinaryAccuracy()
        y_pred = self.net.predict(
            [X_test[0][..., np.newaxis], X_test[1][..., np.newaxis]])
        m.update_state(y_test, y_pred)
        print(m.result())
Esempio n. 4
0
def Acc(y_t, y_p):
    """Computes accuracy for each label
    Argument: ground truth label Y (2D reshaped), prediction (2D reshaped)
    Returns: array of accuracy for each label"""

    a = BinaryAccuracy() #handles the fact that y_p is not given as a binary vector
    Acc_per_label = []

    for i in range(8):
        a.update_state(y_t[:,i], y_p[:,i] )
        acc = a.result().numpy()
        a.reset_states()
        Acc_per_label.append(acc)
    return Acc_per_label
Esempio n. 5
0
def print_bin_predictions_match(y_test, yhat, binary=True):
    accuracy = BinaryAccuracy() if binary else SparseCategoricalAccuracy()
    for i in range(y_test.shape[0]):
        ix = colored(str(f'{i:02d} |'), 'grey')
        sep = colored('|', 'grey')
        y = int(y_test[i][0])
        pred = f"{yhat[i][0]:.02f}" if binary else f"{np.argmax(yhat[i])}"
        accuracy.reset_states()
        accuracy.update_state(y, yhat[i])
        true_pred = accuracy.result().numpy() == 1.0
        color = "green" if true_pred else "red"
        pred = colored(pred, color)
        if i % 9 == 0:
            print(f"\n{ix} ", end='')
        print(f"{y} {sep} {pred} {sep} ", end="")
    print(colored("\n", "white"))
Esempio n. 6
0
def Fmrs(request, pk=None):
    gbinary()
    print('hi')
    # GPU 확인
    tf.config.list_physical_devices('GPU')
    # 자료형 선언
    tf.keras.backend.set_floatx('float32')

    # 데이터 로드
    scaler = MinMaxScaler()
    X = pickle.load(open('knn_models/X.pkl', 'rb'))
    print('X', X)
    Y = pickle.load(open('knn_models/Y.pkl', 'rb'))
    print('Y', Y)
    X = scaler.fit_transform(X)

    n = X.shape[0]
    p = X.shape[1]
    k2 = Y.shape[1]
    k = 10
    batch_size = 8
    epochs = 10
    X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, stratify=Y)

    train_ds = tf.data.Dataset.from_tensor_slices(
        (tf.cast(X_train, tf.float32), tf.cast(Y_train, tf.float32))).shuffle(500).batch(8)

    test_ds = tf.data.Dataset.from_tensor_slices(
        (tf.cast(X_test, tf.float32), tf.cast(Y_test, tf.float32))).shuffle(200).batch(8)
    class FM(tf.keras.Model):
      def __init__(self):
          super(FM, self).__init__()

          # 모델의 파라미터 정의
          self.w_0 = tf.Variable([0.0])
          self.w = tf.Variable(tf.zeros([p]))
          self.V = tf.Variable(tf.random.normal(shape=(p, k)))

      def call(self, inputs):
          linear_terms = tf.reduce_sum(tf.math.multiply(self.w, inputs), axis=1)

          interactions = 0.5 * tf.reduce_sum(
              tf.math.pow(tf.matmul(inputs, self.V), 2)
              - tf.matmul(tf.math.pow(inputs, 2), tf.math.pow(self.V, 2)),
              1,
              keepdims=False
          )

          y_hat = tf.math.sigmoid(self.w_0 + linear_terms + interactions)

          return y_hat

    model = FM()
    optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)
    accuracy = BinaryAccuracy(threshold=0.5)
    loss_history = []

    for i in range(epochs):
      for x, y in train_ds:
          loss = train_on_batch(model, optimizer, accuracy, x, y)
          loss_history.append(loss)

      if i % 2== 0:
          print("스텝 {:03d}에서 누적 평균 손실: {:.4f}".format(i, np.mean(loss_history)))
          print("스텝 {:03d}에서 누적 정확도: {:.4f}".format(i, accuracy.result().numpy()))


    test_accuracy = BinaryAccuracy(threshold=0.5)
    for x, y in test_ds:
        y_pred = model(x)
        test_accuracy.update_state(y, y_pred)

    print("테스트 정확도: {:.4f}".format(test_accuracy.result().numpy()))

    filename = 'knn_models/fm.pkl'
    pickle.dump(model, open(filename, 'wb'))

    return HttpResponse({'message':'end'}, content_type="text/json-comment-filtered", status=status.HTTP_200_OK)