Пример #1
0
def run_task():
    print(sum(x_train[0].tolist(), []))
    print(sum(y_train[0].tolist(), []))

    model = compiled_tcn(num_feat=1,
                         num_classes=10,
                         nb_filters=10,
                         kernel_size=8,
                         dilations=[2**i for i in range(9)],
                         nb_stacks=2,
                         max_len=x_train[0:1].shape[1],
                         activation='norm_relu',
                         use_skip_connections=True,
                         return_sequences=True)

    print(f'x_train.shape = {x_train.shape}')
    print(f'y_train.shape = {y_train.shape}')

    psv = PrintSomeValues()

    # Using sparse softmax.
    # http://chappers.github.io/web%20micro%20log/2017/01/26/quick-models-in-keras/
    model.summary()

    model.fit(x_train,
              y_train,
              validation_data=(x_test, y_test),
              epochs=1000,
              callbacks=[psv],
              batch_size=256)
Пример #2
0
def run_task():
    model = tcn.compiled_tcn(return_sequences=False,
                             num_feat=x_train.shape[2],
                             num_classes=0,
                             nb_filters=24,
                             kernel_size=8,
                             dilations=[2**i for i in range(9)],
                             nb_stacks=1,
                             max_len=x_train.shape[1],
                             use_skip_connections=True,
                             regression=True,
                             dropout_rate=0)

    print(f'x_train.shape = {x_train.shape}')
    print(f'y_train.shape = {y_train.shape}')

    psv = PrintSomeValues()

    # Using sparse softmax.
    # http://chappers.github.io/web%20micro%20log/2017/01/26/quick-models-in-keras/
    model.summary()

    model.fit(x_train,
              y_train,
              validation_data=(x_test, y_test),
              epochs=500,
              callbacks=[psv],
              batch_size=256)
Пример #3
0
def run_task():
    model = compiled_tcn(num_feat=1,
                         num_classes=10,
                         nb_filters=10,
                         kernel_size=8,
                         dilations=[2**i for i in range(9)],
                         nb_stacks=1,
                         max_len=x_train[0:1].shape[1],
                         use_skip_connections=True,
                         opt='rmsprop',
                         lr=5e-4,
                         use_weight_norm=True,
                         return_sequences=True)

    print(f'x_train.shape = {x_train.shape}')
    print(f'y_train.shape = {y_train.shape}')

    psv = PrintSomeValues()

    # Using sparse softmax.
    # http://chappers.github.io/web%20micro%20log/2017/01/26/quick-models-in-keras/
    model.summary()

    model.fit(x_train,
              y_train,
              validation_data=(x_test, y_test),
              epochs=100,
              callbacks=[psv],
              batch_size=256)

    test_acc = model.evaluate(x=x_test, y=y_test)[1]  # accuracy.
    with open(f'copy_memory_{str(uuid4())[0:5]}.txt', 'w') as w:
        w.write(str(test_acc) + '\n')
Пример #4
0
def run_task():
    model = compiled_tcn(return_sequences=False,
                         num_feat=x_train.shape[2],
                         num_classes=0,
                         nb_filters=24,
                         kernel_size=8,
                         dilations=[2**i for i in range(9)],
                         nb_stacks=1,
                         max_len=x_train.shape[1],
                         use_skip_connections=True,
                         regression=True,
                         dropout_rate=0)

    print(f'x_train.shape = {x_train.shape}')
    print(f'y_train.shape = {y_train.shape}')

    psv = PrintSomeValues()

    # Using sparse softmax.
    # http://chappers.github.io/web%20micro%20log/2017/01/26/quick-models-in-keras/
    model.summary()

    model.fit(x_train,
              y_train,
              validation_data=(x_test, y_test),
              epochs=15,
              callbacks=[psv],
              batch_size=256)

    test_acc = model.evaluate(x=x_test, y=y_test, verbose=0)  # loss.
    with open(f'adding_problem_{str(uuid4())[0:5]}.txt', 'w') as w:
        w.write(str(test_acc) + '\n')
Пример #5
0
def run_task():
    (x_train, y_train), (x_test, y_test) = data_generator()

    model = compiled_tcn(return_sequences=False,
                         num_feat=1,
                         num_classes=10,
                         nb_filters=25,
                         kernel_size=7,
                         dilations=[2**i for i in range(9)],
                         nb_stacks=2,
                         max_len=x_train[0:1].shape[1],
                         activation='norm_relu',
                         use_skip_connections=True)

    print(f'x_train.shape = {x_train.shape}')
    print(f'y_train.shape = {y_train.shape}')
    print(f'x_test.shape = {x_test.shape}')
    print(f'y_test.shape = {y_test.shape}')

    model.summary()

    model.fit(x_train,
              y_train.squeeze().argmax(axis=1),
              epochs=100,
              validation_data=(x_test, y_test.squeeze().argmax(axis=1)))
Пример #6
0
def fit_tcn(lag=HISTORY_LAG,
            nb_filters=32,
            kernel_size=5,
            nb_stacks=1,
            verbose=0,
            epochs=100,
            batch_size=16):
    """Learn TCN"""
    #dilations can be infered from input size (lag)
    dilations = [2**i for i in range(0, int(math.log(lag) / math.log(2)))]
    model = tcn.compiled_tcn(
        1,  # num_feat
        1,  # num_classes
        nb_filters,
        kernel_size,
        dilations,
        nb_stacks,
        lag,
        regression=True)
    result = fit(model,
                 lag=lag,
                 verbose=verbose,
                 epochs=epochs,
                 batch_size=batch_size,
                 cut_predict=True)
    print("TCN lag:", lag, "units:", nb_filters, "ksize:", kernel_size,
          "stacks:", nb_stacks, "rmse:", result)
    return result
Пример #7
0
 def __init__(self, vocabulary=None, clf=None):
     self.labeler = BinaryLabels()
     #self.labeler = Relabeled2Agree()
     self.extracter = EmbededTextWordsFeatures()
     self.clf = compiled_tcn(nb_filters=128,
                             kernel_size=2,
                             nb_stacks=4,
                             dilations=[1, 2, 4, 8, 16, 32, 64],
                             activation='norm_relu',
                             padding='causal',
                             use_skip_connections=True,
                             dropout_rate=0.5,
                             return_sequences=False,
                             num_feat=128,
                             num_classes=2,
                             max_len=128)
Пример #8
0
def run_task():
    model = compiled_tcn(return_sequences=False,
                         num_feat=x_train.shape[2],
                         num_classes=0,
                         nb_filters=24,
                         kernel_size=8,
                         dilations=[2**i for i in range(9)],
                         nb_stacks=1,
                         max_len=x_train.shape[1],
                         use_skip_connections=False,
                         use_weight_norm=True,
                         regression=True,
                         dropout_rate=0)

    tcn_full_summary(model)
    model.fit(x_train,
              y_train,
              validation_data=(x_test, y_test),
              epochs=15,
              batch_size=256,
              callbacks=[PrintSomeValues()])
Пример #9
0
def run_task(sequence_length=8):
    x_train, y_train = data_generator(batch_size=2048,
                                      sequence_length=sequence_length)
    print(x_train.shape)
    print(y_train.shape)
    model = compiled_tcn(return_sequences=False,
                         num_feat=1,
                         num_classes=10,
                         nb_filters=10,
                         kernel_size=10,
                         dilations=[1, 2, 4, 8, 16, 32],
                         nb_stacks=6,
                         max_len=x_train[0:1].shape[1],
                         use_skip_connections=False)

    print(f'x_train.shape = {x_train.shape}')
    print(f'y_train.shape = {y_train.shape}')

    # model.summary()

    model.fit(x_train, y_train, epochs=5)
    return model.evaluate(x_train, y_train)[1]
Пример #10
0
def run_task(length_of_convolution = 3,
    kernel_size=3,  # type: int
    dilations=[2 ** i for i in range(9)],  # type: List[int]
    nb_stacks=1,  # type: int
    use_skip_connections=True,  # type: bool
    return_sequences=True, #uncertain about this parameter
    dropout_rate=0.05,  # type: float
    epochs = 100,
    name="run",
    create_plot = False):

    #the portion of the total data set that is dedicated to training
    portion_training_set = .8

    (x_train, y_train), (x_test, y_test) = data_generator(args.CSVFile, length_of_convolution, portion_training_set)

    model = compiled_tcn(num_feat=1,  # type: int
                         num_classes=1,  # type: int
                         nb_filters=20,  # type: int
                         kernel_size=kernel_size,  # type: int
                         dilations=dilations,  # type: List[int]
                         nb_stacks=nb_stacks,  # type: int
                         max_len=None,  # type: int
                         padding='causal',  # type: str
                         use_skip_connections=use_skip_connections,  # type: bool
                         return_sequences=return_sequences, #uncertain about this parameter
                         regression=True,  # type: bool
                         dropout_rate=dropout_rate,  # type: float
                         name=name  # type: str
                         )

    print(f'x_train.shape = {x_train.shape}')
    print(f'y_train.shape = {y_train.shape}')
    print(f'x_test.shape = {x_test.shape}')
    print(f'y_test.shape = {y_test.shape}')

    model.summary()
    history = model.fit(x_train, y_train, epochs=epochs, validation_data=(x_test, y_test))
    loss = history.history['loss']

    # Plot training & validation loss values
    if create_plot:
        plt.plot(loss)
        #plt.plot(history.history['val_loss'])
        plt.title('Model loss')
        plt.ylabel('Loss')
        plt.xlabel('Epoch')
        plt.legend(['Train', 'Test'], loc='upper left')
        plt.show()

    #output parameters file
    average_loss = str(sum(loss) / len(loss))
    file = open(average_loss + "-" + name + ".csv", "w+")
    file.write("kernel_size, " + str(kernel_size) + "\n" +
               "dilations, " + str(dilations) + "\n" +
               "nb_stacks, " + str(nb_stacks) + "\n" +
               "use_skip_connections, " + str(use_skip_connections) + "\n" +
               "return_sequences, " + str(return_sequences) + "\n" + #uncertain about this parameter
               "dropout_rate, " + str(dropout_rate) + "\n" +
               "epochs, " + str(epochs) + "\n" +
               "name, " + str(name) + "\n" +
               "input file, " + str(args.CSVFile) + "\n" +
               "average loss, " + average_loss + "\n" +
               "loss, " + str(loss) + "\n")
Пример #11
0
def run_task():
    getFolderNamesInRootDir()
    create_class_names()

    print('loading saved data...')
    x_train = np.load('x_train.npy')
    y_train = np.load('y_train.npy')

    x_val = np.load('x_val.npy')
    y_val = np.load('y_val.npy')

    x_test = np.load('x_test.npy')
    y_test = np.load('y_test.npy')

    encoder = LabelBinarizer()
    y_train_one_hot_label = encoder.fit_transform(y_train)
    y_test_one_hot_label = encoder.fit_transform(y_test)
    y_val_one_hot_label = encoder.fit_transform(y_val)
    print(y_test_one_hot_label)
    '''
	y_train_one_hot_label = to_categorical(y_train, wordCount)
	y_test_one_hot_label = to_categorical(y_test, wordCount)
	y_val_one_hot_label = to_categorical(y_val, wordCount)
	'''

    y_train_one_hot_label = np.expand_dims(y_train_one_hot_label, axis=2)
    y_test_one_hot_label = np.expand_dims(y_test_one_hot_label, axis=2)
    y_val_one_hot_label = np.expand_dims(y_val_one_hot_label, axis=2)

    model = compiled_tcn(return_sequences=False,
                         num_feat=1,
                         num_classes=wordCount,
                         nb_filters=20,
                         kernel_size=5,
                         dilations=[2**i for i in range(9)],
                         nb_stacks=1,
                         max_len=x_train[0:1].shape[1],
                         use_skip_connections=False)

    print(f'x_train.shape = {x_train.shape}')
    print(f'y_train.shape = {y_train_one_hot_label.shape}')
    print(f'x_test.shape = {x_test.shape}')
    print(f'y_test.shape = {y_test_one_hot_label.shape}')

    model.summary()
    now = datetime.now()
    plot_file_name = 'plots/tcn_model_plot_' + now.strftime(
        "%d_%m_%Y_%H%M%S") + '.png'
    plot_model(model,
               to_file=plot_file_name,
               show_shapes=True,
               show_layer_names=True)
    '''
	y_1 = y_train_one_hot_label.squeeze().argmax(axis=1)
	y_2 = y_test_one_hot_label.squeeze().argmax(axis=1)
	'''
    y_train_one_hot_label = y_train_one_hot_label.squeeze().argmax(axis=1)
    y_test_one_hot_label = y_test_one_hot_label.squeeze().argmax(axis=1)
    y_val_one_hot_label = y_val_one_hot_label.squeeze().argmax(axis=1)
    history = model.fit(x_train,
                        y_train_one_hot_label,
                        epochs=50,
                        validation_data=(x_test, y_test_one_hot_label),
                        callbacks=[tensorboard_callback])

    create_save_plots(history, model, x_train, y_train, x_test, y_test, x_val,
                      y_val, y_train_one_hot_label, y_test_one_hot_label,
                      y_val_one_hot_label)
    evaluate_model(model, x_test, y_test_one_hot_label, x_val,
                   y_val_one_hot_label)
Пример #12
0
def get_architecture(n):
    model = Sequential()
    input_shape = (time_lags[n], number_of_features)
    if n == 1:
        input_shape = input_shape
        model.add(LSTM(16, input_shape=input_shape, return_sequences=False))
        model.add(Dropout(.8))
        model.add(Dense(1, activation='linear'))
        model.compile(
            loss='mae',
            optimizer='adam',
        )
    elif n == 2:
        model.add(LSTM(64, input_shape=input_shape, return_sequences=True))
        model.add(Dropout(.6))
        model.add(LSTM(32, input_shape=input_shape, return_sequences=False))
        model.add(Dropout(.6))
        model.add(Dense(1, activation='linear'))
        model.compile(
            loss='mae',
            optimizer='adam',
        )
    elif n == 3:
        model.add(LSTM(128, input_shape=input_shape, return_sequences=True))
        model.add(LSTM(64, input_shape=input_shape, return_sequences=True))
        model.add(LSTM(32, input_shape=input_shape, return_sequences=False))
        model.add(Dropout(.6))
        model.add(Dense(32))
        model.add(Activation('relu'))
        model.add(Dropout(.4))
        model.add(Dense(16))
        model.add(Activation('relu'))
        model.add(Dropout(.2))
        model.add(Dense(8))
        model.add(Activation('relu'))
        model.add(Dense(1, activation='linear'))
        model.compile(
            loss='mae',
            optimizer='adam',
        )
    elif n == 4:
        model = compiled_tcn(
            return_sequences=False,
            num_feat=number_of_features,
            num_classes=0,
            nb_filters=6,
            kernel_size=2,
            dilations=[1, 2, 4],
            # dilations=[2 ** i for i in range(2, 5)],
            nb_stacks=1,
            max_len=time_lags[n],
            use_skip_connections=True,
            regression=True,
            dropout_rate=0.2)
    elif n == 5:
        model = Sequential()
        model.add(
            Conv1D(32,
                   2,
                   activation='relu',
                   padding='causal',
                   input_shape=input_shape))
        model.add(Dropout(0.5))
        model.add(Flatten())
        model.add(Dense(1, activation='linear'))
        model.compile(
            loss='mae',
            optimizer='adam',
        )
    elif n == 6:
        model.add(
            Dense(64,
                  input_dim=29,
                  kernel_initializer='normal',
                  activation='relu'))
        model.add(Dropout(.2))
        model.add(Dense(32, kernel_initializer='normal', activation='relu'))
        model.add(Dropout(.2))
        model.add(Dense(1, kernel_initializer='normal'))
        model.compile(
            loss='mae',
            optimizer='adam',
        )
    print(model.summary())
    return model
Пример #13
0
    pickle.dump([mean_in, std_in], f)

#  Reshape to keras tensor
x_train = np.expand_dims(x_train, axis=2)
x_test = np.expand_dims(x_test, axis=2)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')

# %% Create TCN
model = compiled_tcn(return_sequences=False,
                     num_feat=x_train.shape[2],
                     num_classes=len(np.unique(y_train)),
                     nb_filters=128,
                     kernel_size=9,
                     dilations=[2**i for i in range(7)],
                     nb_stacks=1,
                     dropout_rate=0.3,
                     use_batch_norm=False,
                     max_len=x_train[0:1].shape[1],
                     use_skip_connections=True,
                     use_layer_norm=True,
                     opt='adam')
model.summary()

#  Train
cnnhistory = model.fit(x_train,
                       y_train,
                       batch_size=16,
                       validation_data=(x_test, y_test),
                       epochs=10,
                       verbose=1)
Пример #14
0
    x_total = data[index_train]
    x_test = data[index_test]
    y_total = label[index_train]
    y_test = label[index_test]
    x_train, x_valid, y_train, y_valid = train_test_split(x_total,
                                                          y_total,
                                                          test_size=0.1,
                                                          random_state=1)
    y_valid1 = np.expand_dims(y_valid, axis=1)
    #y_test = np.expand_dims(y_test, axis = 1)
    y_valid3 = to_categorical(y_valid, num_classes=4)
    model = compiled_tcn(return_sequences=False,
                         num_feat=4,
                         num_classes=4,
                         nb_filters=50,
                         kernel_size=4,
                         dilations=[2**i for i in range(9)],
                         nb_stacks=2,
                         max_len=x_train[0:1].shape[1],
                         activation='norm_relu',
                         use_skip_connections=True)

    score1 = np.ones(2)
    #model.summary()
    cbs = [Snapshot('snapshots', nb_epochs=120, verbose=0, nb_cycles=8)]
    #s_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
    #logs_path='G:/deap/Result/logs/log_%s'%(s_time)
    #try:
    #    os.makedirs(logs_path)
    #except:
    #    pass
    #tensorboard=TensorBoard(log_dir=logs_path,histogram_freq=1,write_graph=True)
Пример #15
0
# including Australia, British, Canada, Switzerland, China, Japan, New Zealand and
# Singapore ranging from 1990 to 2016.
# task: predict multi-column daily exchange rate from history

folds, enc = get_xy_kfolds()
mse_list = []

if __name__ == '__main__':
    mse_list = []
    for train_x, train_y, test_x, test_y in folds:
        model = compiled_tcn(return_sequences=False,
                             num_feat=test_x.shape[1],
                             nb_filters=24,
                             num_classes=0,
                             kernel_size=8,
                             dilations=[2**i for i in range(9)],
                             nb_stacks=1,
                             max_len=test_x.shape[0],
                             use_skip_connections=True,
                             regression=True,
                             dropout_rate=0,
                             output_len=test_y.shape[0])
        model.fit(train_x, train_y, batch_size=256, epochs=100)
        y_raw_pred = model.predict(np.array([test_x]))
        y_pred = enc.inverse_transform(y_raw_pred).flatten()
        y_true = enc.inverse_transform([test_y]).flatten()
        mse_cur = mean_squared_error(y_true, y_pred)
        mse_list.append(mse_cur)
        print(f"train_set_size:{train_x.shape[0]}")
        print(f"y_true:{y_true}")
        print(f"y_pred:{y_pred}")
        print(f"mse:{mse_cur}")
Пример #16
0
def run_task():
    model = compiled_tcn(return_sequences=False,
                         num_feat=x_train.shape[2],
                         num_classes=0,
                         nb_filters=36,
                         kernel_size=8,
                         dilations=[2 ** i for i in range(5)],
                         nb_stacks=1,
                         max_len=x_train.shape[1],
                         use_skip_connections=True,
                         regression=True,
                         dropout_rate=0.0)

    print(f'x_train.shape = {x_train.shape}')
    print(f'y_train.shape = {y_train.shape}')

    psv = PrintSomeValues()

    # Using sparse softmax.
    # http://chappers.github.io/web%20micro%20log/2017/01/26/quick-models-in-keras/
    model.summary()



    # 创建一个权重文件保存文件夹logs
    log_dir = "logs/"
    # 记录所有训练过程,每隔一定步数记录最大值
    tensorboard = keras.callbacks.TensorBoard(log_dir=log_dir)
    checkpoint = keras.callbacks.ModelCheckpoint(log_dir + "best_model.h5",
                                 monitor="val_loss",
                                 mode='min',
                                 save_weights_only=False,
                                 save_best_only=True,
                                 verbose=1,
                                 period=1)

    callback_lists = [tensorboard, checkpoint]

    # history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=50,
    #          callbacks=[psv], batch_size=128)

    history = model.fit(x_train, y_train, validation_split=0.1, shuffle=True, epochs=100,
                        callbacks=callback_lists, batch_size=32)


    pyplot.plot(history.history['loss'])
    pyplot.plot(history.history['val_loss'])
    pyplot.title('model train vs validation loss')
    pyplot.ylabel('loss')
    pyplot.xlabel('epoch')
    pyplot.legend(['train', 'validation'], loc='upper right')
    pyplot.show()

    # 保存模型
    model.save('my_model.h5')

    model = keras.models.load_model('logs/best_model.h5')

    y_pred = model.predict(x_test)

    x_axix = range(len(y_test))

    plt.plot(x_axix, y_test, color='green', label='test_ture')
    plt.plot(x_axix, y_pred, color='red', label='test_tcn')
    plt.legend()  # 显示图例
    plt.xlabel('times')
    plt.ylabel('solar')
    plt.show()
    test_mae_score, test_mse_score = model.evaluate(x_test, y_test, verbose=1)
    print('Test mse score:', test_mse_score)
    print('Test mae score:', test_mae_score)