def dense_temp(f_in): X = diff_length_dat(f_in) X = np.array(X, dtype=float) y = fun2('number_category.csv') x_train, x_test, y_train, y_test = cross_validation.train_test_split( X, y, test_size=0.2) y_train = category_to_target(y_train) y_test = category_to_target(y_test) model = Sequential() model.add(Dense(nb_classes, input_shape=(15, ))) model.add(Dropout(0.1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() acc_list = [] score, acc = model.evaluate(x_test, y_test, batch_size=batch_size) acc_list.append(acc) for epoch in range(nb_epochs): print('Train...') model.fit(x_train, y_train, batch_size=batch_size, nb_epoch=1, validation_split=0.05) score, acc = model.evaluate(x_test, y_test, batch_size=batch_size) acc_list.append(acc) # print(model.predict_proba(X)) print('\nTest score:', score) print('Test accuracy:', acc) plt.plot(range(0, nb_epochs + 1), acc_list) plt.show()
def pure_temp_prediction(): X = diff_length_csv('tiwencheck.csv') X = np.array(X) y = fun2('number_category.csv') X_train, X_test, y_train, y_test = cross_validation.train_test_split( X, y, test_size=0.2) y_train = category_to_target(y_train) y_test = category_to_target(y_test) x_train = reshape_dataset(X_train) x_test = reshape_dataset(X_test) model = Sequential() model.add(LSTM(16, input_shape=(12, 1))) model.add(Dropout(0.25)) model.add(Dense(nb_classes, bias=False)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() print('Train...') model.fit(x_train, y_train, batch_size=batch_size, nb_epoch=5, validation_split=0.05) score, acc = model.evaluate(x_test, y_test, batch_size=batch_size) print(model.predict_proba(reshape_dataset(X))) print('Test score:', score) print('Test accuracy:', acc)
def dense_temp(f_in='5_day_50_check.csv'): X = diff_length_csv(f_in) X = np.array(X, dtype=float) y = fun2('number_category.csv') x_train, x_test, y_train, y_test = cross_validation.train_test_split( X, y, test_size=0.2) y_train = category_to_target(y_train) y_test = category_to_target(y_test) model = Sequential() model.add(Dense(nb_classes, input_shape=(50, ), b_regularizer=l1(0.01))) model.add(Dropout(0.5)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() print('Train...') model.fit(x_train, y_train, batch_size=batch_size, nb_epoch=30, validation_split=0.05) score, acc = model.evaluate(x_test, y_test, batch_size=batch_size) print(model.predict_proba(X)) print('\nTest score:', score) print('Test accuracy:', acc)
def temp(f_in='5_day_50_check.csv'): if f_in[-1] == 't': X = diff_length_dat(f_in) else: X = diff_length_csv(f_in) X = np.array(X, dtype=float) print(X.shape) y = fun2('number_category.csv') x_train, x_test, y_train, y_test = cross_validation.train_test_split( X, y, test_size=0.2) y_train = category_to_target(y_train) y_test = category_to_target(y_test) model = Sequential() model.add( Reshape(input_shape=(temp_length, ), target_shape=(temp_length, 1))) model.add( Convolution1D(border_mode='same', filter_length=2, nb_filter=nb_filter)) model.add(Reshape(target_shape=(temp_length * nb_filter, ))) model.add(Dense(32)) model.add(Dropout(0.5)) model.add(Activation('relu')) model.add(Dense(nb_classes)) model.add(Activation('sigmoid')) # model2=Sequential() # model2.add(Dense(input_shape=(50,),output_dim=3)) # model2.add(Merge([model,model2])) model.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.001), metrics=['accuracy']) model.summary() for epoch in range(nb_epochs): print('Train...') model.fit(x_train, y_train, batch_size=batch_size, nb_epoch=1, validation_split=0.05) score, acc = model.evaluate(x_test, y_test, batch_size=batch_size) # print(model.predict_proba(X)) print('\nTest score:', score) print('Test accuracy:', acc)
def temp_lstm_2(f_in='5_day_50_check.csv'): if f_in[-1] == 't': X = diff_length_dat(f_in) else: X = diff_length_csv(f_in) X = np.array(X, dtype=float) print(X.shape) y = fun2('number_category.csv') y = y - 1 x_train, x_test, y_train, y_test = cross_validation.train_test_split( X, y, test_size=0.2) model = Sequential() model.add( Reshape(input_shape=(temp_length, ), target_shape=(temp_length, 1))) model.add(LSTM(16, )) model.add(Dense(32, )) model.add(Dropout(0.4)) model.add(Activation('relu')) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.002), metrics=['accuracy']) model.summary() acc_list = [] score, acc = model.evaluate(x_test, y_test, batch_size=batch_size) acc_list.append(acc) for epoch in range(nb_epochs): print('Train...') model.fit(x_train, y_train, batch_size=batch_size, nb_epoch=1, validation_split=0.05) score, acc = model.evaluate(x_test, y_test, batch_size=batch_size) acc_list.append(acc) print(model.predict_classes(X)) # print(model.predict_proba(X)) print('\nTest score:', score) print('Test accuracy:', acc) plt.plot(range(0, nb_epochs + 1), acc_list) plt.show()
def length_temp_prediction(): X = diff_length_csv('5_day_50_check.csv') # X=fun2('5_day_50_check.csv') # X = pad_sequences(X, maxlen=maxlen, padding='post', truncating='post', dtype='float') X = np.array(X, dtype=float) y = fun2('number_category.csv') X_train, X_test, y_train, y_test = cross_validation.train_test_split( X, y, test_size=0.2) y_train = category_to_target(y_train) y_test = category_to_target(y_test) x_train = reshape_dataset(X_train) x_test = reshape_dataset(X_test) model = Sequential() # model.add(Masking(mask_value=0, input_shape=(maxlen, 1))) model.add(LSTM(3, input_shape=(maxlen, 1), return_sequences=True)) model.add(Dropout(0.25)) model.add(Reshape((maxlen * 3, ))) model.add(Dense(nb_classes, b_regularizer=l1(0.01))) model.add(Dropout(0.25)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() print('Train...') model.fit(x_train, y_train, batch_size=batch_size, nb_epoch=30, validation_split=0.05) score, acc = model.evaluate(x_test, y_test, batch_size=batch_size) print(model.predict_proba(reshape_dataset(X))) print('Test score:', score) print('Test accuracy:', acc)
from keras.layers import LSTM, Dense,Merge,Dropout from keras.layers import recurrent from keras.layers import Activation from keras.preprocessing.sequence import pad_sequences from sklearn import cross_validation import numpy as np np.random.seed(1337) batch_size = 16 epochs = 40 nb_classes =3 RNN = recurrent.LSTM maxlen =200 in_file_length =57 if __name__ =='__main__': X = fun2('number_age_col55tran.csv') y = fun2('number_category.csv') X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.2) y_train = category_to_target(y_train) y_test = category_to_target(y_test) model =Sequential() model.add(Dense(64,input_dim=in_file_length)) model.add(Dropout(0.25)) model.add(Activation('relu')) model.add(Dense(nb_classes)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary()
def temp_lstm(f_in='5_day_50_check.csv'): if f_in[-1] == 't': X = diff_length_dat(f_in) else: X = diff_length_csv(f_in) X = np.array(X, dtype=float) y = fun2('number_category.csv') nb_x_train = fun3(f_in='nb_train_%d.dat' % (data_version)) nb_x_test = fun3(f_in='nb_test_%d.dat' % (data_version)) nb_x_validation = fun3(f_in='nb_validation_%d.dat' % (data_version)) x_train, x_test, x_validation, y_train, y_test, y_validation = get_train_test_data( X, y, nb_x_train, nb_x_test, nb_x_validation) probability_validation = (sum(y_validation) - len(y_validation)) / len(y_validation) probability_test = (sum(y_test) - len(y_test)) / len(y_test) print('probability_validation:', probability_validation) y_train = category_to_target(y_train) y_test = category_to_target(y_test) y_validation = category_to_target(y_validation) # print(y_train) #get_model model = temp_model2() #evaluation best_epoch = 0 best_acc = probability_validation acc_list = [] score, acc = model.evaluate(x_validation, y_validation, batch_size=batch_size) acc_list.append(acc) print('Train...') for epoch in range(nb_epochs): model.fit(x_train, y_train, batch_size=batch_size, verbose=0, nb_epoch=1, shuffle=True) score, acc = model.evaluate(x_validation, y_validation, batch_size=batch_size) acc_list.append(acc) print('Test score:', score) print('Test accuracy:', acc) if acc > best_acc: best_acc = acc model.save_weights('temp_dataset_%d_epoch_%d.h5' % (data_version, epoch)) if best_epoch != 0: os.remove('temp_dataset_%d_epoch_%d.h5' % (data_version, best_epoch)) best_epoch = epoch plot(range(0, nb_epochs + 1), acc_list) print(best_epoch) print(best_acc) #test model.load_weights('temp_dataset_%d_epoch_%d.h5' % (data_version, best_epoch)) score, acc = model.evaluate(x_test, y_test, batch_size=batch_size) print('Test score:', score) print('probabitity_test:', probability_test) print('Test accuracy:', acc) savefig('temp_dataset_%d.png' % (data_version))