Esempio n. 1
0
 def testTrainingOnSepervisedDataset(self):
     DS = SupervisedDataSet(2, 1)
     DS.addSample([ 0, 0 ] , [0])
     DS.addSample([ 0, 1 ] , [1])
     DS.addSample([ 1, 0 ] , [1])
     DS.addSample([ 1, 1 ] , [0])
     
     network = N = buildNetwork(2, 4, 1)
     trainer = BackpropTrainer(N, learningrate = 0.01, momentum = 0.99)
     trainer.verbose = False
     
     nnf = NeuralNetworkFactory(network, trainer, seed=2, iterationsNum=500)
     nnClassifier = nnf.buildClassifier(DS)
     
     self.assertAlmostEqual(nnClassifier.getPrediction([0, 0]), 0, delta=0.01) 
     self.assertAlmostEqual(nnClassifier.getPrediction([0, 1]), 1, delta=0.01)
     self.assertAlmostEqual(nnClassifier.getPrediction([1, 0]), 1, delta=0.01)
     self.assertAlmostEqual(nnClassifier.getPrediction([1, 1]), 0, delta=0.01)  
def load_image_vector(img_path):
    M = cv2.imread(img_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
    M = cv2.resize(M, (IM_WIDTH, IM_HEIGHT))
    M = M.reshape((1, IM_AREA))
    return M[0]

def save_network_state(signum, stack):
    file_name = "autoencoder_100px_300faces_{}{}".format(int(time()), randrange(0, 10))
    f = open(file_name, "wb")
    pickle.dump(network, f)
    f.close()
    print "network saved as : {}".format(file_name)


network = buildNetwork(IM_AREA, IM_AREA * 2 // 3, IM_AREA)
dataset = SupervisedDataSet(IM_AREA, IM_AREA)

for img_name in os.listdir(IMAGES_PATH):
    try:
        vector = load_image_vector(IMAGES_PATH + img_name)
        if vector != None and len(vector) == IM_AREA:
            dataset.addSample(vector, vector)
    except:
        continue

print "dataset with {} elements done...".format(dataset.getLength())

signal.signal(signal.SIGUSR1, save_network_state)

trainer = BackpropTrainer(network, dataset)
#preload all images
images = []

for img_name in os.listdir(IMAGES_PATH):
    try:
        vector = load_image_vector(IMAGES_PATH + img_name)
        if vector != None and len(vector) == IM_AREA:
            images.append(vector)
    except:
        continue

print "dataset with {} elements done...".format(len(images))

signal.signal(signal.SIGUSR1, save_network_state)

#stochastic training
for i in range(len(images) // 2):
    img = choice(images)
    dataset = SupervisedDataSet(IM_AREA, IM_AREA)
    dataset.addSample(img, img)
    trainer = BackpropTrainer(network, dataset)
    trainer.train()

print "training completed... (1 pass)"

save_network_state(0, 0)

print "done !! :D"

Esempio n. 4
0
def loadNetwork(filename):
	fileObject = open(filename,'r')
	return pickle.load(fileObject)

if __name__ == '__main__':
	# read the training file
	print "Reading in training file..."
	raw_data = numpy.genfromtxt('mydata.csv', delimiter=",")
	numFeatures = raw_data[0].size
	numData = raw_data.size / numFeatures

	# initialize the neural network
	print "Initializing neural network..."
	net = buildNetwork(numFeatures - 1, numFeatures - 1, 2, bias=True)
	ds = SupervisedDataSet(numFeatures - 1, 2)

	# add the input to the dataset
	print "Loading the dataset"
	for i in range(0, numData):
		data = raw_data[i]
		features = tuple(data[0:numFeatures - 1])
		label = int(data[numFeatures - 1])
		ds.addSample(features, (label,))

	# train the network on the dataset
	print "Training neural net"
	trainer = BackpropTrainer(net, ds)
	for i in range(0, 10):
		print "error: " + str(trainer.train())
Esempio n. 5
0
    "difficulty",
    "miners-revenue",
    "blocks-size",
]  # "avg-confirmation-time"
# features = ["market-price", "transaction-fees", "n-transactions", "estimated-transaction-volume-usd"]
(train, test, trainOutput, testOutput) = getData(features)
# take out yesterday's data for later
# x_yesterday = X[len(X)-1]
# y_yesterday = y[len(y)-1]
# X = X[:len(X)-1]
# y = y[:len(y)-1]
num_features = len(features)

print len(train), len(test)

d = SupervisedDataSet(len(features) - 1, 1)
# d = ClassificationDataSet(len(features)-1)
for i in range(len(train)):
    # print(tuple(x[i]),tuple(y[i],))
    d.addSample(tuple(train[i]), tuple(trainOutput[i]))
# for sample_x,sample_y in x,y: d.addSample(sample_x,sample_y)
# d.setField('class', y)

# nn = buildNetwork(len(features)-1, len(features)-1, 1)
nn = FeedForwardNetwork()
inLayer = LinearLayer(len(features) - 1)
hiddenLayer = SigmoidLayer(len(features) - 1)
outLayer = LinearLayer(1)
nn.addInputModule(inLayer)
nn.addModule(hiddenLayer)
nn.addOutputModule(outLayer)