def seq(x_train,y_train,x_val,y_val,x_test,y_test):
#Defining the structure of the neural network
#Creating a Network, with 2 Convolutional layers
model=Sequential()
model.add(Conv2D(128,(2,5),activation='relu',input_shape=(1,39,20)))
model.add(Conv2D(128,(2,3)))
model.add(Conv2D(64,(2,3)))
model.add(MaxPooling2D((2,2)))
model.add(Flatten())
model.add(Dense(1024,activation='relu')) #Fully connected layer 1
model.add(Dropout(0.5))
model.add(Dense(2,activation='softmax')) #Output Layer
model.summary()
# f=open('/home/siddharthm/scd/scores/'+common_save+'-complete.txt','rb+')
# print f >> model.summary()
data_saver(str(model.to_json()))
# f.close()
sgd=SGD(lr=0.1)
early_stopping=EarlyStopping(monitor='val_loss',patience=4)
reduce_lr=ReduceLROnPlateau(monitor='val_loss',patience=4,min_lr=0.0000001)
#Compilation region: Define optimizer, cost function, and the metric?
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
#Fitting region:Get to fit the model, with training data
checkpointer=ModelCheckpoint(filepath=direc+common_save+'.json',monitor='val_acc',save_best_only=True,save_weights_only=True)
#Doing the training[fitting]
model.fit(x_train,y_train,epochs=EPOCH,batch_size=batch,validation_data=(x_val,y_val),callbacks=[checkpointer,early_stopping,reduce_lr])
model.save_weights(direc+common_save+'-weights'+'.json') #Saving the weights from the model
model.save(direc+common_save+'-model'+'.json')#Saving the model as is in its state
### SAVING THE VALIDATION DATA ###
scores=model.predict(x_val,batch_size=batch)
sio.savemat(direc+name_val+'.mat',{'scores':scores,'ytest':y_val}) #These are the validation scores.
classes=model.predict_classes(x_train,batch_size=batch)
### ------------- ###
### SAVING THE TESTING DATA ###
#scores_test=model.predict(x_test,batch_size=batch)
#sio.savemat(direc+name_test+'.mat',{'scores':scores_test,'ytest':y_test})
### ------------- ###
# print model.evaluate(x_test,y_test,batch_size=batch)
#predictions=model.predict(x_val,batch_size=batch)
#print "Shape of predictions: ", predictions.shape
#print "Shape of y_test: ",y_test.shape
return classes
#Non-function section
#y_test,predictions,classes=seq(x_train,y_train,x_val,y_val,x_test,y_test) #Calling the seq model, with 2 hidden layers
评论列表
文章目录