Esempio n. 1
0
    def Training_click(self,
                       orgPath,
                       batch_size,
                       epochs,
                       feature=None,
                       valPath=None):
        orgLrn_Path = orgPath[0]
        orgTrg_Path = orgPath[1]

        #---------------
        # 型変換
        #---------------
        if batch_size != int:
            batch_size = int(batch_size)
        if epochs != int:
            epochs = int(epochs)
        #---------------------
        # データの読み込み
        #---------------------
        x = dataLoad(orgLrn_Path, float)
        t = dataLoad(orgTrg_Path, float)
        #-------------------------------
        # DataFeature
        #-------------------------------
        if feature != None:
            x = Datafeature(x, feature)
            t = Datafeature(t, feature)
        #-----------------------------
        # Validationデータの読み込み
        #-----------------------------
        if valPath != None:
            valLRN_Path = valPath[0]
            valTrg_Path = valPath[1]
            x_val = dataLoad(valLRN_Path, float)
            t_val = dataLoad(valTrg_Path, float)
            #-------------------------------
            # DataFeature
            #-------------------------------
            if feature != None:
                x_val = Datafeature(x_val, feature)
                t_val = Datafeature(t_val, feature)

                validation = (x_val, t_val)
        else:
            validation = None

        # 学習曲線を可視化するコールバックを用意する
        higher_better_metrics = ['r2']
        visualize_cb = LearningVisualizationCallback(higher_better_metrics)
        callbacks = [
            visualize_cb,
        ]

        self.model.fit(x=x,
                       t=t,
                       batch_size=batch_size,
                       epochs=epochs,
                       validation=validation,
                       callbacks=callbacks)
Esempio n. 2
0
def nn(x, t, batch_size, epochs, feature=None, validation=None):
    """
    簡単なニューラルネットワークのモデルを作成する関数

    Parameters
    ----------
    x : ndarray
        学習用データ
    t : ndarray
        教師データ
    batch_size : int
        バッチサイズ
    eopchs : int
        エポック数
    feature : int
        Feature Scalingの選択
    """       
    #-------------------------------
    # DataFeature
    #-------------------------------
    if feature != None:
        x = Datafeature(x, feature)
        t = Datafeature(t, feature)

    #-------------------------------
    # Validation
    #-------------------------------
    if validation != None:    # バリデーションが最初からセットされているとき
        x_val = validation[0]
        t_val = validation[1]
    else:
        x = __shuffle__(x)
        t = __shuffle__(t)
        x_val, x = __sorting__(x, 100)
        t_val, t = __sorting__(t, 100)

    # 学習曲線を可視化するコールバックを用意する
    higher_better_metrics = ['r2']
    visualize_cb = LearningVisualizationCallback(higher_better_metrics)
    callbacks = [
        visualize_cb,
    ]

    model = Sequential()
    model.add(Input(input_shape=x.shape[1]))
    model.add(Dense(50, activation='relu', weight_initializer='relu'))
    model.add(Dense(50, activation='relu', weight_initializer='relu'))
    #model.add(Dense(50, activation='sigmoid', weight_initializer='sigmoid'))
    #model.add(Dense(50, activation='sigmoid', weight_initializer='sigmoid'))
    #model.add(Dense(t.shape[1],  activation='softmax'))
    #model.compile(loss='cross_entropy_error')
    model.add(Dense(t.shape[1], activation = 'liner'))
    model.compile(loss='mean_squared_error', metrics = ['r2', 'rsme'])

    #history=model.fit(x, t, batch_size=batch_size, epochs=epochs, validation=validation)
    history = model.fit(x, t, batch_size=batch_size,
                        epochs=epochs, validation=(x_val, t_val), callbacks=callbacks)

    # lossグラフ
    loss = history['loss_ave']
    val_loss = history['val_loss']

    nb_epoch = len(loss)
    plt.plot(range(nb_epoch), loss, marker = '.', label = 'loss')
    plt.plot(range(nb_epoch), val_loss, marker='.', label='val_loss')
    plt.legend(loc = 'best', fontsize = 10)
    plt.grid()
    plt.xlabel('epoch')
    plt.ylabel('loss')
    plt.show()