コード例 #1
0
class NeuralNetworkClassifier:

	def __init__(self, mimetype, inputUnits, outputUnits, logging = True):
		"""
		constructor for the neural network classifier
		"""
		self.outputHeader = "#nn"
		self.inputUnits = inputUnits
		self.outputUnits = outputUnits
		self.mimetype = mimetype
		self.numberOfHiddenUnits = None
		self.nn = None
		self.testError = None

		if logging:
			self.setLoggingLevel()

	def setLoggingLevel(self):
		"""
		This function sets the logging level of the neural network
		"""
		logging.basicConfig(
            format="%(message)s",
            level=logging.INFO,
            stream=sys.stdout)

	def buildModel(self,numberOfLayers, listOfLayerSpecification, learningRate, numIters):
		"""
		This function builds the neural network layers. The input 
		is the number of layers and the second parameter contains thre
		type of layers to be used
		"""
		self.numberOfHiddenUnits = numberOfLayers
		layers = []
		for i in range(0,numberOfLayers):
			layers.append(Layer(listOfLayerSpecification[i], units = 1))
		layers.append(Layer("Softmax"))
		self.initializeNeuralNetwork(layers,learningRate,numIters)

	def initializeNeuralNetwork(self,layers,learningRate,numIters):
		"""
		This will build the Neural Network and make it ready for training
		"""
		self.nn = Classifier(
    		layers= layers,
    		learning_rate=learningRate,
    		n_iter=numIters
    	)

	def trainNetwork(self,X, Y):
		"""
		Training the neural network
		"""
		self.nn.fit(X, Y)

	def validateNetwork(self,X, Y):
		"""
		Validate the neural network
		"""
		YPred = self.nn.predict(X)
		print accuracy_score(Y, YPred)

	def testNetwork(self,X, Y):
		"""
		Test the neural network
		"""
		YPred = self.nn.predict(X)
		self.testError = 1 - accuracy_score(Y, YPred)
		print accuracy_score(Y, YPred)

	def dumpNeuralNetParameters(self, filename):
		"""
		Dump Neural Network dumpNeuralNetParameters
		"""
		nn_parameters = self.nn.get_parameters()

		with open(filename+".nnmodel", "wb") as myfile:
			myfile.write(self.outputHeader + " " + self.mimetype + " "+
				str(self.inputUnits)+" "+ str(self.numberOfHiddenUnits) + " "+
				str(self.outputUnits)+" "+str(self.testError)+"\n")

		for i in nn_parameters:
			if "hidden" in i[2]:
				for j in i[0]:
					with open(filename+".nnmodel", "a") as myfile:
						myfile.write(str(j[0]) + "\t")
コード例 #2
0
X_train = X[:train_set_size]
X_test = X[train_set_size:]
y_train = y[:train_set_size]
y_test = y[train_set_size:]
nn = Classifier(
    layers = [
    Layer("Rectifier", units=100),
    Layer("Softmax")],    
    n_iter=1,
    learning_rule = 'rmsprop')
#w_train = np.array((X_train.shape[0],))
#w_train = []
#w_train[y_train == 0] = 1.2
#w_train[y_train == 1] = 0.8

#nn.fit(X_train, y_train, w_train)
nn.fit(X_train, y_train)

#y_valid = nn.predict(X_valid)
nn.get_parameters()
nn.predict(X_train)
#score = nn.score(X_test, y_test)
#plt.scatter(X_train, y_train,  color='black')
#plt.scatter(X_test, y_test)
#plt.show()
#plt.scatter()
#logging.basicConfig(
 #           format="%(message)s",
  #          level=logging.DEBUG,
   #         stream=sys.stdout)