def make_model(input_shape, nb_epochs=100, batch_size=128, lr=0.01, n_layers=1, n_hidden=16, rate_dropout=0.3): model_path = '/output/model.{}.{}c.{}l.{}'.format(input_shape[0], n_hidden, n_layers, nb_epochs) wp = WindPuller(input_shape=input_shape, lr=lr, n_layers=n_layers, n_hidden=n_hidden, rate_dropout=rate_dropout) train_set, test_set = read_ultimate("/dataset/", input_shape) wp.fit(train_set.images, train_set.labels, batch_size=batch_size, nb_epoch=nb_epochs, shuffle=False, verbose=1, # validation_split=0.02, validation_data=(test_set.images, test_set.labels), callbacks=[ TensorBoard(log_dir='/output/logs', histogram_freq=100), ModelCheckpoint(filepath=model_path + '.best', save_best_only=True, mode='min') ]) scores = wp.evaluate(test_set.images, test_set.labels, verbose=0) print('Test loss:', scores[0]) print('Test accuracy:', scores[1]) wp.model.save(model_path) saved_wp = wp.load_model(model_path) scores = saved_wp.evaluate(test_set.images, test_set.labels, verbose=0) print('Test loss:', scores[0]) print('test accuracy:', scores[1]) pred = saved_wp.predict(test_set.images, 1024) # print(pred) # print(test_set.labels) pred = numpy.reshape(pred, [-1]) result = numpy.array([pred, test_set.labels]).transpose() with open('output.' + str(input_shape[0]), 'w') as fp: for i in range(result.shape[0]): for val in result[i]: fp.write(str(val) + "\t") fp.write('\n')
def evaluate_model(model_path, code, output_dir, input_shape=[30, 61]): extract_from_file("dataset/%s.csv" % code, output_dir, code) train_set, test_set = read_feature(output_dir, input_shape, code) saved_wp = WindPuller(input_shape).load_model(model_path) scores = saved_wp.evaluate(test_set.images, test_set.labels, verbose=0) print('Test loss:', scores[0]) print('test accuracy:', scores[1]) pred = saved_wp.predict(test_set.images, 1024) [cr, cap] = calculate_cumulative_return(test_set.labels, pred) # Output to a csv file # Read in the date, close from original data file. days_for_test = 700 tmp = pd.read_csv('dataset/%s.csv' % code, delimiter='\t') # tmp.columns = ['date', 'open', 'high', 'low', 'close', 'volume'] date = tmp['date'][-days_for_test:] close = tmp['close'][-days_for_test:] output = pd.DataFrame( { 'Return': test_set.labels, 'Position': pred.reshape(-1), 'Capital': cap.reshape(-1), 'Close': close.values }, index=date, columns=['Close', 'Return', 'Position', 'Capital']) output.to_csv('output/%s.csv' % code)
def make_model_type3(input_shape, nb_epochs=100, batch_size=128, lr=0.01, n_layers=1, n_hidden=16, rate_dropout=0.3): model_path = 'model.%s' % input_shape[0] windowSize = input_shape[0] # num minutes X_train, y_train, X_val, Y_val, X_test, y_test, numFeatures = ft.generateDataSetTXF( os.getcwd(), input_wind_size=input_shape[0], toDataSet=False) input_shape[0] = numFeatures wp = WindPuller(input_shape=input_shape, modelType=3, lr=lr, n_layers=n_layers, n_hidden=n_hidden, rate_dropout=rate_dropout) wp.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epochs, shuffle=True, verbose=1, validation_data=(X_val, Y_val)) scores = wp.evaluate(X_test, y_test, verbose=0) print('Test loss:', scores[0]) print('Test accuracy:', scores[1]) wp.model.save(model_path)
def make_model_type(input_shape, _modelTyp=0, nb_epochs=100, batch_size=128, lr=0.01, n_layers=1, n_hidden=16, rate_dropout=0.3): model_path = 'model.%s' % input_shape[0] train_set, validation_set, test_set, numFeatures = ft.generateDataSetTXF( os.getcwd(), input_wind_size=input_shape[0], toDataSet=True) input_shape[1] = numFeatures wp = WindPuller(input_shape=input_shape, modelType=_modelTyp, lr=lr, n_layers=n_layers, n_hidden=n_hidden, rate_dropout=rate_dropout) wp.fit(train_set.images, train_set.labels, batch_size=batch_size, nb_epoch=nb_epochs, shuffle=True, verbose=1, validation_data=(validation_set.images, validation_set.labels)) scores = wp.evaluate(test_set.images, test_set.labels, verbose=0) print('Test loss:', scores[0]) print('Test accuracy:', scores[1]) wp.model.save(model_path)
def make_separate_model(nb_epochs=100, batch_size=128, lr=0.01, n_layers=1, n_hidden=14, rate_dropout=0.3, input_shape=[30, 73]): train_sets, test_sets = read_separate_feature("./ultimate_feature") wp = WindPuller(input_shape=input_shape, lr=lr, n_layers=n_layers, n_hidden=n_hidden, rate_dropout=rate_dropout) wp.build_model() for code, train_set in train_sets.items(): test_set = test_sets[code] input_shape = [train_set.images.shape[1], train_set.images.shape[2]] print(input_shape) model_path = 'model.%s' % code print(train_set.images.shape) wp.fit(train_set.images, train_set.labels, batch_size=batch_size, nb_epoch=nb_epochs, shuffle=False, verbose=1, validation_data=(test_set.images, test_set.labels), callbacks=[ TensorBoard(histogram_freq=1000), ModelCheckpoint(filepath=model_path + '.best.checkpoints', save_best_only=True, mode='min') ]) scores = wp.evaluate(test_set.images, test_set.labels, verbose=0) print('Test loss:', scores[0]) print('Test accuracy:', scores[1]) wp.model.save(model_path) saved_wp = wp.load_model(model_path) scores = saved_wp.evaluate(test_set.images, test_set.labels, verbose=0) print('Test loss:', scores[0]) print('test accuracy:', scores[1]) pred = saved_wp.predict(test_set.images, 1024) # print(pred) # print(test_set.labels) pred = numpy.reshape(pred, [-1]) result = numpy.array([pred, test_set.labels]).transpose() with open('output.' + str(input_shape[0]), 'w') as fp: for i in range(result.shape[0]): for val in result[i]: fp.write(str(val) + "\t") fp.write('\n')
def evaluate_model(model_path, code, input_shape=[30, 83]): extract_from_file("dataset/%s.csv" % code, code) train_set, test_set = read_feature(".", input_shape, code) saved_wp = WindPuller(input_shape).load_model(model_path) scores = saved_wp.evaluate(test_set.images, test_set.labels, verbose=0) print('Test loss:', scores[0]) print('test accuracy:', scores[1]) pred = saved_wp.predict(test_set.images, 1024) cr = calculate_cumulative_return(test_set.labels, pred) print("changeRate\tpositionAdvice\tprincipal\tcumulativeReturn") for i in range(len(test_set.labels)): print(str(test_set.labels[i]) + "\t" + str(pred[i]) + "\t" + str(cr[i] + 1.) + "\t" + str(cr[i]))