def test(model1,mains,appliances,op): begin=int(input("Enter start point for testing ==> ")) end=int(input("Enter end point for testing ==> ")) X=np.array(mains.iloc[begin:end]) y=np.array(appliances[begin:end]) y=y.reshape((len(X),op)) print(X.shape) pred1=model1.test(X) pred1=threshold(pred1,0.5) plt.subplot(2,1,1) plt.plot(y,'b') plt.ylabel("Actual") plt.subplot(2,1,2) plt.plot(pred1,'r') plt.ylabel("Predicted") plt.xlabel("time") plt.legend() plt.savefig(get_current_dir()+'/Outputs/'+model1.Model_name+".png") plt.close() print("********************************") print("********************************") print("Confusion Matrix for "+model1.Model_name) print(confusion_matrix(y[:,0],pred1[:,0])) print("********************************") print("Accuracy Score for "+model1.Model_name) print(accuracy_score(y[:,0],pred1[:,0])) print("********************************")
def create_model(self): model = Sequential() # 1D Conv model.add( Conv1D(10, 3, input_shape=(5, 1), padding="same", strides=1, activation='relu')) model.add(MaxPooling1D(pool_size=2, strides=None, padding='valid')) #Bi-directional LSTMs model.add( Bidirectional(LSTM(128, return_sequences=True, stateful=False), merge_mode='concat')) model.add( Bidirectional(LSTM(256, return_sequences=False, stateful=False), merge_mode='concat')) model.add(Dropout(0.05)) # Fully Connected Layers model.add(Dense(128, activation='tanh')) model.add(Dense(self.output_dim, activation='sigmoid')) # model.add(ThresholdedReLU(theta=0.5)) model.compile(loss='binary_crossentropy', optimizer='adam') plot_model(model, to_file=get_current_dir() + '/Models/' + self.Model_name + '.png', show_shapes=True) return model
def create_model(self): model = Sequential() # 1D Conv model.add( Conv1D(16, 4, activation="relu", padding="same", strides=1, input_shape=(4, 1))) model.add(Conv1D(8, 4, activation="relu", padding="same", strides=1)) # Bi-directional LSTMs model.add( Bidirectional(GRU(64, return_sequences=True, stateful=False), merge_mode='concat')) model.add( Bidirectional(GRU(256, return_sequences=False, stateful=False), merge_mode='concat')) # model.add(Dropout(0.1)) # Fully Connected Layers model.add(Dense(128, activation='relu')) model.add(Dense(self.output_dim, activation='sigmoid')) # model.add(ThresholdedReLU(theta=0.5)) model.compile(loss='mse', optimizer='adam') plot_model(model, to_file=get_current_dir() + '/Models/' + self.Model_name + '.png', show_shapes=True) return model
def create_model(self): model = Sequential() # 1D Conv model.add( Conv1D(8, 4, activation="linear", input_shape=(1, 1), padding="same", strides=1)) model.add(Flatten()) # Fully Connected Layers model.add(Dropout(0.2)) # model.add(Dense((self.batchsize-0)*8, activation='relu')) model.add(Dense((1 - 0) * 8, activation='relu')) model.add(Dropout(0.2)) model.add(Dense(128, activation='relu')) model.add(Dropout(0.2)) # model.add(Dense((self.batchsize-0)*8, activation='relu')) model.add(Dense((1 - 0) * 8, activation='relu')) model.add(Dropout(0.2)) # 1D Conv # model.add(Reshape(((self.batchsize-0), 8))) model.add(Reshape(((1 - 0), 8))) model.add( Conv1D(self.output_dim, 4, activation="linear", padding="same", strides=1)) model.compile(loss='mse', optimizer='adam') plot_model(model, to_file=get_current_dir() + '/Models/' + self.Model_name + '.png', show_shapes=True) return model
def exists(self): if (os.path.isfile(get_current_dir() + '/Models/' + self.Model_name + '.h5')): return True else: return False
def load(self, name, dir=get_current_dir()): self.model = load_model(str(dir + "/Models/" + name + ".h5")) return self
def save(self): self.model.save(get_current_dir() + '/Models/' + self.Model_name + '.h5')
def load(self, name, dir=get_current_dir()): self.model = load_model(str(dir + '/Models/' + name + '.h5')) return self